Path problems in the Java Web.

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/liang5630/article/details/38474543

If there is infringement, please contact me promptly delete

In the Java Web is often found in the 404 Web page error, the reason is generally the wrong path to access.

The path in the Java Web is divided into two cases by my method, of course, the use of relative path is consistent, this article only say absolute path.

Situation one, the external Web component and itself does not have a small relationship, this category is: HTML using the path of the label, such as the <a> tag in the Href;servlet and JSP in the redirect Sendredirect (path);

Situation two, pointing to the internal Web components and its own relationship, this kind of I see temporarily: servlet or JSP forwarding

Suppose there is a login.html,index.jsp under the MyApp project, and two Servleta and Servletb are written.

Address configuration in Web. xml:

<url-pattern>/servlet/servletA</url-pattern>

<url-pattern>/servlet/servletB</url-pattern>

In case one: if the/start in the path, then this/equivalent http://localhost:8080/

1, login.html has a form form submitted to Servleta, then the action to fill the path:

Absolute path mode: action= "/myapp/servlet/servleta"------Http://localhost:8080/myapp/servlet/servletA

Relative path mode: action= "Servlet/servleta"------Http://localhost:8080/myapp/servlet/servletA

2, login.html has a <a> link to index.jsp so

Absolute path mode: href= "/myapp/index.jsp"------http://localhost:8080/myapp/index.jsp

Relative path mode: action= "index.jsp"------http://localhost:8080/myapp/index.jsp

3. Redirect to Servleta in index.jsp

Absolute path mode: Sendredirect ("/myapp/servlet/servleta"); ------Http://localhost:8080/myapp/servlet/servletA

Relative path mode: Sendredirect ("Servlet/servleta"); ---Http://localhost:8080/myapp/servlet/servletA

In case two: if the/start in the path, then this/equivalent http://localhost:8080/myapp/

1.servletA forwarding to Servletb

Absolute path mode: Request.getrequestdispatcher ("/servlet/servletb"). Forward (request, response);

--------Http://localhost:8080/myapp/servlet/servletB

Relative path mode: Request.getrequestdispatcher ("Servlet/servletb"). Forward (request, response);

--------Http://localhost:8080/myapp/servlet/servletB

Attention:

Absolute paths are recommended, relative paths are paths relative to the current browser's address bar (source address).

May appear: You write a relative path on a page (the target path), because the forwarding is not change the address, then if someone is forwarded to your page, then the address bar source address is not sure, since it is not certain that you use relative path relative to this indeterminate path is very likely to error, Therefore, it is recommended to use absolute paths to avoid this problem.

Get the project path and absolute path:

Project path: String Path=request.getcontextpath (); ----/myapp

String P=this.getservletcontext (). Getrealpath ("/"); -----G:\environment\tomcat\webapps\myapp\

Summarize:

Here the main figure is to point to the external internal, external "/" means the host path, internal time "/" represents the current project path.

Path problems in the Java Web.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.