域对象的作用范围 & 请求的转发和重定向

1. 和属性相关的方法:

①. 方法

          void setAttribute(String name, Object o): 设置属性 

          Object getAttribute(String name): 获取指定的属性

          Enumeration getAttributeNames(): 获取所有的属性的名字组成的 Enumeration 对象

          removeAttribute(String name): 移除指定的属性

 

②. pageContext, request, session, application 对象都有这些方法,

     这四个对象也称之为域对象.

     pageContext: 属性的作用范围仅限于当前 JSP 页面

     request: 属性的作用范围仅限于同一个请求.

     session: 属性的作用范围限于一次会话: 浏览器打开直到关闭称之为一次会话(在此期间会话不失效)

     application: 属性的作用范围限于当前 WEB 应用. 是范围最大的属性作用范围,

                        只要在一处设置属性, 在其他各处的 JSP 或 Servlet 中都可以获取到.

实验:

attr.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <!--
和属性相关的方法
void setAttribute(String name, Object o): 设置属性 
Object getAttribute(String name): 获取指定的属性

Enumeration getAttributeNames(): 获取所有的属性的名字组成的 Enumeration 对象
removeAttribute(String name): 移除指定的属性

pageContext     request   session   application 对象都有这些方法
这四个对象也称为域对象

pageContext: 属性的作用范围仅限于当前 JSP 页面
request: 属性的作用范围仅限于同一个请求. 
session: 属性的作用范围限于一次会话: 浏览器打开直到关闭称之为一次会话(在此期间会话不失效)
application: 属性的作用范围限于当前 WEB 应用. 是范围最大的属性作用范围, 只要在一处设置属性, 在其他各处的 JSP 或 Servlet 中
都可以获取到.
  -->

    <%
        //设置属性
        pageContext.setAttribute("pageContextAttr", "pageContextValue");
        request.setAttribute("requestAttr", "requestValue");
        session.setAttribute("sessionAttr", "sessionValue");
        application.setAttribute("applicationAttr", "applicationValue");
    %>
    <h2>Attr 1 Page</h2>
    <br><br>
    pageContextAttr: <%=pageContext.getAttribute("pageContextAttr") %>    
    <br><br>
    requestAttr: <%=request.getAttribute("requestAttr") %>
    <br><br>
    sessionAttr:<%=session.getAttribute("sessionAttr") %>
    <br><br>
    aplicationAttr:<%=application.getAttribute("applicationAttr") %>
    <br><br>
    
        <a href="arrt_2.jsp">To  Attr  2 Page</a>

</body>
</html>

 

attr_2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>Attr 2 Page</h2>
    <br><br>
    pageContextAttr: <%=pageContext.getAttribute("pageContextAttr") %>    
    <br><br>
    requestAttr: <%=request.getAttribute("requestAttr") %>
    <br><br>
    sessionAttr:<%=session.getAttribute("sessionAttr") %>
    <br><br>
    aplicationAttr:<%=application.getAttribute("applicationAttr") %>
    <br><br>
    
</body>
</html>

2. 请求的转发和重定向:

 本质区别: 请求的转发只发出了一次请求, 而重定向则发出了两次请求.

 具体:

①. 请求的转发: 地址栏是初次发出请求的地址.
请求的重定向: 地址栏不再是初次发出的请求地址. 地址栏为最后响应的那个地址

②. 请求转发: 在最终的 Servlet 中, request 对象和中转的那个 request 是同一个对象.
请求的重定向: 在最终的 Servlet 中, request 对象和中转的那个 request 不是同一个对象.

③. 请求的转发: 只能转发给当前 WEB 应用的的资源
请求的重定向: 可以重定向到任何资源.

④. 请求的转发: / 代表的是当前 WEB 应用的根目录
请求的重定向: / 代表的是当前 WEB 站点的根目录.

test.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<!-- 请求的转发和重定向
      本质区别: 请求的转发只发出一次请求,而重定向则发出了两次请求
             具体: 
                     ①.请求的转发: 地址栏是初次发出的地址,
                         请求的重定向:地址栏不再是初次发出请求的地址,而是最后响应的那个地址
                     ②.请求转发:在最终的Servlet中,request 对象和中转的那个 request 是同一个对象
                         请求重定向:在最终的Servlet中,request 对象和中转的那个 request 不是同一个对象
                     ③.请求的转发:只能转发给当前的 WEB 应用的资源
                         请求的重定向:可以重定向到任何资源,例如: 
response.sendRedirect("http://www.baidu.com"); ④.请求的转发: / 代表的是当前 WEB 应用的根目录 http://localhost:9090/WebT2/ 请求的重定向: / 代表的是当前 WEB 站点的根目录 http://localhost:9090/ -->
<a href="loginServlet">Test</a> <br/><br/> <a href="forwardServlet">Forward</a> <br/><br/> <a href="redirectServlet">Redirect</a> <br/><br/> </body> </html>

 

ForwardServlet

package com.aff.java;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/forwardServlet")
public class ForwardServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("ForwardServlet's  doGet   方法");
        // 请求的转发
        // 1. 调用HttpServletRequest 的 getRequestDispatch()
        // 方法获取RequestDispatcher对象
        // 调用 getRequestDispatch 需要传入转发的地址

        String path = "testServlet";
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/" + path);
// /代表当前web应用的根目录 // 2. 调用HttpServletRequest的forward(request,response)进行请求的转发 requestDispatcher.forward(request, response); } }

 

RedirectServlet

package com.aff.java;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/redirectServlet")
public class RedirectServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("redirectServlet's  do Get   方法");
        // 执行请求的重定向,直接调用 response.sendRedirect(path);
        // path为要重新向的地址
        String path = "testServlet";
        response.sendRedirect(path);
    }

}

 

TestServlet

package com.aff.java;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/testServlet")
public class TestServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("TestServlet's  do Get  方法");
    }

}

 

posted @ 2020-04-17 17:53  林淼零  阅读(359)  评论(0编辑  收藏  举报