09 ServletContext 作用2

请求重定向:
1. 地址栏会发送改变,变成重定向到的地址。
2. 可以跳转到项目内的资源,也可以跳转项目外的资源
3. 浏览器向服务器发出两次请求,那么就不能使用请求来作为域对象来共享数据。

请求转发:
1. 地址栏不会改变。
2. 只能跳转到项目内的资源,不能跳转到项目外的资源
3. 浏览器向服务器发出一次请求,那么可以使用请求作为域对象共享数据

</br>
使用重定向技术访问本站和其他站页面:

public class RedirectDemo extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
   
        /**
         * 请求重定向(浏览器行为)
         */
        //response.sendRedirect("/myFirstServlet/hello.html");

        
        /**
         * 注意:
         *  可以跳转到当前项目的资源,也可以跳转到其他项目的资源
         */
        //resp.sendRedirect("/mySecondServlet/love.html");

        /**
         * 把数据保存到request域对象
         */
        request.setAttribute("name", "丁昌江");
        response.sendRedirect("/myFirstServlet/GetDataServlet");    
    }
}

</br>
使用转发技术访问本站和其他站页面:

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /**
         * 转发:服务器行为
         */
        /*
        ServletContext context = this.getServletContext();
        RequestDispatcher rd = context.getRequestDispatcher("/hello.html");
        rd.forward(request, response);
        /*
         获取RequestDispatcher的简洁方式:
         直接从HttpServletRequest对象上提取出来,但是要其内部还是使用了ServletContext()来获取的
         */
        //RequestDispatcher dis = req.getRequestDispatcher("/hello.html");
        //dis.forward(req, resp);
        
        //转发本站页面
        //RequestDispatcher dis = req.getRequestDispatcher("/hello.html");
        //dis.forward(req, resp);
        
        //转发到其他网站页面(无效)
//      RequestDispatcher dis2 = req.getRequestDispatcher("../mySecondServlet/love.html");
//      dis2.forward(req, resp);
        
        /**
         * 把数据保存到request域对象
         */
        req.setAttribute("name", "丁昌江");
        RequestDispatcher dis = req.getRequestDispatcher("/GetDataServlet");
        dis.forward(req, resp);
    }

从GetDataServlet中的req域对象中读取数据:(结果显示,只能读取到转发的数据)

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String value = (String)req.getAttribute("name");
        System.out.println("name:"+value);
    }
Paste_Image.png

读取web项目中的资源文件:

java.lang.String getRealPath(java.lang.String path)
java.io.InputStream getResourceAsStream(java.lang.String path)
java.net.URL getResource(java.lang.String path)

Paste_Image.png
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//      String path = getServletContext().getRealPath("/");//表示当前项目根
//      String path2 = getServletContext().getRealPath(".");//当前路径就是当前项目根
//      System.out.println(path2);
        
        //方式一:
//      String path = getServletContext().getRealPath("/WEB-INF/classes/user.properties");
//      InputStream in = new FileInputStream(path);
        
        //方式二:
        InputStream in = getServletContext().getResourceAsStream("/WEB-INF/classes/user.properties");
        Properties pro = new Properties();
        pro.load(in);
        String name = pro.getProperty("name");
        String passwd = pro.getProperty("passwd");
        System.out.println(name+":"+passwd);
/*
C:\Users\Administrator\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\ServletStudy\WEB-INF\classes\user.properties
*/
        
        //方式三:
        URL url = getServletContext().getResource("/WEB-INF/classes/user.properties");
        System.out.println(url.getPath());
/*
/localhost/ServletStudy/WEB-INF/classes/user.properties
*/
    
        //方式四
        URL url = Thread.currentThread().getContextClassLoader().getResource("user.properties");
        System.out.println(url.getPath());
        ///D:/softInstall/tomcat/apache-tomcat-7.0.70/webapps/servlet_study/WEB-INF/classes/user.properties

        //方式五
        Thread.currentThread().getContextClassLoader().getResourceAsStream("user.properties")
}

结论:
转发用的是同一个request对象,所以该域对象存储的数据才可以被其他Servlet通过request对象获取;

而重定向是使用了两个不同的request对象,一个域对象存储的数据不可能被另一个域对象获取到;

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,084评论 19 139
  • 从三月份找实习到现在,面了一些公司,挂了不少,但最终还是拿到小米、百度、阿里、京东、新浪、CVTE、乐视家的研发岗...
    时芥蓝阅读 42,494评论 11 349
  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 14,022评论 6 13
  • 国产老电影
    安逸人阅读 1,679评论 0 0
  • 一颗糖果决定给主人一点甜头 要把自己融化在主人的 舌头和口腔的温度里 糖果一层层地变成糖浆,糖水 被咽进喉头 小糖...
    段童阅读 1,625评论 0 1

友情链接更多精彩内容