天天看点

Servlet共享变量Context,Session,或Request的作用范围

在Servlet中进行变量的共享可硬通过Servlet容器中存在的ServletContext,HttpSession和HttpServletRequest的实例来实现。

在三中方式共享变量的方法是使用Context,Session,Request类型的实例调用serAttribute("varName","obj")方法将需要共享的变量存储到对象当中。然后在需要使用该共享变量的地方在通过getAttribute("varName")方法来获得变量。

Servlet共享变量Context,Session,或Request的作用范围
/*提供共享变量的Servlet类:ShareSharingVars.java*/
package javaee.servlet;
import ......
@WebServlet("/Share")
public class ShareSharingVars extends HttpServlet{
  ......
  protected void doGet(HttpServletRequest request,HttpServletResponse response)
    throws ServletException,IOException{
    //使用ServletContext共享变量
    ServletContext sc = this.getServletContext();
    sc.setAttribute("sharingvar_sc", "context");
    //使用httpSession共性变量
    HttpSession session = request.getSession();
    session.setAttribute("sharingvar_se", "session");
    //使用HttpServletRequest共性变量
    request.setAttribute("sharingvar_req", "request");
    
    //在同一页面中读取共享变量
    String sc_value = (String)sc.getAttribute("sharingvar_sc");
    String session_value = (String) session.getAttribute("sharingvar_se");
    String request_value = (String) request.getAttribute("sharingvar_req");
    //在对客户端的响应中显示读取的共享变量值
    response.setContentType("text/html; charset=UTF-8");
    PrintWriter out = response.getWriter();
    ......
    out.println("Context:" + sc_value + "<br>");
    out.println("Session:" + session_value + "<br>");
    out.println("Request:" + request_value + "<br>");
    out.println("<a href=/HelloWorld/GetSharingVars>");
    out.println("使用超链接跳转到使用共享变量页面");
    out.println("</a>");
    ......
  }
}      
/*提供共享变量的Servlet类:GetSharingVars.java*/
package javaee.servlet;
import ......
@WebServlet("/GetSharingVars")
public class GetSharingVars extends HttpServlet{
  ......
  protected void doGet(HttpServletRequest request,HttpServletResponse response)
    throws ServletException,IOException{
    //读取共享变量的值
    ServletContext sc = this.getServletContext();
    HttpSession session = request.getSession();
    String sc_value = (String) sc.getAttribute("sharingvar_sc");
    String session_value = (String) session.getAttribute("sharingvar_se");
    String request_value = (String) request.getAttribute("sharingvar_req");  
    //在对客户端的响应中显示读取的共享变量
    ......
    out.println("GetSharingVars输出页面<br>");
    out.println("在同一个页面中读取共享变量<br>");
    out.println("Context:" + sc_value + "<br>");
    out.println("Session:" + session_value + "<br>");
    out.println("Request:" + request_value + "<br>");
    .......
}      
/*提供共享变量的Servlet类:ShareSharingVarsDispatch.java*/
package javaee.servlet;
import ......
@WebServlet("/ShareAndDispatch")
public class ShareSharingVarsDispatch extends HttpServlet{
  ......
  protected void doGet(HttpServletRequest request, HttpServletResponse  response)
      throws ServletException,IOException{
    this.doPost(request, response);
  }
  protected void doPost(HttpServletRequest request,HttpServletResponse response)
      throws ServletException,IOException{
    //使用ServletContext共享变量
    ServletContext sc = this.getServletContext();
    sc.setAttribute("sharingvar_sc", "context");
    //使用HttpSession共享变量
    HttpSession session = request.getSession();
    session.setAttribute("sharingvar_se", "session");
    //使用HttpServletRequest共享变量
    request.setAttribute("sharingvar_req", "request");
    //使用转发跳转处理
    request.getRequestDispatcher("/GetSharingVars").forward(request, response);
  }
}      
/*提供共享变量的Servlet类:ShareSharingVarsAndRedirect.java*/
package javaee.servlet;
import ......
@WebServlet("/SharAndRedirect")
public class ShareSharingVarsAndRedirect extends HttpServlet{
  ......
  protected void doGet(HttpServletRequest request, HttpServletResponse  response)
      throws ServletException,IOException{
    this.doPost(request, response);
  }
  protected void doPost(HttpServletRequest request,HttpServletResponse response)
      throws ServletException,IOException{
    //使用ServletContext共享变量
    ServletContext sc = this.getServletContext();
    sc.setAttribute("sharingvar_sc", "context");
    //使用HttpSession共享变量
    HttpSession session = request.getSession();
    session.setAttribute("sharingvar_se", "session");
    //使用HttpServletRequest共享变量
    request.setAttribute("sharingvar_req", "request");
    //使用重定向跳转处理
    response.sendRedirect("/HelloWorld/GetSharingVars");
  }
}      

同一个页面内:

Servlet共享变量Context,Session,或Request的作用范围

点击跳转后(超链接):

Servlet共享变量Context,Session,或Request的作用范围

采用转发机制处理(request.getRequestDispatch):

Servlet共享变量Context,Session,或Request的作用范围

采用重定向(response.sendRedirect):

Servlet共享变量Context,Session,或Request的作用范围

关闭后再打开浏览器(http://localhost:8080/HelloWorld/GetSharingVars):

Servlet共享变量Context,Session,或Request的作用范围

PS:同一个页面,或者采用转发机制进行处理,ServletContext、HttpSession、HttpServletRequest的实例都不会发生改变。