1. 程式人生 > 實用技巧 >ServletContext類

ServletContext類

ServletContext物件:

        ServletContext代表是一個web應用的環境(上下文)物件,ServletContext物件 內部封裝是該web應用的資訊,ServletContext物件一個web應用只有一個。

        一個web應用有多個Servlet物件。

        ServletContext物件的生命週期?

        建立:該web應用被載入(伺服器啟動或釋出web應用(前提,伺服器啟動狀 態))

        銷燬:web應用被解除安裝(伺服器關閉,移除該web應用)

        獲得Servlet物件,獲得Servlet應用中任何資源的絕對路徑。(放資源一般放在WebContent上,不會直接放在WEB專案上)

public class Servlet01 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //獲取ServletContext物件
        ServletContext context=getServletContext();
        //獲取資源的相對路徑
        String patha=context.getRealPath("WEB-INF/classes/a.txt");
        System.out.println(patha);
        String pathb
=context.getRealPath("b.txt"); System.out.println(pathb); String pathc=context.getRealPath("WEB-INF/c.txt"); System.out.println(pathc); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }

      ServletContext是一個域物件(儲存資料的區域就是域物件),

      ServletContext域物件的作用範圍:整個web應用(所有的web資源都可以隨意向 servletcontext域中存取資料,資料可以共享)

      域物件的通用的方法:

        setAtrribute(String name,Object obj);

        getAttribute(String name);

        removeAttribute(String name);

        

   

public class MyServlet01 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //獲取ServletContext物件
        ServletContext context=getServletContext();
        //向域中存值
        context.setAttribute("name","張三");
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}   
public class MyServlet02 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //獲取ServletContext物件
                ServletContext context=getServletContext();
        //取值
            String name=(String)context.getAttribute("name");
            System.out.println(name);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

設定計數器功能:

      

public class LoginServlet extends HttpServlet {
private UserService userService=new UserService();
@Override
    public void init() throws ServletException {
    //初始化計數器
        int count=0;
//將計數器放在ServletContext域中    
        //存值
        ServletContext context=getServletContext();
        context.setAttribute("count",count);
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //獲取使用者名稱密碼
        String uname=request.getParameter("username");
        String pwd=request.getParameter("password");
        int count=userService.login(uname, pwd);
        if(count>0){
            //響應客戶端
            //登入成功
            //獲取ServletContext物件
            ServletContext context=getServletContext();
            //取值
            int sum=(int)context.getAttribute("count");
            sum++;
            //將計數器放回去
            context.setAttribute("count",sum);
            response.getWriter().write("yuu are the"+sum+"  person");
        }else{
            response.getWriter().write("false");
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}