第五章:Web上下文
第1節:ServletContext接口
知識點1:上下文對象的概念、作用
1.什麼是上下文對象
用來存儲全局範圍資訊的對象,一個web應用隻有唯一的一個上下文對象.
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIyVGduV2YfNWawNCM38FdsYkRGZkRG9lcvx2bjxiNx8VZ6l2cscXTE9kZ50WWxw2MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnL0QjN3ITMykDM4ATNwAjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
2.上下文對象的作用
存儲全局共享的資料
3.上下文對象的建立時機
伺服器啟動的時候,為每一個web應用建立一個上下文對象
4.上下文對象的銷毀時機
伺服器關閉的時候,上下文對象就銷毀了.
5.上下文對象的接口
ServletContext接口
6.上下文接口 的方法
JavaWeb(五)——Web上下文
知識點2:上下文擷取方法
1.getServletContext()就能擷取上下文對象
實際上調用的是GenericServlet的getServletContext()方法
得到的對象是:[email protected]
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 擷取上下文對象
ServletContext servletContext = getServletContext();
//列印
System.out.println(servletContext);
response.getWriter().append("Served at: ").append(request.getContextPath());
}
知識點3:上下文參數
1.上下文參數的配置
web.xml的配置 上下文參數
<context-param>
<param-name>company</param-name>
<param-value>中軟國際科技有限公司</param-value>
</context-param>
2.如何得到上下文參數的值
上下文對象.getInitParameter(name)
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 擷取上下文對象
ServletContext servletContext = getServletContext();
//列印
System.out.println(servletContext);
//得到上下文參數的值
String company = servletContext.getInitParameter("company");
//列印上下文參數的值
System.out.println(company);
response.getWriter().append("Served at: ").append(request.getContextPath());
}
知識點4:使用注解配置Servlet初始化參數
Servlet3.0規定開始就可以使用注解配置servlet
在servlet類上添加注解:
@WebServlet(urlPatterns="/HelloServlet",initParams= {@WebInitParam(name=“name”,value=“zs”)})
相當于web.xml中:
<servlet>
<description></description>
<display-name>HelloServlet</display-name>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>com.tjetc.servlet.HelloServlet</servlet-class>
<init-param>
<param-name>name</param-name>
<param-value>zs</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/HelloServlet</url-pattern>
</servlet-mapping>
第二節:資料作用域
知識點1:利用ServletContext在應用中共享資料
與屬性相關的方法
上下文的屬性可以在整個應用中共享
JavaWeb(五)——Web上下文
知識點2:四大作用域範圍
在Web應用中,有四大作用域範圍
1.頁面範圍(pageContext):一個頁面,不能跨頁面;
2.請求範圍(request):一次請求中可以通路多個Servlet或JSP; 通路的Servlet或JSP能夠包含其他資源;可以跨頁面,必須使用請求轉發帶資料
3.會話範圍:一次會話中可以包含多個請求;可以跨請求跨頁面,使用請求轉發和重定向都能帶資料
4.上下文範圍:上下文包含所有會話;可以跨會話跨請求跨頁面,使用請求轉發和重定向都能帶資料
作用域範圍由小到大:頁面<請求<會話<上下文
知識點3:請求、會話、上下文中存放、修改、删除資料方法
請求接口、會話接口、上下文接口中都定義相同的方法,可以對資料進行存放、修改、删除
多數伺服器中,都是使用Map對象來實作不同範圍的屬性
知識點4:頁面作用域範圍
(1)pageContex.setAttribute(name,value)
(2)目前頁面pageContext.getAttribute(name)
(3)請求轉發到page2.jsp,pageContext.getAttribute(name)
<%
//向頁面作用域添加屬性和值
pageContext.setAttribute("name", "lisi");
request.getRequestDispatcher("/page2.jsp").forward(request, response);
// response.sendRedirect(request.getContextPath()+"/page2.jsp");
%>
<%=pageContext.getAttribute("name")%>
結果:
null
知識點5:請求作用域範圍
(1)request.setAttribute(name,value)
(2)目前頁面request.getAttribute(name)
(3)請求轉發到page2.jsp,request.getAttribute(name)
<%
//向頁面作用域添加屬性和值
// pageContext.setAttribute("name", "lisi");
//request作用域添加屬性和值
request.setAttribute("name", "lisi");
request.getRequestDispatcher("/page2.jsp").forward(request, response);
// response.sendRedirect(request.getContextPath()+"/page2.jsp");
%>
<%=request.getAttribute("name")%>
結果:
lisi
知識點6:上下文作用域範圍
(1)application.setAttribute(name,value)
(2)目前頁面application.getAttribute(name)
(3)請求轉發或者重定向到page2.jsp,application.getAttribute(name)
<%
//向頁面作用域添加屬性和值
// pageContext.setAttribute("name", "lisi");
//request作用域添加屬性和值
// request.setAttribute("name", "lisi");
//session作用域添加屬性和值
// session.setAttribute("name", "wangwu");
//上下文作用域添加屬性和值
application.setAttribute("name", "zhaoliu");
request.getRequestDispatcher("/page2.jsp").forward(request, response);
// response.sendRedirect(request.getContextPath()+"/page2.jsp");
%>
<%=application.getAttribute("name")%>
結果:
zhaoliu
第三節:ServletConfig接口
知識點1:得到servletConfig對象
getServletConfig()得到servletConfig對象
知識點2:getServletName()
servletConfig對象.getServletName()得到Servlet的名稱
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 得到ServletConfgi對象
ServletConfig servletConfig = getServletConfig();
//得到servlet名稱
String servletName = servletConfig.getServletName();
//列印
System.out.println(servletName);
response.getWriter().append("Served at: ").append(request.getContextPath());
}
TestConfigServlet
知識點3:getServletContext()得到ServletContext對象
//得到上下文對象
ServletContext servletContext = servletConfig.getServletContext();
//列印
System.out.println(servletContext);
[email protected]
知識點4:getInitParameter(name)得到servlet的初始化參數
//得到servlet的初始化參數
String name = servletConfig.getInitParameter("name");
System.out.println(name);
第四節:ServletConfig與ServletContext的差別
1.ServletConfig是針對隻有一個特定的servlet的局部變量,
2.ServletContext是整個web application的全局變量ServletContext存放在ServletConfig中