JSP and Servlet path problems

Source: Internet
Author: User

the path problem in Web development

2012-01-23 01:43:54|   categories: JAVA |   Tags: Span id= "$_blogtaginfo" class= "fc03" >jsp  web  servlet   path   javabean    | Report | Font size Big small   subscription

Path problem in the Web development is a comparison of the problem of egg pain, especially with the relative address, the same code, in different directory structure will appear to have the wrong result, people are bewildered. This kind of error, the reason, or the concept of the path in the Web smattering, in order to completely avoid this error, improve the development efficiency, the web is a little summary of the path problem.

1. Server-side relative address

is resolved on the server side, is relative to the Web application address servlet relative address should be relative to the Web application, that is, relative to the http://localhost:8080/webapp/

2. REDIRECT Path problem

Forward Jump: Request.getrequestdispatcher (Address) in the servlet, this address is parsed on the server side, so forward to a.jsp should be written like this: Request.getrequestdispatcher ("/user/a.jsp") this/relative to the current Web application WebApp, its absolute address is: HTTP//localhost:8080/webapp/user/ a.jsp

Sendredirect Jump: Response.sendredirect (address); This address is parsed on the client, to jump to a.jsp, it should be written like this: Response.sendredirect ("/webapp   /user/a.jsp "); 3. Relative address of the client

All of the relative addresses in the HTML are relative to http://localhost:8080, rather than/HTTP/-localhost:8080/webapp/. The address of the Action property of the form form in HTML should be relative to http://localhost:8080.

So, if submitted to a.jsp as: action= "/webapp/user/a.jsp"; commit to Servlet as action= "/webapp/handleservlet"
JavaScript is also parsed on the client side, so its relative path is the same as the form form.

4. The path of the two relative paths relative to the root directory:

/jsp/path/pathtest.jsp on the server side resolves to an absolute path: the absolute path http://localhost:8080/webapp/jsp/path/pathTest.jsp resolves to on the client is: Http://lo calhost:8080/jsp/path/pathtest.jsp

Path relative to current directory:

Jsp/path/pathtest.jsp the path to the current directory is Http://localhost:8080/webapp/jsp/path,

Then the absolute path is: http://localhost:8080/webapp/jsp/path/jsp/path/pathTest.jsp

5. Site root and CSS path issues

When a CSS is introduced into the JSP, the corresponding CSS will be found correctly relative to the current JSP file, but if the current relative path is the relative path of which servlet is forward from a servlet that is not the same as the path of the current JSP, Instead of the path where the JSP is located, you cannot use such a path in the JSP: <link href= "Css.css" rel= "stylesheet" type= "Text/css" > or <link href= ". /.. /css.css "rel=" stylesheet "type=" Text/css ">.

In this case, to use the site root to refer to the CSS file, is relative to the http://ip/directory, with "/" beginning, that is, href= "/webapp/css.css". This way, after the servlet is forwarded and the JSP is relative to the site root's relative path, the defined CSS style can be used correctly.

It can also be understood that when referencing a file in the client (jsp/html file), it starts with/wabapp/*** because the client's relative address is http://localhost:8080/

6. Comparison of several acquisition path methods in a servlet

For Url:http://localhost:8080/webapp/blog/list

What paths are returned by calling Request.getcontextpath (), Request.getservletpath (), Request.geturi (), and Request.geturl () respectively?

These methods are defined in the HttpServletRequest interface.

1). Request.getcontextpath () returns the path to the Web application, in which case the method returns/webapp;

2). Request.getservletpath () returns the path of the servlet. In the above example, the method will return/blog/list;

3). Request.getrequesturl () returns the requested URL, which is http://localhost:8080/webapp/blog/list in the above example;

4). Request.getrequesturi () returns the portion of the request URI in the HTTP request line. In the example above, the method returns/webapp/blog/list.

* Note: The server port number for the case is 8080,webapp is the Web project name

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.