Summary of jump between JSP and Servlet

Source: Internet
Author: User

Request. getcontextpath () indicates/project name. Example:/news01

 

1. jump from the JSP page to another page or Servlet

 

Use the full path (/project name/file path) to jump from the JSP page to other pages ). For example, the project name is news01, and the page Directory is as follows:

 

1) For example, jump from the index. jsp page to the list. jsp page under the main Folder:

 

Link path:/news01/main/list. jsp

 

2) For example, jump from the index. jsp page to your own page:

 

Link path:/news01/index. jsp

 

3) For example, jump from the list. jsp page to the index. jsp page:

 

Link path:/news01/index. jsp

 

4) for example, jump from the list. jsp page to the insert. jsp page.

 

Link path:/news/main/insert. jsp

 

5) The servlet URL is/servlet/testservlet.

 

The link to the servlet jump from any JSP page is/news01/servlet/testservlet

 

6) The servlet URL is/testservlet.

 

The link to the servlet jump from any JSP page is/news01/testservlet

 

7) Example of the Form Action path:

<Form action = "/news01/servlet/loginservlet" method = "Post">

 

Ii. jump from servlet to JSP page

 

(1) Use the requestdispatcher method:

 

1. Single Layer: The servlet URL is in the root directory (it does not need to start with a slash)

 

(1) From testservlet (urlpattern:/testservlet) to index. jsp under the root directory

 

The jump statement is:

 

Request. gerrequestdispatcher ("index. jsp"). Forward (request, response );

 

(2) From testservlet (urlpattern:/testservlet) to list. jsp under the main directory

 

The jump statement is:

 

Request. gerrequestdispatcher ("Main/list. jsp"). Forward (request, response );

 

2. Double or multi-layer: the servlet URL is under the root directory (start with a slash, and the slash here represents the current application-this project represents/news01/

 

(1) From testservlet (urlpattern:/servlet/testservlet) to index. jsp under the root directory

 

The jump statement is:

 

Method 1: Relative Path

 

Request. gerrequestdispatcher ("../index. jsp"). Forward (request, response );

 

Method 2: Start with a slash

 

Request. gerrequestdispatcher ("/index. jsp"). Forward (request, response );

(3) from testservlet (urlpattern:/servlet/testservlet) to list. jsp in the main directory

 

The jump statement is:

 

Method 1: Relative Path

 

Request. gerrequestdispatcher ("../main/list. jsp"). Forward (request, response );

 

Method 2: Start with a slash

 

Request. gerrequestdispatcher ("/main/list. jsp"). Forward (request, response );

 

(2) Use response. sendredirect ()

 

Whether the servlet is a single layer or a double layer, use the following method:

 

Response. sendredirect ("/news01/index. jsp ");

 

Response. sendredirect ("/news01/main/list. jsp ");

 

Request. getcontextpath () indicates the/project name. Example:/news01

 

Response. sendredirect (request. getcontextpath () + "/main/list. jsp ");

 

Response. sendredirect (request. getcontextpath () + "/index. jsp ");

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.