Home >Java >javaTutorial >Detailed explanation of web path issues in servlets in Java
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");3.
response.sendRedirect("/myservlet/index.jsp");Redirect needs to match the response header "Location" to send the response to the browser, and then the browser will resend the request to the new URL in the redirect, so it is the address used by the client browser. [myservlet] is the name of the web application and exists in the [webapps] directory of Tomcat.
response.getWriter().write("<meta http-equiv='refresh' content='3;url=/myservlet/index.jsp'>");Obviously this will be parsed by the browser and the HTML language will be used as the response header in the page, so it is The address given to the browser requires the web application name.
<form action=”/myservlet/servlet/ServletRegister”> </form>Submit the form to the server in the browser, which is the address given to the browser.
<a href=”/myservlet/servlet/ServletRegister”></a> <img src=”/myservlet/servlet/ServletRegister” />are all addresses for the browser.
request.getRequestDispatcher("/servlet/ServletDemo").forward(request, response);For client browser address:
response.sendRedirect("/myservlet/ servlet/ServletDemo");Another note: "/" is usually used to represent virtual addresses, such as URL addresses, while "\" is usually used for file addresses on the system hard disk.
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!