The path for mutual access between servlet, jsp, and html in java web.

Source: Internet
Author: User

The path for mutual access between servlet, jsp, and html in java web.

In java web, the Error 404 cannot find a webpage is often caused by incorrect access paths.

The path in java web can be divided into two situations by my method. Of course, the two use the same relative path. This article only describes the absolute path.

Scenario 1: pointing to external web components has little to do with itself. This type includes the path labels used in html, such as href in tags; redirect sendRedirect (path) in servlet and jsp );

Case 2: pointing to the internal web components is related to itself. What I see for the moment is servlet or jsp forwarding.

Assume that there is a login.html, index. jsp under the myappproject, and two servletA and servletB are written.

Address Configuration in web. xml:

/Servlet/servletA

/Servlet/servletB

In Case 1: If the path starts with/, this/is equivalent to http: // localhost: 8080/

1、login.html has a form that has been submitted to servletA. The path to the action is as follows:

Absolute path: action = "/myapp/servlet/servletA" ------ http: // localhost: 8080/myapp/servlet/servletA

Relative Path: action = "servlet/servletA" ------ http: // localhost: 8080/myapp/servlet/servletA

2most login.html has a link to index. jsp.

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

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

3. Redirect to servletA in index. jsp

Absolute path: sendRedirect ("/myapp/servlet/servletA"); ------ http: // localhost: 8080/myapp/servlet/servletA

Relative Path: sendRedirect ("servlet/servletA"); --- http: // localhost: 8080/myapp/servlet/servletA

In Case 2: If the path starts with/, this/is equivalent to http: // localhost: 8080/myapp/

1. Forward servletA to servletB

Absolute path: request. getRequestDispatcher ("/servlet/servletB"). forward (request, response );

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

Relative Path: request. getRequestDispatcher ("servlet/servletB"). forward (request, response );

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

Note:

We recommend that you use an absolute path. The relative path is relative to the path (Source Address) in the address bar of the current browser ).

It may occur that you write a relative path (target path) on a page, because the forwarding does not change the address. If someone else arrives at your page through forwarding, the source address in the address bar is uncertain. Since you are not sure that you are using a relative path, it is very likely that an error occurs. Therefore, we recommend that you use an absolute path to avoid this problem.

Obtain the project path and absolute path:

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

String p = this. getServletContext (). getRealPath ("/"); ----- G: \ environment \ tomcat \ webapps \ myapp \


Summary:

It mainly points to external and internal, and "/" indicates the host path, while "/" indicates the current project path.



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.