JSP, relative path in servlet and absolute path page jump problem __jsp

Source: Internet
Author: User

1.JSP, relative and absolute paths in the servlet

Premise: Suppose your HTTP address is HTTP://192.168.0.1/your Web application for test,path= "/test" then your Web application URL is http://192.168.0.1/test/

If the Jsp,js file is not accessible at all in the Web-inf directory, JSP if placed in the Web-inf directory can be accessed through the internal server (mainly for the security of the page), but JS is through the client to the server request, so the picture and some JS, CSS can only be placed outside the Web-inf
directory structure for 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 mapping is relative to the current Web application
         </servlet-mapping>
  All relative paths are by "/ "The beginning. For example:/image/a.gif,/user/main.jsp, you know the relative path in HTML is this:

There is an HTML file: a.html, which has the <link href= "One.css" rel= "stylesheet" type= "Text/css", where the href attribute represents the path of the referenced CSS file.
ONE.CSS: Indicates that one.css and A.HMTL are in the same directory
USER/ONE.CSS: Indicates that ONE.CSS is in the subdirectory user of the directory where a.html resides, that is, the user is a.html in the same directory.
.. /ONE.CSS: Indicates that the ONE.CSS is located in the top level of A.HMTL directory,
.. /.. /ONE.CSS: Indicates that the ONE.CSS is located in the upper level of the directory at the A.HMTL level,
./: Represents and A.HMTL the same directory
We call this relative path the HTML relative path


1,Server-side Address
The relative address on the server side refers to the address of your Web application, which is resolved on the server side (unlike the relative addresses in HTML and JavaScript, which are parsed by the client browser). In other words, the relative address in the JSP and servlet should be relative to your Web application, that is, relative to the http://192.168.0.1/test/.
The places it uses are:
Forward:servlet in Request.getrequestdispatcher (address); This address is resolved on the server side, so you have to forwarder to user/ A.jsp should write this: Request.getrequestdispatcher ("/user/a.jsp") this/relative to the current Web application test, its absolute address is: http://192.168.0.1/test/user/a.jsp.
Redirect: <%response.sendredirect in JSP ("/rtccp/user/a.jsp");%>

2,The address of the client
The relative addresses in all HTML are relative to the HTTP://192.168.0.1/, not the http://192.168.0.1/test/.
The address of the form's action attribute in HTML should be relative to HTTP://192.168.0.1/, so if you submit to user/a.jsp as: action= "/test/user/a.jsp" ; submit to Servlet for action= "/test/handleservlet"
JavaScript is also parsed on the client side, so the relative path is the same as the form form.

3,Site root and CSS path problems (JSP is a server-side program, the address is changed, when referencing the general site root directory relative path)
We call such a relative path/test/.... is the relative path relative to the site root directory.
When a CSS is introduced into a JSP, if its relative path is the same as that of the current JSP file, forward the JSP in a servlet that is not the same as the path to the JSP, it will find that the CSS style does not work at all. This is because the path of the CSS when forwarding in the servlet is relative to the servlet's relative path rather than the JSP's path. So this is not the way to use this in a 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 ... /.. /ONE.CSS's HTML relative path is relative to the file (a.jsp) that references the CSS. Forwarding in the servlet is relative to the servlet's relative path, because the JSP path and servlet path are not the same, so the reference must be wrong.
So this time, to use the site root directory, is relative to the HTTP://192.168.0.1/directory, to "/" start.
Therefore, the above error should be corrected to the relative directory of the site root directory similar to the href= "/test/one.css". This makes it possible to use the defined CSS style correctly when the servlet is forwarded and the JSP is relative to the site root.

page Jump question:

Forward High, Redirect low, because the Redirect process is like this, Request1 sent to server, the server return to the client, and then

Request2 then sent to server. However, forward is handled only in server side and is transparent to client side. Because the redirect has two times transmission, so the efficiency is low.

Range:
Because of the Request.setattribute (), it carries the object survival scope only in the request, so the redirect way can cause the object that request carries to lose.

Instructions for use:

1. sendredirect

The servlet is the same as the JSP

Response.sendredirect ();

2. include this is also the forward form mentioned above, the value of request will be saved

1) inside the servlet

Request.getrequestdispatcher ("jsp2.jsp"). Include (request, response);

2) JSP inside

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

Description
The page contains the contents of page 1 and page 2, and the address bar is unchanged.
Use the content of Request.setattribute, you can use it normally

3. ForWord

1) inside the servlet

Request.getrequestdispatcher ("jsp2.jsp"). Forward (request, response);
2) JSP inside

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

Description
Page will be 2 of the content, the address bar does not change
Use the content of Request.setattribute, you can use it normally


3.JSP references to js,css files in relative paths in three cases

The most common situation in the first case
A Tomcat runs multiple projects, using engineering names to differentiate
Because my URL is: http://localhost/ Engineering name /home/index.jsp
One more project name, so add <%=request.getcontextpath ()%>
such as: <script src= "<%=request.getcontextpath ()%>/home/test.js" ></script>

Write <%=request.getcontextpath ()%> too troublesome, you can add the following content at the top of each JSP file,

Java code <% String path = Request.getcontextpath ();    String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/"; %> <base href= "<%=basePath%>" >

You can directly use the <script src= "/home/test.js" ></script>

second, direct access to JSP files

In real projects, a tomcat can also run multiple projects, using IP to differentiate
The URL is this: http://localhost/home/index.jsp Note here, direct access to the JSP file (JSP file is not web-inf, you can directly access), not a servlet nor struts.
Test2.js and index.jsp placed in the same folder, the following relative path to introduce JS file is OK:
<script src=test2.js></script>

Index.jsp can find test2.js files

Third Scenario: servlet forwarding to JSP (JSP under Web-inf, must be accessed with servlet or action)

On the basis of the second case, a Tomcat can also run multiple projects, using IP to differentiate.

We are accessing the servlet or Struts action and forwarding it to index.jsp .
URL is: http://localhost/***.do

URL is: http://localhost/index.action

This is not an access JSP file.

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

There must be/home/in front of Test2.js.

In the actual project, you have the most action to access the servlet or struts, so use <script src=/home/test2.js></script> here.

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.