A common method for JSP and servlet to jump and get the path

Source: Internet
Author: User
Tags filter command line relative root directory

Today, the process of organizing the project, between the JSP and the servlet jumps, once, I have a servlet path defined as "/someservlet", that is defined as the root directory, because the compatibility is better, but after using MyEclipse, The new servlet default path is "/servlet/someservlet", which is easy to manage, and is better suited for setting filter for the servlet alone (refer to this article for settings on the filter). My JSP file is currently placed in the root directory of the project, which forms the path structure of the following figure:

/projectroot/
|--servlet/
|  |--servlet1
|  |--servlet2
|
|--myjsp1.jsp
|--myjsp2.jsp

There are two ways to have a servlet jump:

1, Sendredirect () way

Response.sendredirect (String targeturl);

2, Requestdispather Way

RequestDispatcher RequestDispatcher = Request.getrequestdispatcher (String targeturl);
Requestdispatcher.forward (request, response);

The first way is to send a notification to the user's browser, and then the browser to send a jump request to the server, so compare similar to the user to point to the URL of the jump, this way if you need to pass parameters to the jump page, You need to use the session or get to write the parameters explicitly in the TargetUrl (such as: ooxx.jsp?id=1), and in most cases, because of the limitations of the Got method, this jump method can only take a relatively simple parameter.

The second way somewhat similar to C # in the Server.Transfer () method, that is, server-side jump, from the phenomenon is the user's browser content has changed, but the browser's address bar is unchanged or the old address. This way the server directly control the request and response direction and parameters, from the command line parameters can see this point. This facilitates the programmer to control the transfer of parameters, can almost pass any type of parameters, as long as the simple use of the setattribute () method can:

Request.setattribute (String attriname, Object attrivalue);

But also because it is a server-side jump, the user's browser's address bar is not changed. So, if the project path structure is as shown in the previous illustration, then:

1, from the JSP jump to the servlet

Just use the relative path "Serlvet/someservlet" simply.

2, when you jump from the servlet to another servlet

Because the servlet is all under the same path, you can write a relative path directly, such as "./someservlet" or "Someservlet" directly.

3, from the servlet jump to JSP

Because the servlet path is "Servlet/someservlet", if you want to use the Requestdispather method to jump, the JSP page will take the address of the address bar as the current directory to find the method, JavaScript, CSS and so on. So often have friends encounter JavaScript error "ext undefined" is because the JSP page can not find Ext JS file. So in this case, you need to use an absolute path to tell the JSP where to get the resources. Java for more ways to get paths, the test is as follows:

Project root directory: http://localhost:8080/TestProject/

JSP test: http://localhost:8080/TestProject/TestPath.jsp

1<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
2 pageencoding=" UTF-8 "%>
3<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
4

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.