1.JSP, relative path and absolute path in servlet
Premise: If your HTTP address is HTTP://192.168.0.1/your web app is test,path= "/test" then your Web app 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 server internal switch (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 apps:
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 app;
</servlet-mapping>
All relative paths are that start with "/". 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 <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 of the directory where a.html is located in user, that is, user is a.html in the same directory.
.. /ONE.CSS: Indicates that One.css is located in the A.HMTL level directory,
.. /.. /ONE.CSS: Indicates that One.css is located at the top level of the A.HMTL directory,
./: Indicates and A.HMTL the same directory
We call this relative path the HTML relative path
1.Server-side Address
The server-side relative address refers to the address that is relative to your Web application, which is resolved on the server side (different from the relative addresses in HTML and JavaScript, which are parsed by the client browser. This means that the relative addresses in the JSP and servlet should be relative to your Web application, that is, relative to http://192.168.0.1/test/.
Where they are used are:
Forward:servlet in Request.getrequestdispatcher (address); This address is parsed on the server side, so you 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 ("/rtccp/user/a.jsp") in the JSP;%>
2.The address of the client
All the relative addresses in the HTML are relative to the HTTP://192.168.0.1/, not the http://192.168.0.1/test/.
The address of the Action property of the form form in HTML should be relative to HTTP://192.168.0.1/, so if committed to user/a.jsp: action= "/test/user/a.jsp" ; submitted to servlet for action= "/test/handleservlet"
JavaScript is also parsed on the client side, so its 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 a change, referring to the general use of the site root directory relative path)
We call a relative path like this/test/.... Relative path relative to the site root directory.
When a CSS is introduced into a JSP, the CSS style does not work at all if its relative path is forward to the current JSP file, and in a servlet that is not the same as the path to the JSP. This is because the path to the CSS when it is forwarded in the servlet is relative to the servlet's relative path, not the JSP path. So this is not the time to use this path 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 ... /.. /ONE.CSS's HTML relative path is relative to the file (a.jsp) that references this CSS. While forwarding in the servlet is relative to the servlet's relative path, because the JSP path and the servlet path are not the same, so such a reference must be wrong.
So this time, to use the site root directory, is relative to the HTTP://192.168.0.1/directory, with "/" start.
Therefore, the above error should be corrected to the relative directory of the site root that is similar to href= "/test/one.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.
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 only handled in server side and is transparent to client side. Because the Redirect has two transmissions, it is inefficient.
Range:
Because of the Request.setattribute (), the object that it carries is only within the request, so the Redirect mode causes the object that is carried by the request to be lost.
Instructions for use:
1. sendredirect
The servlet is the same as inside 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 both page 1 and Page 2, and the address bar does not change.
Use the content of Request.setattribute to 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 the content of page 2, the address bar does not change
Use the content of Request.setattribute to use it normally
Three scenarios for referencing js,css files with relative paths in 3.JSP
The most common situation in the first case
A Tomcat runs multiple projects, distinguishing them with engineering names.
Because my URL is: http://localhost/ project name /home/index.jsp
More than one project name, so add <%=request.getcontextpath ()%>
such as: <script src= "<%=request.getcontextpath ()%>/home/test.js" ></script>
Write <%=request.getcontextpath ()%> too cumbersome, you can add the following 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 a real project, a Tomcat would run multiple projects and use IP to differentiate
The URL is this: http://localhost/home/index.jsp Note here is the direct access to the JSP file (JSP file is not under Web-inf, can be accessed directly), not the servlet is not struts.
Test2.js and index.jsp in the same folder, the following with the relative path to introduce JS file is OK:
<script src=test2.js></script>
Index.jsp can find test2.js file
Third case: servlet forwarding to JSP (JSP under Web-inf, must be accessed with servlet or action)
On the basis of the second case, a Tomcat also runs multiple projects and uses 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 the 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, access to the servlet or struts is the most action, so use <script src=/home/test2.js></script>
Relative paths and absolute paths in JSPs, Servlets