Servlet several objects in:
HttpServletRequest , HttpSession , ServletContext , these three objects have
void setattribute (String name, Object o) and void removeattribute (String name)
These two methods, through these two methods we can be very convenient for the transfer of parameters.
The size of the scope of the three objects is small to large, respectively. When we put an object throughvoidsetattribute (String name, Object o)when added to these three objects, unless we display the callvoidremoveattribute (String name)or when these three objects are destroyed by themselves, they will not release the space occupied by these objects, so we are passing an objectsetattributewhen you add to these three objects, you need to be aware that when you're doneRemoveoff(of course, after the use of this added to the object itself is quickly destroyed, we do not need to superfluous). And as much as possible when objects are stored in httpservletrequest, then HttpSession is the last servletcontext. Try not to keep large objects in these objects because the resources that a process consumes are limited.
Servlet problem with path jump in:
let's start with an example to see HttpServletRequest gets the return value of some methods of the fetch path.
Servletpath:
Package Squall.servlet.path;
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;
Public class Servletpath extends HttpServlet
... {
protected voiddoget (httpservletrequest req, HttpServletResponse resp)
throwsServletexception, IOException
...{
Showparam (REQ,RESP);
}
protected voidDoPost (httpservletrequest req, HttpServletResponse resp)
throwsServletexception, IOException
...{
Showparam (REQ,RESP);
}
Private voidShowparam (httpservletrequest req, HttpServletResponse resp)throwsIOException
...{
Resp.setcontenttype ("text/html");
PrintWriter out=Resp.getwriter ();
String DocType= "<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >";
Out.println (DocType+ "<HTML>" + "<HEAD><TITLE> pathshow"
+ "</TITLE></HEAD>" + "<body bgcolor= "#FDF5E6" >"
+"Getcontextpath ():" +Req.getcontextpath ()+ "</br>"
+"getpathinfo ():" +Req.getpathinfo ()+ "</br>"
+"Getrequesturi ():" +Req.getrequesturi ()+ "</br>"
+"Getrequesturl ():" +Req.getrequesturl ()+ "</br>"
+"Getservletpath ():" +Req.getservletpath ()+ "</br>"
+"getquerystring ():" +req.getquerystring ()+ "</br>"
);
Out.println ("</body>");
}
}
put this Servlet after deployment, Mapping to <url-pattern>/ServletPath/*</url-pattern>
in the browser input http://127.0.0.1:8080/train/ServletPath/a/b/c?name=1&passord=2
You can see the following output:
Getcontextpath ():/train
getpathinfo ():/a/b/c
Getrequesturi ():/train/servletpath/a/b/c
Getrequesturl (): http://127.0.0.1:8080/train/ServletPath/a/b/c
Getservletpath ():/servletpath
getquerystring (): name=1&passord=2
We can see the return value of each method very clearly.
ahtml,servlet,jspaccess to a differentServletwhen you can pass the relative path(relative to current path), relative to the sameWebthe path under Application(like ours./train, throughRequest.getcontextpath ()Get)and Absolute Path(Directhttp://...).
we are generallyServletTo access the otherServletorJSPcan passServletin the outputHTMLelementFORMthe property valueACTIONto achieve. But sometimes we can use some more convenient methods.
the following simple introduction 3 a method:
RequestDispatcher of the
void forward (servletrequest request, servletresponse response) and
void include (ServletRequest request, servletresponse response) method.
And
httpresponse void sendredirect (String location) method.
We first introduce the HttpResponse void Sendredirect (String location)
this Side The method will request a url location web application path or direct Http
Beforesendredirect :
Package Squall.servlet.path;
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;
Public class Beforesendredirect extends HttpServlet
... {
protected voidDoget (httpservletrequest req, HttpServletResponse resp)
throwsServletexception, IOException
...{
Resp.setcontenttype ("text/html");
PrintWriter out=Resp.getwriter ();
String DocType= "<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >";
Req.setattribute ("Canyousee", " This");
Out.println (DocType+ "<HTML>" + "<HEAD><TITLE> Beforesendredirect"
+ "</TITLE></HEAD>" + "<body bgcolor= "#FDF5E6" >"
+""
);
Out.println ("</body>");
Resp.sendredirect ("Aftersendredirect");
System.err.println ("Canyousee" +Req.getattribute ("Canyousee") + "In beforesendredirect");
System.err.println ("End in beforesendredirect");
}
protected voidDoPost (httpservletrequest req, HttpServletResponse resp)
throwsServletexception, IOException
...{
Doget (REQ,RESP);
}
}
Aftersendredirect :
Package Squall.servlet.path;
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;
Public class Aftersendredirect extends HttpServlet
... {
protected voidDoget (httpservletrequest req, HttpServletResponse resp)
throwsServletexception, IOException
...{
System.err.println ("begin aftersendredirect!");
Resp.setcontenttype ("text/html");
PrintWriter out=Resp.getwriter ();
String DocType= "<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >";
Out.println (DocType+ "<HTML>" + "<HEAD><TITLE> Aftersendredirect"
+ "</TITLE></HEAD>" + "<body bgcolor= "#FDF5E6" >"
+ "");
Out.println ("</body>");
System.err.println ("Canyousee" +Req.getattribute ("Canyousee") + "In aftersendredirect");
}
protected voidDoPost (httpservletrequest req, HttpServletResponse resp)
throwsServletexception, IOException
...{
Doget (req, resp);
}
}
will be Beforesendredirect Mapping to <url-pattern>/BeforeSendRedirect</url-pattern>
will be Beforesendredirect Mapping to <url-pattern>/AfterSendRedirect</url-pattern>
then we visit Http://127.0.0.1:8080/train/BeforeSendRedirect , you can see the page jump to http://127.0.0.1:8080/train/AfterSendRedirect, and the content displayed on the page is also
Aftersendredirect
and Tomcat the console output is:
Canyousee this in Beforesendredirect
End in Beforesendredirect
Begin Aftersendredirect
Canyousee NULL in Aftersendredirect
you can see that using HttpResponse of the void Sendredirect (String location) Method
1, html Location Span ' Comic Sans MS '; Mso-ascii-font-family: "Comic Mso-hansi-font-family:ms" "" "Page
2, url Location the absolute path of the
3, original servlet call void Sendredirect (String location)
4, Original servlet and jump to the servlet of the HttpServletRequest object is not the same object.
Here's the
RequestDispatcher of the
void forward (servletrequest request, servletresponse response) and
Description of the void include (ServletRequest request, servletresponse response) method.
before that, let's take a look at The method of obtaining the Javax.servlet.RequestDispatcher object.
The method to obtain this object exists in the3class or interface, respectively, isServletContext,ServletRequestandServletrequestwrapper. Here we only seeServletContextandServletRequest.
in theServletContextandServletRequestGet inRequestDispatcherall through the way .requestdispatcher getrequestdispatcher (String path), but be aware that this method has some differences when invoked with different classes, using theServletContextinterface, when the object invocation of theString Pathparameter must be in the"/"beginning, that is to say, from thisWebthe root of the project begins. and useServletRequestobject of the interface to invoke this method, thePathcan not take"/"to begin with, it simply means that you can use a relative path compared to the current request.
here we go . RequestDispatcher of the void Forward (ServletRequest request, servletresponse response) method, as is customary, we still use examples to illustrate the problem.
Beforeforward :
Package Squall.servlet.path;
Import java.io.IOException;
Import Java.io.PrintWriter;
Import Javax.servlet.RequestDispatcher;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Public class Beforeforward extends HttpServlet
... {
protected voidDoget (httpservletrequest req, HttpServletResponse resp)
throwsServletexception, IOException
...{
Resp.setcontenttype ("text/html");
PrintWriter out=Resp.getwriter ();
String DocType= "<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >";
Req.setattribute ("Canyousee", " This");
Out.println (DocType+ "<HTML>" + "<HEAD><TITLE> Beforeforward"
+ "</TITLE></HEAD>" + "<body bgcolor= "#FDF5E6" >"
+ "");
Out.println ("</body>");
RequestDispatcher Rd= This. Getservletcontext (). Getrequestdispatcher ("/afterforward");
System.err.println ("Canyousee" +Req.getattribute ("Canyousee")
+ "In Beforeforward");
Rd.forward (req, resp);
System.err.println ("End in beforeforward");
}
protected voidDoPost (httpservletrequest req, HttpServletResponse resp)
throwsServletexception, IOException
...{
Doget (req, resp);
}
}
Afterforward :
Package Squall.servlet.path;
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;
Public class Afterforward extends HttpServlet
... {
protected voiddoget (httpservletrequest req, HttpServletResponse resp)
throwsServletexception, IOException
...{
System.err.println ("begin afterforward!");
Resp.setcontenttype ("text/html");
PrintWriter out=Resp.getwriter ();
String DocType= "<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >";
Out.println (DocType+ "<HTML>" + "<HEAD><TITLE> Afterforward"
+ "</TITLE></HEAD>" + "<body bgcolor= "#FDF5E6" >"
+ "");
Out.println ("</body>");
System.err.println ("Canyousee" +Req.getattribute ("Canyousee")
+ "In Afterforward");
}
protected voidDoPost (httpservletrequest req, HttpServletResponse resp)
throwsServletexception, IOException
...{
Doget (req, resp);
}
}
Beforeforward and Afterforward separate Mapping to <url-pattern>/BeforeForward</url-pattern> and <url-pattern>/AfterForward</url-pattern>
You can see the console output as follows:
Canyousee this in Beforeforward
Begin afterforward!
Canyousee this in Afterforward
End in Beforeforward
Displayed on the browser is:
Afterforward
on the browser URL for Http://127.0.0.1:8080/train/BeforeForward
From the results we can see
1, forward will show forward after the content of the page
2, forward after the browser
3, forward after the page and the original page Request to the same object
below is requestdispatcher include (ServletRequest request, servletresponse response example, in this example, we use request RequestDispatcher object , so you can use a relative path.
Beforeinclude :
Package Squall.servlet.path;
Import java.io.IOException;
Import Java.io.PrintWriter;
Import Javax.servlet.RequestDispatcher;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Public class Beforeinclude extends HttpServlet
... {
protected voidDoget (httpservletrequest req, HttpServletResponse resp)
throwsServletexception, IOException
...{
Resp.setcontenttype ("text/html");
PrintWriter out=Resp.getwriter ();
String DocType= "<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >";
Req.setattribute ("Canyousee", " This");
Out.println (DocType+ "<HTML>" + "<HEAD><TITLE> beforeinclude"
+ "</TITLE></HEAD>" + "<body bgcolor= "#FDF5E6" >"
+ "");
RequestDispatcher Rd=Req.getrequestdispatcher (
"Ininclude");
System.err.println ("Canyousee" +Req.getattribute ("Canyousee")
+ "In beforeinclude");
Rd.include (req, resp);
Out.println ("");
Out.println ("</body>");
System.err.println ("End in beforeinclude");
}
protected voidDoPost (httpservletrequest req, HttpServletResponse resp)
throwsServletexception, IOException
...{
Doget (req, resp);
}
}
Ininclude :
Package Squall.servlet.path;
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;
Public class Ininclude extends HttpServlet
... {
protected voiddoget (httpservletrequest req, HttpServletResponse resp)
throwsServletexception, IOException
...{
System.err.println ("In ininclude!");
PrintWriter out=Resp.getwriter ();
Out.println ("");
System.err.println ("Canyousee" +Req.getattribute ("Canyousee")
+ "In ininclude");
}
protected voidDoPost (httpservletrequest req, HttpServletResponse resp)
throwsServletexception, IOException
...{
Doget (req, resp);
}
}
Beforeinclude and Ininclude separate Mapping to <url-pattern>/path/BeforeInclude</url-pattern> and <url-pattern>/path/InInclude</url-pattern>
Access http://127.0.0.1:8080/train/path/BeforeInclude
You can see the browser display as:
Beforeinclude
In InClude
Afterinclude
And the console output is:
Canyousee this in Beforeinclude
In ininclude!
Canyousee this in Ininclude
End in Beforeinclude
Note that in the example above , the Ininclude Unable to output a complete HTML page.
As you can see from the results:
1, include Include include output content
2, forward after the browser
3, forward after the page and the original page Request to the same object
I'd like to emphasize Ininclude Unable to output a complete HTML page, it just outputs part of the thing.
In this case, basically , servlet The forwarding and jumping of the request in is finished.