Page jump in JSP and Servlet relative paths and absolute paths

Source: Internet
Author: User

1. relative and absolute paths in JSP and Servlet

Premise: assume that your Http address is http: // 192.168.0.1/your web application is test, path = "/test", then your web application URL is http: // 192.168.0.1/test/


Directory structure of web applications:

Test/web/

Css/
Js/

Test. js

Web-inf/
Classes/
Lib/
User/
A. jsp
B. jsp
Images/
Web. xml
<Servlet-mapping>
<Servlet-name> handleservlet </servlet-name>
<Url-pattern>/handleservlet </url-pattern> This ing is relative to the current web Application
</Servlet-mapping>
. 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 <link href = "one.css" rel = "stylesheet" type = "text/css">, 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: Your one.css is in.
../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 above relative path


1,Server address
The relative address of the server is relative to the address of your web application. (different from) That is to say, the relative address in jsp and servlet is relative to your web application, that is, relative to http: // 192.168.0.1/test.
Where it is used:
Forward: request in servlet. getRequestDispatcher (address); this address is resolved on the server side. Therefore, You Need To forwarder to user/. jsp should write this: request. the absolute address of getRequestDispatcher ("")/relative to the current web application test is:
Redirect: in jsp, <% response. sendRedirect ("/rtccp/user/a. jsp"); %>

2,Client address
All relative URLs in html are.
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 user/. jsp: action = "user/. jsp "; submit to servlet as action ="/test/handleservlet"


3,Site root directory and css Path Problems
We call a relative path like this as relative.
When css is introduced in jsp, if its relative path is relative to the current jsp file, and the jsp is forward 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, the following path cannot be used in jsp: <link href = "one.css" rel = "stylesheet" type = "text/css"> or <link href = ".. /.. /one.css "rel =" stylesheet "type =" text/css "> similar to href =" one.css "and .. /.. the relative html path of/one.css is relative to the file that references this css. In servlet forwarding, This is the relative path relative to this servlet, because 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 as the relative directory of the similar site root directory. In this way, after servlet forwarding and jsp, the defined css style can be correctly used.

 

Page Jump problem:

 

High, low, because the process is like this, request1 sent to server, server return back to client, and then

Request2 then sent to server. But it is only processed on server side and transparent to client side. Because there are two transfers, the efficiency is low.

Range:
For request. setAttribute (), the object contained in the request is only within the request range. Therefore, the object carried in the request is lost.

 

Instructions for use:

1.SendRedirect

The servlet is the same as the jsp.

Response. sendRedirect ();

 

2.Include is also the forward form mentioned above,

1) servlet

Request. getRequestDispatcher ("jsp2.jsp"). include (request, response );

2) inside jsp

<Jsp: include page = "include. jsp"/>

Description
The page contains the content of page 1 and page 2 at the same time, and the address bar remains unchanged.
The content of request. setAttribute can be used normally.

 

3.Forword

1) servlet

Request. getRequestDispatcher ("jsp2.jsp"). forward (request, response );
2) inside jsp

<Jsp: forward page = "include. jsp"/>

Description
The page will be the content of page 2, and the address bar will not change
The content of request. setAttribute can be used normally.


3. Reference JS and CSS files with relative paths in JSP

First caseMost common cases
A single tomcat runs multiple projects, which are distinguished by engineering names.
Because my URL is: http: // localhost/Project name/Home/index. jsp
An additional project name is added, so <% = request. getContextPath () %>
For example, <script src = "request. getContextPath () %>/home/test. js"> </script>

Writing request. getContextPath () %> is too troublesome. You can add the following content to the top of each jsp file,

Java code
  1. <%
  2. String path = request. getContextPath ();
  3. String basePath = request. getScheme () ++ request. getServerName () ++ request. getServerPort () + path +;
  4. %>
  5. <Base href =>

/Home/test. js "> </script>

 

Second, directly access the jsp file

In a real project, a tomcat also runs multiple projects and uses IP addresses to differentiate them.
The url is as follows:Http: // localhost/home/index. jsp.
Put test2.js and index. jsp in the same folder. it is OK to introduce the JS file using the relative path below:
<Script src = test2.js> </script>

Index. jsp can be found in the test2.js File

 

Case 3: servlet forwarding to jsp (jsp must be accessed using servlet or action under web-inf)

On the basis of the second case, if multiple projects are run on a tomcat, IP addresses are used to differentiate them.

WeIs accessed or struts index. jsp
The url is:

The url is:

The JSP file is not accessed here.

 

The following is OK.
<Script src = test2.js> </script>

Before test2.js, you must have/home/

In actual projects, servlet or struts is the most accessed, so <script src =/home/test2.js> </script> is used here.

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.