zoukankan      html  css  js  c++  java
  • Servlet 网页重定向

      当文档移动到新的位置,我们需要向客户端发送这个新位置时,我们需要用到网页重定向。当然,也可能是为了负载均衡,或者只是为了简单的随机,这些情况都有可能用到网页重定向。

      重定向请求到另一个网页的最简单的方式是使用 response 对象的 sendRedirect() 方法。

    转发页面跳转

    1.request.getRequestDispatcher("//WEB-INF/jsp/reg.jsp").forward(request,response);
    2.response.sendRedirect("/web/user?m=login");
     
    public class UserController extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            request.setCharacterEncoding("utf-8");
            doGet(request,response);
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //接受客户端发送的参数
            String m = request.getParameter("m");
            if("reg".equals(m)){
                //显示注册界面
                //1.转发
                request.getRequestDispatcher("//WEB-INF/jsp/reg.jsp").forward(request,response);
                //跳转页面,forward执行转发 需要request,response两个参数
            }if("regDo".equals(m)){  //执行注册 将获取参数注册到数据库中
                regDo(request,response);
            }else if("login".equals(m)) {
                //转发
                //获取分发器
                RequestDispatcher dis = request.getRequestDispatcher("/WEB-INF/jsp/login.jsp");
                //执行转发
                dis.forward(request, response);
            }
    }
      private void loginDo(HttpServletRequest request, HttpServletResponse response) throws IOException {
            String username =request.getParameter("username");
            String password =request.getParameter("password");
            UserEntity user =SqlUtil.login(username,password); //返回对象
            if(user !=null){ //对象不为空登录成功
                response.sendRedirect("/web/user?m=main");
            }else { //登录失败
                response.sendRedirect("/web/user?m=login");
            }
    
        }
    
    


  • 相关阅读:
    Selenium+Java自动化之如何优雅绕过验证码
    Java替换中使用正则表达式实现中间模糊匹配
    【转】Jmeter中使用CSV Data Set Config参数化不重复数据执行N遍
    【转】安全测试===如何查看浏览器保存的密码
    JMeter 中_time 函数的使用(时间戳、当前时间)
    TCP拆包粘包之分隔符解码器
    TCP粘包/拆包问题
    Netty 入门示例
    JDK AIO编程
    JDK NIO编程
  • 原文地址:https://www.cnblogs.com/houtian2333/p/10718215.html
Copyright © 2011-2022 走看看