ServletContext 对象
作为域对象的作用1:
主要用于存取数据,域对象都具备存储数据
的方法。
方法摘录:
这个域对象的作用范围最大,它涵盖整个Web应用。可以将其存取数据的结构理解为一个Map
java.lang.Object
getAttribute
(java.lang.String name) //获取数据
返回具有给动名字的servlet container 的属性,或者当没有具有所给名字的属性时,返回一个空值。
java.util.Enumeration
getAttributeNames()
//获取所有的key
返回一个包含servlet context中属性名字变量的集合.
void
removeAttribute
(java.lang.String name) //移除数据
从servlet context中删除带有给定名字的属性。
void
setAttribute
(java.lang.String name, java.lang.Object object) //设置数据
绑定一个对象到在 servlet context中给定的属性名称。
如何获取该对象
由于这个对象跟我们的Web项目是一一对应的关系,所以在我们的服务器环境下,通过大多数的API类都可以直接或间接的获取到该对象。
演示HttpServlet 类中获取 ServletContext 域对象 ——> application 域对象
1. 此类有一个getServletContext
的成员方法
2. 通过ServletConfig
的getServletContext
成员方法
3. 通过HttpServletRequest
的getServletContext
成员方法
代码演示获取对象:
class ServletContextDomo extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
ServletContext servletcontext=req.getServletContext();
ServletContext servletcontext=this.getServletContext();
}
}
图解ServletContext
实战案例:
需求:
显示当前第几位访问的用户。
- 即跨 Servlet 传递数据。
方法1: 如果全部通过定义公共的成员变量的方法是可以解决跨Servlet数据传递的问题的。但是很不方便。
方法2: 又由于数据传递其实就是涉及都数据存取。所以,优先考虑SerlvetAPI中提供的“域对象”。
唯一需要把握的是选择哪种类型的域对象。
代码演示:
第一步.配置xml
<?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_4_0.xsd"
version="4.0">
<servlet-name>test1</servlet-name>
<servlet-class>com.example.servlet.Test1Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>test1</servlet-name>
<url-pattern>/test1</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>result</servlet-name>
<servlet-class>com.example.servlet.ResultServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>result</servlet-name>
<url-pattern>/result</url-pattern>
</servlet-mapping>
</web-app>
第二部.定义Test1Servlet 类继承 HttpServlet
- 先判断:说明以前没有记录
第一次记录 - 从一个Servlet 跳转到另外一个Servlet
有两种技术: 请求转发、 响应重定向,下面代码使用的是响应重定向。
public class Test1Servlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//resp
Object count = getServletContext().getAttribute("count");
if(count==null){
//说明以前没有记录
//第一次记录
getServletContext().setAttribute("count", 1);
}else{
getServletContext().setAttribute("count", ((int)count)+1);
}
/*
当用户请求这个Servlet的时候,我需要记录一个变量 int count
要想多次请求,共享此变量,不能定义为局部变量。
考虑将它定义为成员变量
*/
//先用一下: 从一个Servlet 跳转到另外一个Servlet
//有两种技术: 请求转发、 响应重定向
resp.sendRedirect(req.getContextPath()+"/result");
}
}
第三步.定义ResultServlet 类 继承 HttpServlet
实现:
- .重写doGet方法,
- 从ServletContext域对象中获取数据 如:getServletContext().getAttribute(“count”)
- 利用
字符输出流
, 向浏览器返回数据
。
public class ResultServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//直接从ServletContext域对象中获取数据
Object count = getServletContext().getAttribute("count");
//字符输出流,向浏览器返回数据的。
PrintWriter writer = resp.getWriter();
writer.write("<html><head><title>my web site</title></head><body><h1>count="+ count+" </h1></body></html>");
}
}