码迷,mamicode.com
首页 > 其他好文 > 详细

Servlet 中 RequestDispacher 请求与分发

时间:2017-09-29 12:42:07      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:result   except   iter   cep   package   print   throw   close   attribute   

RequestDispacher 请求与分发使用HttpServletRequest的getRequestDispatcher()方法取得

Login.java页面

package control;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 实现请求后在请求然后再相应(请求LoginCheck后LoginCheck再请求ResultView,ResultView在响应)
 */
@WebServlet("/LoginCheck")
public class LoginCheck extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      String user=request.getParameter("user");
      String pwd=request.getParameter("pwd");
      if (user.equals("admin")&&pwd.equals("123")){
          request.setAttribute("state","成功");//请求设置带参数
      }
      else request.setAttribute("state","失败");
      request.getRequestDispatcher("ResultView").forward(request,response);
    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}

ResultView.java页面

package control;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/ResultView")
public class ResultView extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String jg = (String) request.getAttribute("state");
        PrintWriter out = response.getWriter();
        out.write(jg);
        out.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}

 

Servlet 中 RequestDispacher 请求与分发

标签:result   except   iter   cep   package   print   throw   close   attribute   

原文地址:http://www.cnblogs.com/feipengting/p/7610495.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!