Request.setattribute () How to use it?JSP1 code String [] test=new string[2];test[0]= "1"; test[1]= "2"; Request.setattribute ("Test", test); Response.sendredirect ("jsp2.jsp"); JSP2 Code String test[]= (string[]) request.getattribute ("test"); Out.print (test); However, test will not be found in the JSP2. Why? First, explain the reques cycle: setattribute is used to save variables in the same request cycle. For example, after the servlet call, and bring up the JSP page, this is a request cycle, if you want to servelet in the JSP page some processing structure, is obtained from the Request.getattribute. For the above Sendredirect () method is redirected through the browser, so the request on the second JSP page is not the request of the previous page (two requests generated before and after two different request objects)
Request.getattribute () and Request.setattribute ()Request.getattribute ("Nameofobj") can get the value of a control in a JSP page form. The name and value of object in the form control are stored in a hash table, so you can find the corresponding value based on the name of object and use Request.setattribute ("Nameofobj" for different page values). Valueofobj "), only one pass from Jsp1 to JSP2, after which the request loses its scope, and a request.setattribute () will be set again. The use of Session.setattribute () will always retain this value in a process, but the use of the session to meet other specifications, the specific requirements of Google.
try the Forward () method with the RequestDispatcher interfaceList List = new Commonsdao (). Getallcommons (); Request.setattribute ("Commonslist", list); Request.getrequestdispatcher ("view.jsp"). Forward (request, response);
Request.getrequestdispatcher () and Response.sendredirect () differences request.getrequestdispatcher () is a request forward, and the front and back pages share a request response.sendredirect () is redirected, and the front and back pages are not a requst Sendredirect notifies the browser to resubmit an HTTP request dispatcher is to go directly to the destination address, the most direct performance is dispatcher after the URL address is changed request.getrequestdispatcher () is server-side jump Response.sendredirect () is client jump My understanding is: sendredirect () request Redirect, Similar to hyperlinks. For example, write a request.setattribute,sendredirect to B page on page a: server------Response---->A page------Request---- >b page The real situation is: a page------Request----> server (sendredirect operation)------response---- >a page (then browser)------request------> Server------Response-------> B page , &NB Sp &nbs P includes two request and response, so it is not possible to take the B page to the Request.setattribute value, can see the change from the URL Request.getrequestdispatcher (). Forwar (Request,reponse) is a request dispatcher, such as you write a reuqest.setattribute on a page, Request.getrequestdispatcher (). Forwar (request,reponse) to page B, that is, the server gives you the response is the B page, and only once request and response, So only in the B page to get the value of Request.setattribute, the URL is still a page.
Summary:
Sendredirect implements request redirection, forward implements request forwarding. 1. Jump mode Use the forward method to redirect only one resource in the same Web application. The Sendredirect method allows you to redirect to any URL. form action= "/uu", Sendredirect ("/uu"), relative to the server root path. like Http://localhost:8080/Test application (then submit to Http://localhost:8080/uu); forward code "/uu" Represents the path relative to the Web App. such as Http://localhost:8080/Test application (then submit to Http://localhost:8080/Test/uu); 2. Parameter passing forward redirection, The browser URL address does not change, after Sendredirect forwarding, the browser URL address becomes the destination URL address. forward redirection passes the original HTTP request object (request) from one servlet instance to another, and the Sendredirect way is not the same application. forward the form parameter is passed along, you can use setattribute to pass the parameter to the next page. Sendredirect can only pass parameters via links, response.sendredirect ("Login.jsp?param1=a"). 3. Server-side and browser-side using forward redirection is the process by which the browser sends the request request to the destination servlet first, and then sends the requests to the destination URL at the server driven by servlet. The server-side servlet then returns response to the browser side. The browser and the server request a response at a time. using the Sendredirect forwarding process, the browser sends a request to the destination servlet first, and the servlet sees that Sendredirect returns the destination URL to the browser, The browser then goes to request the destination URL, and the destination URL returns response to the browser. Browser and server two request response. 4Request and response are shared between the caller of the. Request and the Response forward method and the callee The sendredirect method has two requests and response because of a two-time browser server request. If you use Request.setattribute to pass some properties you need to use forward, if you want to jump to another application's resources, you need to use Sendredirect. 5. Cannot have Printerwriter output to client either the forward method or the Sendredirect method
Call Previous There is no PrintWriter output to the client. forward method error: Java.lang.IllegalStateException:Cannot forward after response have been committed sendredirect error: Java.lang.IllegalStateException at org.apache.catalina.connector.responsefacade.sendredirect (ResponseFacade.java : 435) Two pictures simple direct: forward:sendredirect: ps: Other people's views on forward today, when debugging a servlet program, the following error was reported! Java.lang.IllegalStateException:Cannot forward after response have been committed according to the literal understanding of the words, consciousness is in the response has been submitted after the program can not jump again! The code was discovered later because the previous request.request.getRequestDispatcher () was executed once. Forward () But the back of Request.request.getRequestDispatcher (). Forward () is still executed! Then there is reason to believe that Request.request.getRequestDispatcher (). Forward () The jump itself is not going to return anything, nor will it terminate the execution of the program body! The execution will be executed after the program body! Reference: https://my.oschina.net/abbchina/blog/649274http://blog.csdn.net/yoxibaga/article/details/8624056
Java Learning--forward and Sendredirect differences Summary