ServletContext对象
ServletContext对象,,,,,
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);
}



浙公网安备 33010602011771号