• Servlet 中 RequestDispacher 请求与分发


    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 {
    
        }
    }
  • 相关阅读:
    Python的运算符
    RabbitMQ 的配置文件
    安装新版本的rabbitmq
    Ubuntu 16.04 安装rabbitmq
    Python Web 版本tailf, grep
    解决pycharm问题:module 'pip' has no attribute 'main'
    Python argparse
    Ansible 并行和异步
    cef相关
    浏览器透明设置例子,qt5.6才支持
  • 原文地址:https://www.cnblogs.com/feipengting/p/7610495.html
Copyright © 2020-2023  润新知