Today, I organized the project process and jumped between JSP and Servlet. For a while, I defined the Servlet Path as "/someservlet", that is, the root directory, because the compatibility is good, but after myeclipse is used, the default path of the newly created servlet is "/servlet/someservlet", which is easy to manage. In addition, it is more suitable to set the filter for the servlet separately. My JSP file is currently placed in the root directory of the project, that is, the path structure is formed:
/Projectroot/
| -- Servlet/
| -- Servlet1
| -- Servlet2
|
| -- Myjsp1.jsp
| -- Myjsp2.jsp
Servlet redirection can be implemented in two ways:
1. sendredirect () Mode
Response. sendredirect (string TargetUrl );
2. requestdispather Method
Requestdispatcher = request. getrequestdispatcher (string TargetUrl );
Requestdispatcher. Forward (request, response );
The first method is to send a notification to the user's browser, and then the browser sends a jump request to the server, so it is similar to the user's own jump to the URL, this method if you need to pass parameters to the jump page, you need to use session or get to explicitly write parameters in TargetUrl (for example, ooxx. JSP? Id = 1). In most cases, due to the limitations of the get method, this jump method can only contain relatively simple parameters.
The second method is similar to the server in C. the transfer () method, that is, server-side jump, is a change in the user's browser content, but the address bar of the browser remains unchanged. In this way, the server directly controls the request and response direction and parameters, which can be seen from the command line parameters. In this way, programmers can easily control the transmission of parameters, and almost any type of parameters can be passed, as long as the setattribute () method is used simply:
Request. setattribute (string attriname, object attrivalue );
However, because it is a server jump, the address bar of the user's browser does not change. If the project path structure is shown in, then:
1. When redirecting from JSP to Servlet
Simply use the relative path "Serlvet/someservlet.
2. When you jump from servlet to another Servlet
Because the servlet is in the same path, you can directly write the relative path, such as "./someservlet" or "someservlet ".
3. When redirecting from servlet to JSP
Because the Servlet Path is "Servlet/someservlet", if you want to use the requestdispather method to redirect, when the JSP page is connected to parameters, the address in the address bar is used as the current directory to find the methods, JavaScript, CSS, and so on. So some people often encounter the Javascript error "ext undefined" because the JS file of ext cannot be found on the JSP page.
Solution: You can access the relative path ".. \ myjsp1.jsp "... \ Indicates a directory to be jumped up. Similarly, \... \ indicates two layers to be jumped up.
Sometimes you need to use an absolute path to tell JSP where to get these resources. Java has many methods for obtaining paths. The test is as follows:
Project root directory: http: // localhost: 8080/testproject/
JSP test: http: // localhost: 8080/testproject/testpath. jsp
Testpath. jsp
<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8"
Pageencoding = "UTF-8" %>
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> insert title here </title>
</Head>
<Body>
<% = "Request. getcontextpath () ="
+ Request. getcontextpath () + "<br/>" %>
<% = "Request. getservletpath () ="
+ Request. getservletpath () + "<br/>" %>
<% = "Request. getrequesturi () ="
+ Request. getrequesturi () + "<br/>" %>
<% = "Request. getrequesturl () ="
+ Request. getrequesturl () + "<br/>" %>
<%
String realpath = session. getservletcontext (). getrealpath ("/");
%>
<% = "Request. getrealpath (\"/\ ") =" + realpath + "" %>
</Body>
</Html>
Returned results:
Request. getcontextpath () =/testproject
Request. getservletpath () =/testpath. jsp
Request. getrequesturi () =/testproject/testpath. jsp
Request. getrequesturl () = http: // localhost: 8080/testproject/testpath. jsp
Request. getrealpath ("/") = c: \ Tomcat \ webapps \ testproject \
Servlet Test
Testpath. Java
Package servlet;
Import java. Io. ioexception;
Import java. Io. printwriter;
Import javax. servlet. servletexception;
Import javax. servlet. http. httpservlet;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
Import javax. servlet. http. httpsession;
Public class testpath extends httpservlet {
Private Static final long serialversionuid = 3093721348408094325l;
Public void doget (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
Response. setcontenttype ("text/html ");
Printwriter out = response. getwriter ();
Out. println ("request. getcontextpath () =" + request. getcontextpath ()
+ "<Br/> ");
Out. println ("request. getservletpath () =" + request. getservletpath ()
+ "<Br/> ");
Out. println ("request. getrequesturi () =" + request. getrequesturi ()
+ "<Br/> ");
Out. println ("request. getrequesturl () =" + request. getrequesturl ()
+ "<Br/> ");
Httpsession session = request. getsession ();
String realpath = session. getservletcontext (). getrealpath ("/");
Out. println ("request. getrealpath (\"/\ ") =" + realpath + "");
Out. Flush ();
Out. Close ();
}
Public void dopost (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
Doget (request, response );
}
}
Returned results:
Request. getcontextpath () =/testproject
Request. getservletpath () =/servlet/testpath
Request. getrequesturi () =/testproject/servlet/testpath
Request. getrequesturl () = http: // localhost: 8080/testproject/servlet/testpath
Request. getrealpath ("/") = c: \ Tomcat \ webapps \ testproject \
This makes it easy to understand. In addition, the getrealpath () method is used to obtain the physical disk path of the URL. The previous method is simple: request. getrealpath (string path. However, this method has been deprecated. Use servletcontext. getrealpath (string path ). That is to say, the servletcontext object needs to be obtained first, and there are several ways to obtain this object. What is simpler is to get it from the session:
Httpsession session = request. getsession ();
String realpath = session. getservletcontext (). getrealpath ("/");
There are also several ways to obtain servletcontext:
Javax. servlet. http. httpsession. getservletcontext ()
Javax. servlet. jsp. pagecontext. getservletcontext ()
Javax. servlet. servletconfig. getservletcontext ()
Reference: http://blog.csdn.net/zhaojp0411/archive/2009/04/03/4045339.aspx
Difference between JSP jump mode and Servlet jump Mode Http://blog.sina.com.cn/s/blog_406127500100cb6n.html