Henry Sun

没有所谓的偶然,一切皆是因果

博客园 首页 新随笔 管理

 1、 无论对象的作用域如何,设置和读取共享变量的方法是一致的

  -setAttribute("varName",obj);

  -getAttribute("varName");

  2、变量的作用域

         ServletContext:范围最大,应用程序级别的,整个应用程序都能访问

         HttpSession:次之,会话级别的,在当前的浏览器中都能访问

         HttpServletRequest:范围最小,请求级别。请求结束,变量的作用域也结束

设置共享变量

package servletdemo;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class SetServletVar
 */
@WebServlet("/SetServletVar")
public class SetServletVar extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SetServletVar() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        ServletContext ctx=this.getServletContext();
        ctx.setAttribute("ctx_name", "ctx_value");
        HttpSession session=request.getSession();
        session.setAttribute("session_name", "session_value");
        request.setAttribute("request_name", "request_value");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
代码

获取共享变量(注意这是新建的一个Servlet)

package servletdemo;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class GetServetVar
 */
@WebServlet("/GetServetVar")
public class GetServetVar extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetServetVar() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        ServletContext ctx=this.getServletContext();
        HttpSession session=request.getSession();
        String ctx_value=(String)ctx.getAttribute("ctx_name");
        String session_value=(String)session.getAttribute("session_name");
        String request_value=(String)request.getAttribute("request_name");
        System.out.println("ctx_value:"+ctx_value);
        System.out.println("session_value:"+session_value);
        System.out.println("request_value:"+request_value);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
代码

  我们看到HttpServletRequest变量为空,验证了这个变量作用域是在请求级别的,但是如果我们在设置了变量后跳转到GetServletVar

  request.getRequestDispatcher("GetServetVar").forward(request, response);那么输出的值就是"request_value";

posted on 2015-05-16 17:32  Sam Flynn  阅读(297)  评论(0编辑  收藏  举报