This article mainly introduces the relevant information on the web path problem of servlet in detail. It has certain reference value. Interested friends can refer to it
First of all, in web development, pay attention to Special emphasis here is in web development, that is, when we use Servlet to process web applications:
It is best for the address to start with "/"! ! !
Starting with "/" represents different relative root directories in different objects, methods, and labels. The difference is that programmers should pay attention to whether this address is used by the server or the client browser.
If it is an address used by the server, "/" represents the current web project;
If it is used by the client browser Address, "/" represents the host in the server, or represents the root directory where the server deploys web applications (such as Tomcat's [webapps] directory).
The following are explained through several examples:
1.
this.getServletContext().getRealPath("/index.jsp");
operates the resources in the server on the server side, so it is the address used by the server, indicating the index.jsp under the web project.
2.
##
this.getServletContext().getRequestDispatcher("/index.jsp");
response.sendRedirect("/myservlet/index.jsp");
response.getWriter().write("<meta http-equiv='refresh' content='3;url=/myservlet/index.jsp'>");
<form action=”/myservlet/servlet/ServletRegister”> </form>
<a href=”/myservlet/servlet/ServletRegister”></a> <img src=”/myservlet/servlet/ServletRegister” />
request.getRequestDispatcher("/servlet/ServletDemo").forward(request, response);
response.sendRedirect("/myservlet/ servlet/ServletDemo");
The above is the detailed content of Detailed explanation of web path issues in servlets in Java. For more information, please follow other related articles on the PHP Chinese website!