HttpServletRequest Object Explanation
Javax.servlet.http.httpservletrequest Yes sun developed servlet specification, is an interface. Represents the request , "http request Agreement "are encapsulated into the request Object, httpservletrequest interface is Javax.servlet.servletrequest
The complete class name for the Javax.servlet.http.HttpServletRequest interface implemented by the Apache Software Foundation's "Tomcat container" is Org.apache.catalina.connector.RequestFacade, but we javaweb programmers do not need to care about the specific Request object type, do not need to care about which container, we only need to face The HttpServletRequest interface calls the method.
One request corresponds to a Request object, the request begins, the request object is created, the request ends, the request object is reclaimed, and the next request is a new request object. How to understand a request: the browser sends a request to the server to end the server response as a single request.
common methods in HttpServletRequest interfaces
by user-submitted parameters name get parameter value( the most common method)
String value = Request.getparameter (string name);
to storing data (bindings) in a HttpServletRequest object
Request.setattribute (String name,object obj);
from fetching data in HttpServletRequest objects (reading data)
Object obj = request.getattribute (String name);
removed from httpservletrequest data in an object
Request.removeattribute (String name);
Complete forwarding using the Request object (forwarding is a request, one request spans multiple servlets)
Request.getrequestdispatcher ("Resource Path"). Forward (Request,response);
gets the client's IP Address
String ClientIP = Request.getremoteaddr ();
Get URI
String uri = Request.getrequesturi ();
Get URL
StringBuffer url = request.getrequesturl ();
Get Servlet Path
String Servletpath = Request.getservletpath ();
Get the application's root path, get the app context path
String ContextPath = Request.getcontextpath ();
Resolving garbled problems in the request body (set valid before fetching any data from the requests object)
Request.setcharacterencoding ("UTF-8");
gets all of the requests sent by the user on the server side Cookies
cookie[] cookies = request.getcookies ();
Get Session Object Session
HttpSession sessoin = Request.getsessoin (); Gets the session object and opens a new session if it is not available
HttpSession Sessoin = Request.getsession (true); Gets the session object and opens a new session if it is not available
HttpSession Sessoin = Request.getsession (false); Gets the session object, or returns null if not obtained
The HttpServletRequest object is a request-level object that requests one object at a time, so the request object can only be passed through the data in the same request completely, and can be passed across multiple servlets, but the forwarding mechanism must be used. If both the request object and the ServletContext object can perform this function, we prefer the request scope. Request cannot complete data passing across users. You can only complete passing data in a single request.
HttpServletResponse Object Explanation
Javax.servlet.ServletResponse is an interface, a Response object developed by SUN, that specifically responds to the browser.
Javax.servlet.http.HttpServletResponse Parent Interface is Servletresponse
To get the response stream: PrintWriter out = Response.getwriter ();
about the methods in PrintWriter:
1, println method: Output to the browser HTML Source Line
2, Print method: Output to the browser HTML source code does not wrap, If you want to do in the Web page line effect, you need to use the HTML identification <br> tags.
3 . Send Cookie:response.addCookie (cookies) to a specific browser client;
redirect : Response.sendredirect ("/webcontextpath/servletpath");
About forwarding and redirection:
The difference between forwarding and redirection (resource jumps), as shown in
Implementation on the code:
Forward : Request.getrequestdispatcher ("/servletpath"). Forward (Request,response);
REDIRECT : Response.sendredirect ("/webcontextpath/servletpath");
Note Resource path: You do not need to write the root path name of the Web app in forwarding. However, when redirecting, you need to write the root path name of the Web app, assuming that the Web app's root path name is Studyservlet, the Web app has a resource under the root, and if it is forwarded, the path writes A/A, and if the redirect is/studyservlet/a
The Requestdispatcher.forward method can only forward requests to resources in the same Web application (this resource might be servlet, JSP, HTML, etc.). Instead, the Httpservletresponse.sendredirect method can redirect to other resources in the current application and redirect to resources in another web App
After the access process that calls the Httpservletresponse.sendredirect method redirects, the URL that is displayed in the browser address bar changes, the initial URL address becomes the redirected destination URL, and the call Requestdispatcher.forward The browser address bar keeps the initial URL address intact after the request-forwarding process for the method is completed.
The Httpservletresponse.sendredirect method responds directly to the browser's request, and the result of the response is to tell the browser to re-issue the access request to another URL, a process like a person nicknamed "the browser" who writes to find Zhang San to borrow money, Zhang San replied that there is no money, let "browser" to find John Doe borrow, and will John Doe now the address of the communication to the "browser." So, "browser" also press Zhang San to provide correspondence address to John Doe letter to borrow money, John Doe received the letter to the money to the "browser". As can be seen, the browser sent out two letters and received two replies, and the browser also knew that the money he borrowed was from John Doe's hand. Requestdispatcher.forward The method forwards the request to another resource on the server side, and the browser only knows that the request was made and the result of the response was not aware of the forwarding behavior within the server program. This process is like nicknamed "Browser" people write to find Zhang San borrow money, Zhang San no money, so Zhang San find John Doe borrowed some money, even can add some of their own money, and then remit the money to the "browser." As can be seen, the "browser" only sent a letter and received a reply, he only knew from Zhang San to borrow money, do not know that a part of the money from John Doe hand.
The caller of the Requestdispatcher.forward method shares the same request object and the response object as the callee, which belong to the same access request and response process, while the Httpservletresponse.sendredirect method caller and the The caller uses the respective request object and the response object, which belong to two separate access request and response procedures. For jumps between internal resources of the same Web application, especially before a jump, some pre-processing of the request And to pass the preprocessing results using the Httpservletrequest.setattribute method, you should use the Requestdispatcher.forward method. Redirection between different Web applications, especially if you are redirecting resources to another Web site, should use the Httpservletresponse.sendredirect method.
Forwarding and redirection cannot be followed by forwarding and redirection-related code, either forward or redirect. ( forwarding and redirection can only be one time in the same Servlet .) )
How do you choose to redirect or forward?
Forwarding is usually faster and can keep the object within the request, so it is the first choice. However, since the URL in the browser still points to the start page after forwarding, if the current page is overloaded, the start page will be called again. If you don't want to see such a situation, select redirect ( page refresh issue ).
REDIRECT: the variables stored in the previous request are all invalidated and enter a new request scope.
forwarding: the variables stored in the previous request are not invalidated, just like putting two of pages together.
1. If you are jumping between resources in a Web app, you must use redirection.
2, in order to solve the page refresh problem, you must use redirection.
3. If a data is stored in the servlet in the request object and you want to remove the data from the request object in the next servlet/jsp page, you must use forwarding, since the redirect is two requests, and request cannot pass the data across the request. Redirects can be used in all other situations.
HttpServletRequest and HttpServletResponse objects