Servlet之间的跳转
Servlet之間的跳轉
1. 轉向(Forward)
轉向(forward)是通過RequestDispatcher對象的forward(HttpServletRequest request, HttpServletResponse response)來實現的。示例如下:
?| RequestDispatcher dispatcher = request.getRequestDispatcher("/servlet/LifeCycleServlet"); dispatcher.forward(request, response); |
getRequestDispatcher()方法的參數必須以“/”開始,“/”表示本Web應用程序的根目錄。如上例中,
表示要跳轉的地址為http://localhost:8080/servlet/servlet/LifeCycleServlet。
forward是最常用的方式,在Structs等MVC框架中,都是用Servlet來處理用戶請求,把結果通過request.setAttribute()放到request中,
然后forward到JSP中顯示。
當執行forward方法時,不能有任何輸出到達客戶端,否則會拋出異常,也就是說,在forward之前,不要使用out.println()語句向客戶端輸出。
?
| public void doGet(HttpServletRequest request, HttpServletResponse response) ????????throws ServletException, IOException { ????String destination = request.getParameter("destination"); ?????? ????if("file".equals(destination)){ ????????RequestDispatcher d = request.getRequestDispatcher("/WEB-INF/web.xml"); ????????d.forward(request, response); ????}else if("jsp".equals(destination)){ ????????request.setAttribute("date", new Date()); //attributes are reset between requests. ????????RequestDispatcher dispatcher = request.getRequestDispatcher("/forward.jsp"); ????????dispatcher.forward(request, response); ????}else if("servlet".equals(destination)){ ????????RequestDispatcher disp = request.getRequestDispatcher("/servlet/LifeCycleServlet"); ????????disp.forward(request, response); ????}else{ ????????response.setCharacterEncoding("UTF-8"); ????????response.getWriter().println("缺少參數。用法:"+request.getRequestURI()+"?destination=jsp或者file或者servlet"); ????} } |
2. 重定向(Redirect)
重定向是通過服務器端返回狀態碼來實現的。301,302都表示重定向,區別是301表示永久性重定向,302表示臨時性重定向。通過sendRedirect(String location)就可以實現重定向,下面是例子。本例子主要實現了Servlet來實現文件下載并統計下載次數。要下載的文件以及下載次數都保存在一個Map中。主要思路是:首先加載頁面表單,當用戶點擊下載鏈接時,客戶端發起請求,運行doGet里的if判斷,實現重定向。
重定向和跳轉的區別:跳轉是在服務器端實現的,客戶端瀏覽器并不知道該瀏覽動作,而使用Redict跳轉時,跳轉是在客戶端實現的,也就是說客戶端瀏覽器實際上請求了2次服務器。
?| public class RedictServlet extends HttpServlet { ?? ????Map<String,Integer> map = new HashMap<String,Integer>(); //new一個Map ?? ????public void init() throws ServletException { //放在init中,加載servlet時運行此方法,把文件內容放到map中去 ????????map.put("/download/setup.exe", 0); ????????map.put("/download/application.zip", 0); ????????map.put("/download/01.mp3", 0); ????} ?? ????public void doGet(HttpServletRequest request, HttpServletResponse response) ????????????throws ServletException, IOException { ????????String filename = request.getParameter("filename"); ?????????? ????????if(filename!=null){ ????????????int hit = map.get(filename); //取下載次數 ????????????map.put(filename, ++hit); //下載次數加1后保存 ????????????response.sendRedirect(request.getContextPath()+filename); //重定向到文件 ?????????????? ????????}else{ ????????????response.setCharacterEncoding("UTF-8"); ????????????PrintWriter out = response.getWriter(); ????????????response.setContentType("text/html"); ????????????out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); ????????????out.println("<HTML>"); ????????????out.println("? <HEAD><TITLE>文件下載</TITLE></HEAD>"); ????????????out.println("?? <link rel='stylesheet' type='text/css' href='../css/style.css'>"); ????????????out.println("? <BODY><br/>"); ?? ????????????out.println("<fieldset align=center style=width:90%><legend>文件下載</legend>"); //繪制頁面表單 ????????????out.println("<table width=100%>"); ????????????out.println("?? <tr>"); ????????????out.println("?????? <td><b>文件名"+"</b></td>"); ????????????out.println("?????? <td><b>下載次數</b></td>"); ????????????out.println("?????? <td><b>下載</b></td>"); ????????????out.println("?? </tr>"); ?????????????? ????????????for(Entry<String,Integer> entry: map.entrySet()){ //遍歷map的方法 ????????????????out.println("<tr>"); ????????????????out.println("?? <td>"+entry.getKey()+"</td>"); ????????????????out.println("?? <td>"+entry.getValue()+"</td>"); ????????????????out.println("?? <td><a href = '"+request.getRequestURI()+"?filename="+entry.getKey()+"'target = '_blank' onclick ='location = location;'>下載</a></td>"); //target='_blank'目標地址在無標題的新頁面中打開。onclick ='location = location;'頁面刷新 ????????????????out.println("</tr>"); ????????????} ????????????out.println("</table>"); ????????????out.println("?? </legend>"); ????????????out.println("? </BODY>"); ????????????out.println("</HTML>"); ????????????out.flush(); ????????????out.close(); ????????} ?????????? ????} ?? ????public void destroy() { ????????super.destroy(); // Just puts "destroy" string in log ????????// Put your code here ????????map = null; ????} } |
結果圖:
總結
以上是生活随笔為你收集整理的Servlet之间的跳转的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: POI入门教程
- 下一篇: 服务器端使用sendRedirect跳转