09 ServletContext 作用2

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

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



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

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");    
    }
}



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

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);
    }
09 ServletContext 作用2_第1张图片
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)

09 ServletContext 作用2_第2张图片
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对象,一个域对象存储的数据不可能被另一个域对象获取到;

你可能感兴趣的:(09 ServletContext 作用2)