日期:2014-05-17 浏览次数:20799 次
public class CheckServlet extends HttpServlet { @Override //响应客服端请求 public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { //获取用户名 String username = request.getParameter("username"); String message = null; if(username==null) message = "please input username"; else { message = "hello "+username; } //添加message属性 request.setAttribute("message", message); //把请求转发给outputServlet ServletContext context = getServletContext(); RequestDispatcher dispatcher = context.getNamedDispatcher("/output"); PrintWriter out = response.getWriter(); out.print("output from checkServlet before forwarding request"); System.out.println("output from checkServlet before forwarding request"); dispatcher.forward(request,response); out.print("output from checkServlet after forwarding request"); System.out.println("output from checkServlet after forwarding request"); } }
public class OutServlet extends HttpServlet { @Override public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { //读取checkServlet存放在请求范围内的消息 String message = (String) request.getAttribute("message"); PrintWriter out = response.getWriter(); out.print(message); out.close(); } }