ServletContext物件
阿新 • • 發佈:2020-08-13
ServletContext物件
1.什麼是ServletContext物件(代表整個專案的物件)
ServletContext代表是一個web應用的環境(上下文)物件,ServletContext物件 內部封裝是該web應用的資訊,ServletContext物件一個web應用只有一個
問題:一個web應用有幾個servlet物件?----多個
ServletContext物件的生命週期?
建立:該web應用被載入(伺服器啟動或釋出web應用(前提,伺服器啟動狀 態))
銷燬:web應用被解除安裝(伺服器關閉,移除該web應用)
開啟伺服器時建立
銷燬伺服器時銷燬
2.怎樣獲得ServletContext物件
1)ServletContext servletContext = config.getServletContext();
2)ServletContext servletContext = this.getServletContext();
//獲取ServletContext物件 ServletContext context=getServletContext();
3.ServletContext的作用
(1)獲得web應用中任何資源的絕對路徑(重要 重要 重要)
在伺服器上的絕對路徑
方法:String path = context.getRealPath(相對於該web應用的相對地址);
寫一個相對,得到一個絕對的path
通過相對路徑獲取絕對路徑,相對路徑相也對於伺服器
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //獲取ServletContext物件 ServletContext context=getServletContext(); //獲取通過相對路徑獲取絕對路徑 String patha=context.getRealPath("a.txt"); String pathb=context.getRealPath("WEB-INF/b.txt"); String pathc=context.getRealPath("WEB-INF/classes/c.txt"); System.out.println(patha); System.out.println(pathb); System.out.println(pathc); }
(2)ServletContext是一個域物件(重要 重要 重要)
什麼是域物件?什麼是域?
儲存資料的區域就是域物件
ServletContext域物件的作用範圍:整個web應用(所有的web資源都可以隨意向 servletcontext域中存取資料,資料可以共享)
Servlet01封存訊息,
Servlet02獲取訊息
public class Servlet01 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext context=getServletContext();
//存值 context.setAttribute("name", "小紅帽"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
public class Servlet02 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 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); } }
域物件的通用的方法:
setAtrribute(String name,Object obj);
getAttribute(String name);
removeAttribute(String name);
登入,顯示第幾個登入的
public class LoginServlet extends HttpServlet { private UserService userService=new UserService(); //對自定義資料進行初始化 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("uname"); String pwd=request.getParameter("pwd"); //調Service層方法 int count=userService.login(uname, pwd); if(count>0){ //獲取ServletContext物件 ServletContext context=getServletContext(); //取值 int c=(int)context.getAttribute("count"); int num=++c; //存值 context.setAttribute("count", num); response.getWriter().write("you are "+num+" success"); }else{ response.getWriter().write("fail"); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }