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.
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).
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);//调用了类的方法,其实这个地方可以选择转发更方便! }
Configuration of web.xml:
jump database.jump jump /jump
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!