Servlet and JSP paths

Source: Internet
Author: User

The absolute path and Relative Path Problems in Servlet and JSP have plagued me for several days. After some efforts, I will share some of my experiences with you.

Premise: assume that your Http address is http: // 192.168.0.1/your web application is webapp, then your web application URL is http: // 192.168.0.1/webapp/
◆ Directory structure of web applications:
◆ Webapp/
◆ Web-inf/
◆ Classes/
◆ Lib/

Web. xml

 
 
  1. <Servlet-mapping> 
  2. <Servlet-name>Handleservlet</Servlet-name> 
  3. <Url-pattern>/Handleservlet</Url-pattern>This ing is relative to the current web Application
  4. </Servlet-mapping> 

◆ User/
A. JSP
B. JSP
◆ Images/
◆ Css/
◆ Js/

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: Your one.css and a. hmtl are in the same directory as user/one.css: Your one.css, where the sub-directory of 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.

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 is to say, the relative address in Servlet and JSP should be relative to your web application, that is, relative to http: // 192.168.0.1/webapp.

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: // 192.168.0.1/webapp/user/. JSP.
SendRedirect: in JSP

2. Client address

All relative URLs in html are relative to http: // 192.168.0.1/, rather than http: // 192.168.0.1/webapp.

The address of the form action attribute in Html should be relative to http: // 192.168.0.1/. 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
When the JSP forwarder in the Servlet with different paths is used, 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 similar to href = "one.css" and ...... /...... The html relative path of/one.css is relative to the file that references this css. When forwarding in Servlet, It is the relative path relative to this Servlet. Because the Servlet and JSP paths are different, such a reference must be wrong.

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

Therefore, the above error should be corrected to the relative directory of the site root directory similar to "/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.

  1. Rebuild Struts2 JSP pagination
  2. Three methods for displaying Chinese Characters in JSP Web programs
  3. Java JSP uses Google's Translate to develop APIs
  4. Detailed analysis of nine implicit JSP objects
  5. Introduction to JSP JavaBean development and release Environments

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.