Path Problems in JSP and Servlet

Source: Internet
Author: User

Premise: assume that your HTTP address is http: // web application is webapp, then your web application URL is http: //

Directory structure of Web applications:





Web. xml


/Handleservlet this ing is relative to the current Web Application


A. jsp

B. jsp




All relative paths start. For example:/image/a.gif,/user/Main. jsp. We all know that the relative path in HTML is like this:

There is an HTML file: a.html, where the href attribute indicates the path of the referenced CSS file.

One.css: one.css and A. hmtl are in the same directory.

User/one.css: unzip one.css is used in the sub-directory user in the directory where a.html is located.

../One.css: one.css is located in the upper-level directory of A. hmtl,

.../../One.css: one.css is located in the upper-level directory of A. hmtl,

./: Indicates the same directory as A. hmtl.

We call the preceding relative path HTML relative path.

1. server address

The relative address on the server is relative to the address of your web application. This address is parsed on the server (different from the relative address in HTML and JavaScript, they are parsed by the client browser) That means the relative address in JSP and Servlet should be relative to your web application, that is, relative to http: //

Where it is used:

Forwarder: request in servlet. getrequestdispatcher (Address); this address is resolved on the server, so you need to forwarder to. JSP should write this: request. getrequestdispatcher ("/user/. JSP ") the absolute address of this/relative to the current web application webapp is: http: // JSP.

Sendredirect: in JSP

2. Client address

All relative URLs in HTML are relative to http: //, rather than http: //

The address of the Form Action attribute in HTML should be relative to http: // Therefore, if the URL is submitted to. JSP: Action = "/webapp/user/. JSP "; submit to Servlet as action ="/webapp/handleservlet"

Javascript is also parsed on the client, so its relative path is the same as the form.

3. Website root directory and CSS Path Problems

We call it a relative path like this/webapp /.... Is the relative path relative to the site root directory.

When CSS is introduced in JSP, if its relative path is relative to the current JSP file, and the JSP is forwarder in a servlet that is different from the JSP path, the CSS style does not work at all. This is because when forwarding in servlet, the CSS path is relative to the Servlet Path rather than the JSP path. Therefore, this path cannot be used in JSP at this time: or HTML relative paths like href = "one.css" and.../../one.css are relative paths relative to the files that reference this CSS. When forwarding in servlet, It is the relative path relative to this servlet. Because the JSP path is different from the Servlet Path, such a reference must be wrong.

Therefore, the site root directory must start with "/" relative to the http: //

Therefore, the above error should be corrected to the relative directory of the site root directory similar to href = "/webapp/one.css. In this way, after servlet forwarding and JSP are relative paths relative to the site root directory, the defined CSS style can be correctly used.

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: 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.