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对象
     
     
     
     
     
     
     
     
     
     
     
  • 相关阅读:
    RFID之linux下利用PN532对M1卡(饭卡,
    Ubuntu server配置远程VNC服务
    如何在Linux命令行中剪裁图像
    CentOS 6.3 yum安装LAMP(Apache+MySQL+PHP)
    ubuntu16.04 安装Opencv 3.1.0 import cv2 报错ImportError: No module named hdf5
    ubuntu16.04 安装Opencv 3.1.0 fatal error: hdf5.h: 没有那个文件或目录
    通过子网掩码确定主机地址
    单调数据结构
    利用Python分析羊车门问题
    Welcome To My Blog!!!
  • 原文地址:https://www.cnblogs.com/haozhengfei/p/6537925.html
Copyright © 2011-2022 走看看