Path Problems in JSP

Source: Internet
Author: User

First, let's take a look at an Important Role in JSP: El implicit object pagecontext

The implicit object of pagecontext corresponds to javax. servlet. JSP. pagecontext objects and hidden objects are automatically added to pagecontext. You can use them to obtain servlet objects corresponding to JSP-related hidden objects, such as getrequest () servletrequest can be obtained, while servletconfig () can be obtained, servletconfig can be obtained, and httpsession can be obtained by getsession. Servlet objects corresponding to hidden objects are not the main function of pagecontext. Its main function is to provide a single interface, to manage various public objects (such as httpsession, servletcontext, servletconfig, servletrequest, and servletresponse), a single API is provided to manage the attribute scope.
We have previously used the setattribute () method of the session to set attribute objects that can be shared by a process. The attributes set by the session can be shared in the same process, except for sessions, you can also use methods such as setattribute () for pagecontext, request, and application (For details, refer to the API file) to set attribute objects that can be shared, however, the attributes set by these four objects have different sharing ranges.
The property objects set by pagecontext are shared within the same JSP page and the property objects set by request are used, it can be shared during the same request processing (including forward to other JSP pages), and the property objects set by the session object can be shared only during the same process, the attributes set by the Application object can be shared on the JSP page of the entire web application.

CodeExample:

<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Taglib prefix = "C" uri = "http://java.sun.com/jsp/jstl/core" %>

<HTML>
<Head>
<Title> pagecontext. jsp </title>
</Head>
<Body>

<H2> El implicit object pagecontext </H2>

$ {Pagecontext. Request. querystring }:< C: Out value = "$ {pagecontext. Request. querystring}"/> </BR>
$ {Pagecontext. Request. requesturl }:< C: Out value = "$ {pagecontext. Request. requesturl}"/> </BR>
$ {Pagecontext. Request. contextpath }:< C: Out value = "$ {pagecontext. Request. contextpath}"/> </BR>
$ {Pagecontext. Request. Method }:< C: Out value = "$ {pagecontext. Request. Method}"/> </BR>
$ {Pagecontext. Request. Protocol}: <C: Out value = "$ {pagecontext. Request. Protocol}"/> </BR>
$ {Pagecontext. Request. remoteuser }:< C: Out value = "$ {pagecontext. Request. remoteuser}"/> </BR>
$ {Pagecontext. Request. remoteaddr }:< C: Out value = "$ {pagecontext. Request. remoteaddr}"/> </BR>
$ {Pagecontext. session. New }:< C: Out value = "$ {pagecontext. session. New}"/> </BR>
$ {Pagecontext. session. ID }:< C: Out value = "$ {pagecontext. session. ID}"/> </BR>

</Body>
</Html>

 

 

 

 

Then we analyze the Path Problem in JSP:

 

Whether to use absolute paths or relative paths in JSP is more and more complex with the technology adopted, and this problem becomes increasingly difficult to solve.

1) problems with using relative paths

L The relative path is flexible, but it is difficult to copy the code in the page because different pages have different relative paths. After copying, you must modify the path of each connection.

L if a page is contained by more than one page, the relative path in the contained page is incorrect.

L if the struts action is used to return the page, the browser cannot correctly interpret the path in the page because the page path is different from the action path, for example, the page is/pages/Cust. JSP. The image directory is/images/title.gif. the path used in JSP is ".. /.. /images/title.gif ", but if the forward of an action points to this JSP file, the action path is/Cust/manage. do, then the page content is ".. /.. "/images/title.gif" will no longer point to the correct path.

It seems that only the absolute path is used to solve the above problem.

2) problems with absolute paths

L the absolute path value varies with different web application publishing methods. If the web application is published as MyApp, the path "/MyApp/images/title.gif" is correct, but the path is incorrect when the web application is published as myapp2, this may be a small case, but the absolute path above is also different when the web application is released in the default mode: "/images/title.gif ".

 

 

 

Solution:

 

 

1) use an absolute path. To solve the differences between different deployment methods, add $ {pagecontext. Request. contextpath} before all paths with non-Struts labels. For example, the original path is:

"/Images/title.gif", changed

"$ {Pagecontext. Request. contextpath}/images/title.gif ".

The Code "$ {pagecontext. Request. contextpath}" is used to retrieve the deployed application.ProgramNo matter how deployed, the path used is correct.

Disadvantages:

 

 

The operation is inconvenient. Other tools cannot correctly explain $ {pagecontext. Request. contextpath}

2) use a relative path to add a base tag to each JSP file, such:

<Base href = "http: // $ {header ['host'] }$ {pagecontext. Request. contextpath}/pages/Cust/relation. jsp"/>

In this way, all paths can use relative paths.

Disadvantages:

 

 

The file to be included is still invalid.

For real use, flexible application is required. 1) and 2) write more robust code.

 

 

PS:

 

 

Note that if the above $ {pagecontext. Request. contextpath} expression is used as the response, make sure that your web. xml header is like this.

<? XML version = "1.0" encoding = "GBK"?>

<Web-app xmlns = "http://java.sun.com/xml/ns/j2ee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemalocation = "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
Version = "2.4">

 

Make sure the schema is 2.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.