Home >Java >javaTutorial >Detailed explanation of web path issues in servlets in Java

Detailed explanation of web path issues in servlets in Java

黄舟
黄舟Original
2017-07-27 15:19:361450browse

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.


4.


response.getWriter().write("<meta http-equiv=&#39;refresh&#39; content=&#39;3;url=/myservlet/index.jsp&#39;>");

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.


5.



<form action=”/myservlet/servlet/ServletRegister”>
</form>

Submit the form to the server in the browser, which is the address given to the browser.


6.



<a href=”/myservlet/servlet/ServletRegister”></a>
<img src=”/myservlet/servlet/ServletRegister” />

are all addresses for the browser.


Note here: For web resources such as Servlet, the path is mainly based on the external access path name (virtual path) (defined in the 870ae7edaa11700bcea972d006efb06e tag in the web.xml document). The difference is that the web application name needs to be added to the browser address. Example:


For server address:



 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!

Statement:
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