zoukankan      html  css  js  c++  java
  • 请求内部转发与重定向区别


    1.请求内部转发

        使用:req.getRequestDispatcher("uri").forward(reqresp);
     
        1、多个Servlet共享一个request对象(重点)
        2、一次请求,当转发结束的时候也就是业务处理完毕的时候request对象会被销毁
        3、地址栏不改变
        注意在执行转发完毕后会继续执行代码,但是没有意义,所以一般在转发后面加return
        4、request对象的作用域(重中之重)
                作用域:共享request对象的Servlet
                作用域中存取数据:
                            req.setAttribute(key,value)
                            req.getAttribute(key)

                            req.removeAttribute(key)

    请求内部转发实例演示

    1. @Override
    2. protectedvoid service(HttpServletRequest req,HttpServletResponse resp)
    3. throws ServletException,IOException{
    4. //设置响应编码格式
    5. resp.setCharacterEncoding("utf-8");
    6. resp.setContentType("text/html;charset=utf-8");
    7. //设置请求字符编码格式
    8. req.setCharacterEncoding("utf-8");
    9. //请求转发(获取用户数据):
    10. System.out.println(req.getParameter("uname"));
    11. System.out.println(req.getParameter("pwd"));
    12. //开始请求转发
    13. req.setAttribute("405","听说支付宝要收费了,赶紧给我转账");
    14. req.getRequestDispatcher("tr.action").forward(req, resp);
    15. //开始重定向
    16. resp.sendRedirect("http://www.baidu.com");
    17. return;
    18. }
    1. @Override
    2. protectedvoid service(HttpServletRequest req,HttpServletResponse resp)
    3. throws ServletException,IOException{
    4. //获取用户数据
    5. System.out.println(req.getParameter("uname")+"-----tr.action");
    6. System.out.println(req.getParameter("pwd")+"-----tr.action");
    7. System.out.println(req.getAttribute("405"));
    8. resp.getWriter().write("哈哈");
    9. }
     
    2.重定向
                使用:resp.sendRedirect("uri");
                特点:
                    地址栏改变
                    两次请求,两个request对象
     
     
    3.重定向/请求转发时参数URI问题
    3.1重定向
    项目目录                                             
     
                 
    main.jsp               
     
    在main.jsp中进行重定向
    response.sendRedirect("/login.jsp")-->代表localhost:8080/login.jsp
    response.sendRedirect("login.jsp")-->代表localhost:8080/Admin/login.jsp
    response.sendRedirect("checkOper")-->代表localhost:8080/Admin/checkOper
    response.sendRedirect("/checkOper")-->代表localhost:8080/checkOper
    response.sendRedirect("/Exercise_Jsp/login.jsp")-->代表localhost:8080/Exercise_Jsp/login.jsp
    response.sendRedirect("/Exercise_Jsp/checkOper")-->代表localhost:8080/Exercise_Jsp/checkOper
     
    3.2请求转发
    checkOper中转到Oper下的selectAll.jsp
     
     

    请求内部转发和重定向图解

    总结:

    请求转发是一次请求,地址栏不改变,多个Sevrlet共享一个request
    重定向是两次请求,地址栏改变,两个request对象
     
     
     
     
     
     
     
     
     
     
     
  • 相关阅读:
    【查漏补缺】普通类中获取有注解的类
    【线程池原理】线程池的原理及实现
    【SpringCloud微服务实战学习系列】客户端负载均衡Spring Cloud Ribbon
    【SpringCloud错误】错误记录
    【SpringCloud微服务实战学习系列】服务治理Spring Cloud Eureka
    【SpringBoot整合Elasticsearch】SpringBoot整合ElasticSearch
    【SpringCloud微服务实战学习系列】配置详解
    【SpringCloud微服务实战学习系列】创建应用及解析
    【微服务系列】Spring SpringMVC SpringBoot SpringCloud概念、关系及区别
    【错误整理】ora-00054:resource busy and acquire with nowait specified解决方法【转】
  • 原文地址:https://www.cnblogs.com/haozhengfei/p/6537925.html
Copyright © 2011-2022 走看看