In Javaweb development, as long as the URL address is written, it is advisable to start with "/", that is, the use of absolute path, then what does this "/" represent? You can remember "/" in the following way: if "/" is used for the server, it represents the current Web project, and if "/" is for the browser, it represents the WebApps directory.
1. "/" represents a common application scenario for the current Web project
①.servletcontext.getrealpath (String path) gets the absolute path of the resource
/** * 1.servletcontext.getrealpath ("/download/1.jpg") is used to obtain a resource on the server, * then this "/" is for the server, "/" represents the Web project at this time * Servletcontext.getrealpath ("/download/1.jpg") means reading 1 of the download folder under Web Engineering. JPG This resource * As long as you understand the specific meaning of the "/" representative, you can quickly write down the absolute path of the Web resource to be accessed */this. Getservletcontext (). Getrealpath ("/ Download/1.jpg ");
②. Forward to other pages on the server side
/** 2 * 2.forward3 */ this. Getservletcontext (). Getrequestdispatcher ("/index.jsp"). Forward (request, response);
③. Introducing pages using include directives or <jsp:include> tags
<% @include file= "/JSPFRAGMENTS/HEAD.JSPF"%><jsp:include page= "/jspfragments/demo.jsp"/>
At this point, "/" stands for Web engineering.
2.
"/"
represents the WebApps directoryCommon Application Scenarios for
①. Using Sendredirect to implement request redirection
Response.sendredirect ("/javaweb_httpservletresponse_study_20140615/index.jsp");
The server sends a URL to the browser, the browser gets the URL address, and then requests the server, so this "/" is for the browser, "/" represents the WebApps directory, "/javaweb_httpservletresponse_study_ 20140615/index.jsp "This address means" webapps\javaweb_httpservletresponse_study_20140615\index.jsp. "
Response.sendredirect ("/Project name/folder directory/page"); This kind of writing is to write the project name to die in the procedure, not flexible, in the event of the day when the name of the project changed, the program must be changed, so we recommend the following flexible wording:
Will
Response.sendredirect ("/javaweb_httpservletresponse_study_20140615/index.jsp");
This is changed into
Response.sendredirect (Request.getcontextpath () + "/index.jsp");
What Request.getcontextpath () gets is "/javaweb_httpservletresponse_study_20140615", which makes it more flexible, Use Request.getcontextpath () instead of "/project name", it is recommended to use this method, flexible and convenient!
②. using hyperlinks to jump
<a href= "/javaweb_httpservletresponse_study_20140615/index.jsp" > Jump to Home </a>
This is the hyperlink jump used by the client browser, this "/" is used for the browser, at this time "/" represents the WebApps directory.
Using hyperlinks to access Web resources, it is recommended to use the following syntax to improve the absolute path:
<a href= "${pagecontext.request.contextpath}/index.jsp" > Jump to Home </a>
This avoids the occurrence of the project name in the path, using ${pagecontext.request.contextpath} instead of the "/javaweb_httpservletresponse_study_20140615 "
③.form form Submission
1 <form action= "/javaweb_httpservletresponse_study_20140615/servlet/checkservlet" method= "POST" > 2 <input type= "Submit" value= "Submission" >3 </form>
This is the client browser submits form form to the server, so this "/" is used for the browser, at this time "/" represents the WebApps directory.
It is also recommended to use the following ways to improve the absolute path of the action attribute in form submission:
1 <form action= "${pagecontext.request.contextpath}/servlet/checkservlet" method= "POST" >2 <input type= "Submit" value= "Submission" >3 </form>
${pagecontext.request.contextpath} got "/javaweb_httpservletresponse_study_20140615".
${pagecontext.request.contextpath} has the same effect as Request.getcontextpath (), both get "/project name"
④.js a reference to a script and CSS style file
1 <%--Reference JS script using absolute path--%>2 <script type= "Text/javascript" src= "${ Pagecontext.request.contextpath}/js/index.js "></script>3 <%--${pagecontext.request.contextpath } and Request.getcontextpath () The result is the same--%>4 <script type= "Text/javascript" src= "<%= Request.getcontextpath ()%>/js/login.js "></script>5 <%--using absolute paths to reference CSS styles--%>6 < Link rel= "stylesheet" href= "${pagecontext.request.contextpath}/css/index.css" type= "Text/css"/>
Comprehensive Example:
<%@ page language= "Java"Import= "java.util.*" pageencoding= "UTF-8"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >Reprint: Recommended wording for URL addresses in Web projects