ServletContext-作用1 ***
-
(1)什麼是ServletContext
1:ServletContext是Servlet中自帶一個對象
2:ServletContext對象時在第一次通路Servlet時,自動建立
3:一個項目中隻有一個ServletContext對象
(2) 作用1:ServletContext可以實作多個Servlet之間資料的共享
getServletContext()
setAttribute(key,value)
getAttribute(key)

Servlet1可以往ServletContext添加鍵值對數,所有的Servlet都可以取
src\com\wzx\Demo01SetServlet.java
@WebServlet("/set")
public class Demo01SetServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1:擷取ServletContext
ServletContext context = super.getServletContext();
//2:添加資料 key-value 集合 Map
context.setAttribute("name","jack");
context.setAttribute("age",22);
System.out.println("setServlet");
}
}
src\com\wzx\Demo02GetServlet.java
@WebServlet("/get")
public class Demo02GetServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.擷取servletContext
ServletContext context = getServletContext();
//2.根據鍵擷取值
String name = (String) context.getAttribute("name");
Integer age = (Integer) context.getAttribute("age");
System.out.println(name);
System.out.println(age);
}
}
通路測試
http://localhost:8080/web02_servletContext_war_exploded/set
http://localhost:8080/web02_servletContext_war_exploded/get
ServletContext-作用2
2:ServletContext可以讀取web項目中檔案的内容
- src下的檔案:
InputStream is = Demo3FileServlet.class.getClassLoader().getResourceAsStream("a.txt");
- web目錄下檔案
InputStream is = getServletContext().getResourceAsStream("a.txt");
src\com\wzx\Demo03FileServlet.java
@WebServlet("/file")
public class Demo03FileServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1:java使用類加載器去讀取
//InputStream inputStream = Demo03FileServlet.class.getClassLoader().getResourceAsStream("a.txt");
InputStream inputStream = getServletContext().getResourceAsStream("b.txt");
//2:将位元組流轉成字元流
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
String line = br.readLine();
response.setCharacterEncoding("utf-8");
response.getWriter().println(line);
System.out.println(line);
}
}
通路測試
http://localhost:8080/web02_servletContext_war_exploded/file
ServletContext-作用3
web\WEB-INF\web.xml
3:ServletContext可以讀取整個項目的初始化資料
<context-param>
<param-name>ip</param-name>
<param-value>192.168.31.72</param-value>
</context-param>
src\com\wzx\Demo04ParamServlet.java
@WebServlet("/param")
public class Demo04ParamServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1:擷取ServletContext
ServletContext context = getServletContext();
//2:列印
String value = context.getInitParameter("ip");
System.out.println(value);
}
}
通路測試
http://localhost:8080/web02_servletContext_war_exploded/param
一個網站被多少使用者登入通路
比如 三個人 通過LoginServlet登入成功,那必須讓LoginServlet 對count值三次+1
在前面的登入案例裡面修改
(1)初始化count值
src\com\wzx\web\LoginServlet.java
@Override
public void init() throws ServletException {
//Servlet建立出來,ServletContext也會建立出來
//1:擷取ServletContext
ServletContext context = getServletContext();
//2:添加一個count變量,預設值為0
context.setAttribute("count",0);
}
(2)每次登入成功對count值+1
src\com\wzx\web\LoginServlet.java
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1:擷取參數
String username=request.getParameter("username");
String password=request.getParameter("password");
//2:處理參數
UserService userService = new UserService();
//2.1登入就是拿着賬号,密碼去資料庫判斷,存在就傳回true,否則就傳回false
boolean result = userService.login(username,password);
response.setCharacterEncoding("utf-8");
//2.2根傳回結果提示使用者
if(result){
//登入成功,對count值+1
//a.擷取servletContext
ServletContext context = getServletContext();
//b.擷取裡面的count
Integer count= (Integer) context.getAttribute("count");
//c. count + 1
count += 1;
//d. 放回ServletContext
context.setAttribute("count",count);
//3:響應給浏覽器
response.getWriter().println("登入成功,您是第"+count+"個登入");
}else{
response.getWriter().println("賬号或者密碼出錯");
}
}