1. Accessing resources
use the Forward method to redirect only one resource in the same Web application. The Sendredirect method allows you to redirect to any URL. 2.request.getThe "/uu" in the forward code represents the relative path to the Web App. Example:package path for Servlet and destination files:
Servlet code: In DoPost and Doget code:
The request redirects the URL address or is relative to this item (WEB13) Http:localhost:8080/web13+url ;
The URL address is:/html/ok.html or
Html/ok.html
The URL that response requests to be forwarded starts with a "/" equivalent to the server http:localhost:8080+ URL url such as:/web13/html/no.html
And the beginning does not start with '/' (html/no.html) is the address to correspond to this item (WEB13) Http:localhost:8080/web13/+url
1 Public voiddoget (httpservletrequest request, httpservletresponse response)2 throwsservletexception, IOException {3Request.setcharacterencoding ("UTF-8");4Response.setcharacterencoding ("UTF-8");5 6 7DataSource datasource=Dbcputils.getdatasource ();8 System.out.println (dataSource);9Queryrunner qr=NewQueryrunner (dataSource);TenPrintWriter out=Response.getwriter (); One AString username=request.getparameter ("UserName"); -String password=request.getparameter ("Password"); - the System.out.println (userName); -String sql1= "SELECT * from User"; -String sql= "SELECT * from user where username=? and password=? "; - //Object [] Params={username,password}; + Try { -object[] u = qr.query (sql,NewArrayhandler (), Username,password); + A if(u!=NULL) { atSYSTEM.OUT.PRINTLN ("Query result is not empty"); -Request.getrequestdispatcher ("html/ok.html"). Forward (request, response); - //response.sendredirect ("/web13/html/ok.html"); -}Else { -SYSTEM.OUT.PRINTLN ("Query result is empty"); -Response.sendrediect ('/web13/html/no.html '); in } -}Catch(SQLException e) { to e.printstacktrace (); + } - } the * Public voidDoPost (httpservletrequest request, httpservletresponse response) $ throwsservletexception, IOException {Panax Notoginseng doget (request, response); -}
3. When a servlet requests a request for forwarding, the form jumps to the servlet page by submitting the method: Get, Post:
(using the RequestDispatcher interface Forward) method forward () cannot redirect to a JSP file with a frame, can be redirected to a frame HTML file,
At the same time forward () can not be passed with parameters at the back, such as Servlet?name=frank, so it is not possible to pass the program through Response.setattribute ("name", name) to the next page .
That is: when another page (which can be said to be the login page) jumps to the servlet using get Mode Request.getrequestdispatcher (). ForWord (Request.response); is not able to jump
4.servlet using request to forward to another page in Chinese garbled problem
Servlet Request forwarding jumps to another page: Chinese garbled characters appear
Ok.html
1 <!DOCTYPE HTML>2 <HTML>3 <Head>4 <MetaCharSet= "UTF-8">5 <title>Login successful</title>6 </Head>7 <Body>8
Although in line fourth <meta charset= "UTF-8" > has told the browser to show the encoding of the OK page, but not add response.setcharacterencoding ("UTF-8"); forwarding jump to OK page, the display is still garbled
and response redirects will not;
Here's a screenshot of someone's blog address
The forward method can be called only if the client does not have output. If the buffer of the current page is not empty, you must first empty the buffer before calling the forward method. "/" stands for relative to the Web application path RequestDispatcher rd = Request.getrequestdispatcher ("/ooo"); Rd.forward (request, response); submit to http://localhost:8080/Test/ooo RequestDispatcher Rd = Getservletcontext (). Getrequestdispatcher ("/ooo"); Rd.forward (request, response); submit to http://localhost:8080/Test/ooo RequestDispatcher Rd =getservletcontext (). Getnameddispatcher ("Testservlet"); (Testservlet is a<Servlet-name>Rd.forward (request, response); commit to a servlet named Testservlet if the<Jsp:forward>There is a lot of output before, the previous output has the buffer full, will automatically output to the client, then this statement will not work, this should pay special attention. Also note: it cannot change the browser address, which will cause duplicate submissions to be forwarded from http://localhost:8080/Test/gw/page.jsp .<Jsp:forwardpage= "otherpage.jsp"/>The JSP page is parsed and converted into Pagecontext.forward ("otherpage.jsp"); "/otherpage.jsp" submitted to http://localhost:8080/Test/OtherPage.jsp "otherpage.jsp" to http://localhost:8080/Test/gw/ Otherpage.jsp (Sendredirect using the HttpServletResponse Interface) method 302 is working on the user's browser, and Sendredirect () can be passed with parameters such as Servlet?name= Frank passes to the next page, and it can be redirected to a different host, and Sendredirect () can redirect the JSP file with frame. Assume that the forwarding code is included in the registered Servlet-url of/ggg/tt;jsp for/ggg/tt.jsp: Absolute path: Response.sendredirect ("http://www.brainysoftware.com") Send http://www.brainysoftware.com Root path: Response.sendredirect ("/ooo") sent to Http://localhost:8080/ooo Relative path: Response.sendredirect ("ooo") is sent to Http://localhost:8080/Test/ggg/ooo, Sendredirect is equivalent to this mode Response.setstatus ( httpservletresponse.sc_moved_permanently); String NEWLOCN = "/newpath/jsa.jsp"; Response.setheader ("Location", NEWLOCN); (meta Refresh) method 200 This method is provided by HTML, and Meta itself is an HTML tag. Use this method:<Metahttp-equiv= "Refresh"content= "5; url=http://www.dreamdu.com/" />The corresponding Java code String content=staytime+ "; Url= "+url; Response.setheader ("REFRESH", content);
Request forwarding and response redirection in Javaweb