ServletContext:Servlet上下文,代表目前整個應用程式。在jsp中就是application。
一、概述
ServletContext:Servlet上下文。
當WEB伺服器啟動時,會為每一個WEB應用程式(webapps下的每個目錄就是一個應用程式)建立一塊共享的存儲區域
ServletContext也叫做“公共區域”,也就是同一個WEB應用程式中,所有的Servlet和JSP都可以共享同一個區域。
ServletContext在WEB伺服器啟動時建立,伺服器關閉時銷毀。
二、相關方法的使用
@WebServlet(name = "ApplicationServlet", value = "/as")
public class ApplicationServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//擷取servletcontext(application對象)
//方式1【推薦】
ServletContext application1 = this.getServletContext();
//方式2
ServletContext application2 = request.getServletContext();
//方式3
ServletContext application3 = this.getServletConfig().getServletContext();
//方式4
ServletContext application4 = request.getSession().getServletContext();
//共享區域,可以儲存共享資訊
application1.setAttribute("appname", "個人網站");
application1.setAttribute("version", "2.0");
application1.setAttribute("author", "張三");
//在其他servlet都可以讀到
Object appname = application1.getAttribute("appname");
System.out.println(appname);
//擷取真實路徑,Out檔案夾下的真實目錄【重要】
String realPath = application1.getRealPath("/");
//System.out.println(realPath);
//擷取其他資訊
//伺服器資訊
String serverInfo = application1.getServerInfo();
//擷取上下文路徑【重點,應用程式名稱】
String contextPath = application1.getContextPath();
//列印結果:/0902web1,這是固定的位置,可以用作動态的跟路徑
System.out.println(contextPath);
//用于重定向,兩種書寫方式,擷取該路徑
//注意該路徑是/0902web1,隻有重定向在其後加上"/islogin"可以通路,但是轉發的"/"包含了一次"/0902web1"不能直接追加
//request.getRequestDispatcher("/islogin").forward(request, response);
response.sendRedirect(this.getServletContext().getContextPath() + "/islogin");
response.sendRedirect(request.getContextPath() + "/islogin");
//通過xml配置servlet上下文參數,共享資料,讀取
System.out.println(application1.getInitParameter("appname1"));
System.out.println(application1.getInitParameter("version1"));
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
在xml檔案中配置servlet上下文參數
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!--配置servlet上下文參數-->
<context-param>
<param-name>appname1</param-name>
<param-value>購物網站</param-value>
</context-param>
<context-param>
<param-name>version1</param-name>
<param-value>3.0</param-value>
</context-param>
</web-app>
三、案例——ServletContext統計Servlet通路次數
@WebServlet(name = "CountServlet", value = "/countservlet")
public class CountServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
ServletContext application = this.getServletContext();
Integer count;
//上鎖,處理并發
synchronized (this) {
count = (Integer) application.getAttribute("count");
if (count == null) {
count = 1; //第一次指派
} else {
count++;
}
application.setAttribute("count", count);
}
System.out.println("通路次數:" + count);
response.getWriter().println("<h2>目前通路次數:</h2>" + count);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}