Home> Java> javaTutorial> body text

How to solve the problem that Java Web project cannot access html files

WBOY
Release: 2023-04-27 20:34:12
forward
1629 people have browsed it

The pitfall problem and reasons

The pitfall problem

Because the blogger’s JavaWeb is a crash course, he is not very familiar with some knowledge points, so today’s problem appeared&mdash ;—Tomcat cannot access the html file.

404 will always appear when running. Every time this happens it's annoying and confusing. Although this problem actually does not have a big impact on some projects, it will make my project directory very cluttered. Generally speaking, the problem is that if you don't solve it, a bunch of static resources will appear in one folder. This may cause it to take half a day to find some project files later.

The reason for the trap

Due to carelessness, I put some html files under WEB-INF. Because the resources under WEB-INF cannot be accessed directly (WEB-INF is Java's web application security directory, it is only open to the server and invisible to the client.), it can only be accessed through requests and forwarding. Therefore, it cannot be accessed on the browser (redirection is also not possible).

Java Web项目访问不到html文件怎么解决

Solution

As mentioned above, direct access is not accessible. But we can access html files through forwarding and requests. In this project, the blogger uses request access. In other words, the front end first sends a signal to the back end - indicating that I need to request a certain web page. Then the backend reacts accordingly and then returns to jump to the corresponding interface.

public class regist extends ssm.ViewBaseServlet{//继承了一个类,这个类就是用来解析html时用的。 @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.processTemplate("/user/regist",req,resp);//调用了类的方法,其实这个地方可以选择转发更方便! }
Copy after login

Configuration of web.xml:

jump database.jump   jump /jump 
Copy after login

Or add a @WebServlet("/login") to the Java file.

Of course, we can also put static resources outside WEB-INF at the same level as static. Generally, if placed outside, you need to use an interceptor/filter to complete permission verification and determine legitimate users.

The above is the detailed content of How to solve the problem that Java Web project cannot access html files. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!