标签:
先来看个请求转发的实例:
在这个例子中,我们编写一个PortalServlet,在这个Servlet中,首先判断访问用户是否已经登录,如果没有登录,则调用RequestDispatcher接口的include()方法,将请求转发给LoginServlet2,LoginServlet2在响应中发送登录表单;如果已经登录,则调用RequestDispatcher接口的forward()方法,请求转发给WelcomeYou界面,向用户显示欢迎信息。
1,PortalServlet.java
1 package com.woodrow.outputInfo; 2 import javax.servlet.http.*; 3 import javax.servlet.*; 4 import java.io.*; 5 6 public class PortalServlet extends HttpServlet 7 { 8 public void doGet(HttpServletRequest req, HttpServletResponse resp) 9 throws ServletException,IOException 10 { 11 resp.setContentType("text/html;charset=gb2312"); 12 13 PrintWriter out=resp.getWriter(); 14 15 out.println("<html><head><title>"); 16 out.println("登录页面"); 17 out.println("</title></head><body>"); 18 19 String name=req.getParameter("user"); 20 String pwd=req.getParameter("password"); 21 22 if("zhangsan".equals(name) && "1234".equals(pwd)) 23 { 24 ServletContext context=getServletContext(); 25 RequestDispatcher rd=context.getRequestDispatcher("/welcome"); 26 rd.forward(req,resp); 27 } 28 else 29 { 30 RequestDispatcher rd=req.getRequestDispatcher("login2"); 31 rd.include(req,resp); 32 } 33 out.println("</body></html>"); 34 out.close(); 35 } 36 public void doPost(HttpServletRequest req, HttpServletResponse resp) 37 throws ServletException,IOException 38 { 39 doGet(req,resp); 40 } 41 }
2,LoginServlet2.java
1 package com.woodrow.outputInfo; 2 import javax.servlet.*; 3 import java.io.*; 4 import javax.servlet.http.*; 5 6 public class LoginServlet2 extends HttpServlet 7 { 8 public void doGet(HttpServletRequest req, HttpServletResponse resp) 9 throws ServletException,IOException 10 { 11 12 resp.setContentType("text/html;charset=gb2312"); 13 14 PrintWriter out=resp.getWriter(); 15 16 out.println("<form method=post action=portal>"); 17 18 out.println("<table>"); 19 20 out.println("<tr>"); 21 out.println("<td>请输入用户名</td>"); 22 out.println("<td><input type=text name=user></td>"); 23 out.println("</tr>"); 24 25 out.println("<tr>"); 26 out.println("<td>请输入密码</td>"); 27 out.println("<td><input type=password name=password></td>"); 28 out.println("</tr>"); 29 30 out.println("<tr>"); 31 out.println("<td><input type=reset value=重填></td>"); 32 out.println("<td><input type=submit value=登录></td>"); 33 out.println("</tr>"); 34 35 out.println("</table>"); 36 37 out.println("</form>"); 38 } 39 40 public void doPost(HttpServletRequest req, HttpServletResponse resp) 41 throws ServletException,IOException 42 { 43 doGet(req,resp); 44 } 45 }
3,WelcomeYou.java
1 package com.woodrow.outputInfo; 2 import java.io.IOException; 3 import java.io.PrintWriter; 4 import javax.servlet.*; 5 import javax.servlet.http.*; 6 7 public class WelcomeYou extends HttpServlet 8 { 9 public void doGet(HttpServletRequest req, HttpServletResponse resp) 10 throws ServletException,IOException 11 { 12 String user=req.getParameter("user"); 13 String welcomeInfo="Welcome you, "+user; 14 15 resp.setContentType("text/html"); 16 17 PrintWriter out=resp.getWriter(); 18 19 out.println("<html><head><title>"); 20 out.println("Welcome Page"); 21 out.println("</title></head>"); 22 out.println("<body>"); 23 out.println(welcomeInfo); 24 out.println("</body></html>"); 25 out.close(); 26 } 27 28 public void doPost(HttpServletRequest req, HttpServletResponse resp) 29 throws ServletException,IOException 30 { 31 doGet(req,resp); 32 } 33 }
1 <servlet> 2 <servlet-name>welcome</servlet-name> 3 <servlet-class>com.woodrow.outputInfo.WelcomeYou</servlet-class> 4 </servlet> 5 <servlet> 6 <servlet-name>PortalServlet</servlet-name> 7 <servlet-class>com.woodrow.outputInfo.PortalServlet</servlet-class> 8 </servlet> 9 10 <servlet> 11 <servlet-name>LoginServlet2</servlet-name> 12 <servlet-class>com.woodrow.outputInfo.LoginServlet2</servlet-class> 13 </servlet> 14 15 <servlet-mapping> 16 <servlet-name>welcome</servlet-name> 17 <url-pattern>/welcome</url-pattern> 18 </servlet-mapping> 19 20 21 <servlet-mapping> 22 <servlet-name>PortalServlet</servlet-name> 23 <url-pattern>/portal</url-pattern> 24 </servlet-mapping> 25 26 <servlet-mapping> 27 <servlet-name>LoginServlet2</servlet-name> 28 <url-pattern>/login2</url-pattern> 29 </servlet-mapping>
启动Tomcat服务器,打开IE浏览器,在地址栏中输入http://localhost:8080/OutputInfo/portal,你将看到如下图:
第一次访问时,用户还没有登录,PortalServlet会先输出一部分HTML代码,然后利用include()方法调用LoginServlet2对请求进行响应,LoginServlet2输出登录表单后PortalServlet继续输出剩余的HTML代码,最后关闭输出流对象,提交响应。
在登录表单中输入用户名zhangsan和密码1234,单击“登录”提交按钮,你将看到如下图:
当用户输了正确的用户名和密码,PortalServlet利用forwrd()方法将请求转发给WelcomeYou进行处理,而WelcomeYou想用户发回了欢迎信息。在PortalServlet中调用forward()方法之前输出的没有提交的HTML代码被清除,而在调用forward()方法之后,执行的控制权转到WelcomeYou,PortalServlet中剩余的代码也不再有效。
HttpServletResponse接口的sendRedirect()方法和RequestDispatcher接口的forward()方法都可以利用另外的资源(Servlet,jsp页面或HTML文件)来为客户端进行服务,但是这两种方法有着本质上的区别。
sendRedirect()方法的工作原理图:
交互过程如下:
1,浏览器访问Servlet1;
2,Servlet1想让Servlet2为客户端服务;
3,Servlet1调用sendRedirect()方法,将客户端的请求重定向到Servlet2;
4,浏览器访问Servlet2;
5,Servlet2对客户端的请求做出响应
forward()方法的工作原理图:
交互过程如下:
1,浏览器访问Servlet1;
2,Servlet1想让Servlet2对客户端的请求进行响应,于是调用forward()方法,请求转发给Servlet2处理;
3,Servlet2对请求做出响应。
sendRedirect()方法和forward()方法还有一个区别,那就是sendRedirect()方法不但可以在位于同一主机上的不同Web应用程序之间进行重定向,而且还可以将客户端重定向到其他服务器上的Web应用程序资源。
009_02sendRedirect()和forward()方法的区别
标签:
原文地址:http://www.cnblogs.com/woodrow2015/p/4584372.html