學(xué)習(xí)自孤傲蒼狼的博客園
- ServletConfig
- ServletContext
ServletConfig的學(xué)習(xí)
ServletConfig的作用是讓Servlet獲得初始化的數(shù)據(jù),在servlet的web.xml中可以通過<init-param>來為servlet配置初始化參數(shù).
<servlet>
<servlet-name>asda</servlet-name>
<servlet-class>servleta.asda</servlet-class>
<init-param>
<param-name>name</param-name>
<param-value>aaa</param-value>
</init-param>
<init-param>
<param-name>pass</param-name>
<param-value>aaaa</param-value>
</init-param>
<init-param>
<param-name>acc</param-name>
<param-value>aaaaaaa</param-value>
</init-param>
</servlet>
當(dāng)servlet配置好之后,創(chuàng)建一個servlet時會自動將這些數(shù)據(jù)初始化封裝到ServletConfig中,調(diào)用
init方法時,數(shù)據(jù)會傳給init的config參數(shù)中,通過ServletConfig的對象就可以取得數(shù)據(jù).
@WebServlet("/asda")
public class asda extends HttpServlet {
private static final long serialVersionUID = 1L;
//通過ServletConfig接收參數(shù)
private ServletConfig config;
//init-param配置的數(shù)據(jù)會通過init方法中的config參數(shù)來獲取
@Override
public void init(ServletConfig config) throws ServletException {
this.config=config;
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//獲取指定的一個數(shù)據(jù)
String paramVal=this.config.getInitParameter("name");
response.getWriter().print(paramVal);
response.getWriter().print("</hr>");
//獲取所有數(shù)據(jù)
Enumeration<String> e=config.getInitParameterNames();
while(e.hasMoreElements()) {
String name=e.nextElement();
String value=config.getInitParameter(name);
response.getWriter().println(name+"="+value);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
運行結(jié)果:
aaa
ccc=aaaaaaa
pass=aaaa
name=aaa
ServletContext的學(xué)習(xí)
ServletContext可以看做成一個被所有Servlet共享的全局容器,你可以通過Servletonfig.getServletContext獲取ServletContext對象,來實現(xiàn)servlet之間的數(shù)據(jù)共享.
ServletContext的生命周期是整個web工程
實例:兩個servlet通過ServletContext實現(xiàn)數(shù)據(jù)共享
public class ServletContextTest extends HttpServlet {
private static final long serialVersionUID = 1L;
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String data="傻狗铭乾!";
//獲取context實例
ServletContext context=this.getServletConfig().getServletContext();
//設(shè)置值到context中
context.setAttribute("data", data);
response.getWriter().println("aaa");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
public class ServletContextTest2 extends HttpServlet {
private static final long serialVersionUID = 1L;
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
//給獲取的信息設(shè)置編碼格式乳乌,這樣就不會出現(xiàn)都是耍群?沐旨?巴席?的情況
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
//獲取context的實例
ServletContext context=this.getServletContext();
//獲取“data”的數(shù)據(jù)
String data=(String) context.getAttribute("data");
response.getWriter().print(data);
response.getWriter().println("aaa");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
web工程都是在tomcat下運行的,他們生命周期的運作都是基于開啟同一個服務(wù)所產(chǎn)生的,所以運行第一個后關(guān)閉再重新開啟運行第二個,tomcat里就不再存有第一個的值,第二個就會報null.
使用init方法必須重寫寫出父類方法super.init(config);去獲取config,不然就會報空指針.
最后別忘了在web.xml下添加servlet的路徑.