Java Development Servlet jump, assumervlet jump
I. Forward)
1. Key Points
The redirection is implemented through the forward () method of the RequestDispatcher object. RequestDispatcher can be obtained through the getRequestDispatcher () method of HttpServletRequest. The parameter of the getRequestDispatcher () method must start. Forward not only redirects to another Servlet, JSP page of the application, but also to another file, or even files in the WEB-INF folder.
Turn to request. setAttribute () to put the result into the request, and then forward it to jsp for display.
Note: When the forward action is executed, no output can be made to the client (for example, System. out. println (). Otherwise, an IllegalStateException will be thrown. That is, do not use the out. println () statement to output data to the client before forward.
2. Code practice
Web. xml
1 <! -- Forward --> 2 <servlet> 3 <servlet-name> ForwardServlet </servlet-name> 4 <servlet-class> com. servlet. forwardServlet </servlet-class> 5 </servlet> 6 <servlet-mapping> 7 <servlet-name> ForwardServlet </servlet-name> 8 <url-pattern>/servlet/ forwardServlet </url-pattern> 9 </servlet-mapping>
ForwardServlet. java
1 package com. servlet; 2 3 import java. io. IOException; 4 import java. io. printWriter; 5 6 import javax. servlet. requestDispatcher; 7 import javax. servlet. servletException; 8 import javax. servlet. http. httpServlet; 9 import javax. servlet. http. httpServletRequest; 10 import javax. servlet. http. httpServletResponse; 11 12 public class ForwardServlet extends HttpServlet {13 14 public void doGet (HttpServletRequ Est request, HttpServletResponse response) 15 throws ServletException, IOException {16 17 request. setCharacterEncoding ("UTF-8"); 18 String destination = request. getParameter ("destination"); 19 if ("file ". equals (destination) {// jump to/WEB-INF/web. xml20 RequestDispatcher d = request. getRequestDispatcher ("/WEB-INF/web. xml "); 21 d. forward (request, response); 22} else if ("jsp ". equals (destination) {// jump to UploadFi Le. jsp23 RequestDispatcher dispatcher = request. getRequestDispatcher ("/page/UploadFile. jsp "); 24 dispatcher. forward (request, response); 25} else if ("servlet ". equals (destination) {// jump to another Servlet26 RequestDispatcher dispatcher = request. getRequestDispatcher ("/servlet/LifeCycleServlet"); 27 dispatcher. forward (request, response); 28} else {29 response. setCharacterEncoding ("UTF-8"); 30 response. setContentTy Pe ("text/html"); 31 response. getWriter (). println ("the parameter is missing. Usage: "+ request. getRequestURI () + 32 "? Destination = jsp or file or servlet "); 33} 34 35} 36 37}
Forward. jsp
1 <% @ page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> 2 <% 3 String path = request. getContextPath (); 4 String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; 5%> 6 7 <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 8
3. Results
Forward. jsp:
Jump to servlet:
Jump to web. xml:
Jump to jsp:
2. Redirection (Redirect)
1. Highlights
Redirection is implemented using the status code returned by the server. When the client browser requests the server, the server returns a status code. The server uses the setStatus () method of HttpServletResponse to set the status code.
HttpServletResponse status code
Status Code |
Meaning |
1xx |
Information Status Code. Indicates that the request has been accepted and is being processed. |
2xx |
The correct status code. Indicates that the request has been correctly received and processed, and no errors have occurred. For example, 200 indicates that everything is correct. |
3xx |
Redirect status code. For example, 301 (permanent redirection) and 302 (zero-point redirection) indicate that the resource does not exist or the address is changed, and the client needs to redirect to a new resource. The new resource address is included in the server response. |
4xx |
Request error. For example, "401" indicates no access permission, "404" indicates that the resource does not exist, and "405" indicates that the access method is incorrect (for example, the Servlet only accepts the GET method, but the POST method is accessed but the client uses the DELETE method) |
5xx |
Server error. For example, 500 indicates that the program stops running because of an exception. |
2. Code practice
Web. xml
1 <! -- Redirect --> 2 <servlet> 3 <servlet-name> RedirectServlet </servlet-name> 4 <servlet-class> com. servlet. redirectServlet </servlet-class> 5 </servlet> 6 <servlet-mapping> 7 <servlet-name> RedirectServlet </servlet-name> 8 <url-pattern>/servlet/ redirectServlet </url-pattern> 9 </servlet-mapping>
RedirectServlet. java
1 package com. servlet; 2 3 import java. io. IOException; 4 import java. io. printWriter; 5 import java. util. hashMap; 6 import java. util. map; 7 import java. util. map. entry; 8 9 import javax. servlet. servletException; 10 import javax. servlet. http. httpServlet; 11 import javax. servlet. http. httpServletRequest; 12 import javax. servlet. http. httpServletResponse; 13 14 public class RedirectServlet extends HttpServlet {15 16 Map <String, Integer> map = new HashMap <String, Integer> (); 17 18 public void destroy () {19 super. destroy (); // Just puts "destroy" string in log20 // Put your code here21 map = null; 22} 23 24 public void doGet (HttpServletRequest request, HttpServletResponse response) 25 throws ServletException, IOException {26 27 String filename = request. getParameter ("filename"); 28 if (filename! = Null) {// count the number of downloads 29 int hit = map. get (filename); // get download times 30 map. put (filename, ++ hit); // download times + 1 and save 31 response. sendRedirect (request. getContextPath () + filename); // redirect to file 32} else {// display download times 33 response. setCharacterEncoding ("UTF-8"); 34 response. setContentType ("text/html"); 35 PrintWriter out = response. getWriter (); 36 out. println ("<! Doctype html public \ "-// W3C // dtd html 4.01 Transitional // EN \"> "); 37 out. println ("<HTML>"); 38 out. println ("<HEAD> <TITLE> File Download </TITLE> </HEAD>"); 39 out. println ("<BODY>"); 40 out. println ("<table width = 100%>"); 41 out. println ("<tr>"); 42 out. println ("<td> <B> file name </B> </td>"); 43 out. println ("<td> <B> downloads </B> </td>"); 44 out. println ("<td> <B> download </B> </td>"); 45 out. println ("</tr>"); 46 for (Entry <String, Integer> entr Y: map. entrySet () {// traverse map47 out. println ("<tr>"); 48 out. println ("<td>" + entry. getKey () + "</td>"); 49 out. println ("<td>" + entry. getValue () + "</td>"); 50 out. println ("<td> <a href = '" + request. getRequestURI () + 51 "? Filename = "+ entry. getKey () + "'target = 'blank '" + 52 "onclick = 'location = location;'> download </a> </td>"); 53 out. println ("</tr>"); 54} 55 out. println ("</table>"); 56 out. println ("</legend>"); 57 out. println ("</BODY>"); 58 out. println ("</HTML>"); 59 out. flush (); 60 out. close (); 61} 62} 63 64 public void init () throws ServletException {65 // Put your code here66 map. put ("/uploadfile/1.txt", 0); 67 map. put ("/uploadfile/2.txt", 0); 68 map. put ("/uploadfile/3425.jpg", 0); 69} 70 71}
3. Results
3. Refresh)
1. Highlights
Automatic refresh not only automatically jumps to another page after a period of time, but also automatically refreshes this page after a period of time. Servlet sets the Header attribute through the HttpServletResponse object to achieve automatic refresh.
2. Code practice
1 response.setHeader("Refresh", "1000; URL=http://localhost:8080/King/servlet/Forward.jsp");
1000 is the time, in milliseconds. When the URL is set to the Servlet Path, the page is automatically refreshed on a regular basis.