The path of servlet, JSP, and HTML access to each other in the Java Web.

Source: Internet
Author: User

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, Web Components that point to the outside are not very related to themselves, such as tags that use paths in HTML, such as Href;servlet in <a> tags and redirects in JSPs sendredirect (path);

situation Two, The Web component that points to the inside is related to itself, which I see for the moment: a 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 the case one: If you start with/in the path, 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 the case of two: If you start with/in the path, 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

Note:

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.



Related Article

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.