ServletContext
概念:
ServletContext是Servlet上下文对象
- 代表当前项目对象,运行在Tomcat中的每个项目都有一个对应的对象,称为上下文对象。
- 是一个接口,由Tomcat去实现,我们使用这个对象即可。定义了一套Servlet与Web容器之间进行交互的方法。
- 主要作用:
1) 获得当前Web项目中的资源
2) 获得全局的初始配置参数
3) 是一个上下文作用域对象
如何得到ServletContext:
- ServletContext getServletContext()
- 返回了一个上下文对象
Servlet的继承结构
因为ServletContext继承了GenericServlet,可以直接调用方法getServletContext()
获取当前工程的资源文件
ServletContext的方法
InputStream getResourceAsStream(String path)
- 读取项目根目录及其子目录下(开发路径WebRoot)的资源文件,以输入流的方式返回
- String getRealPath(String path)
- 得到资源部署到服务器上的真实地址
示例:
读取WEB-INF路径下的属性文件,向浏览器输出属性文件的company.properties中的name、address的键和值内容
PrintWriter out = response.getWriter();
// 1. 得到ServletContext对象
ServletContext application = getServletContext();
//2. 读取资源文件
InputStream inputStream = application.getResourceAsStream("/WEB-INF/company.properties");
//如果没有这个文件会怎么样? inputStream会返回null
//3. 加载到属性对象
Properties info = new Properties();
//把资源加载到info中
info.load(inputStream);
//4. 输出到浏览器上
out.print("公司名:" + info.getProperty("name") + "<br/>");
out.print("地址:" + info.getProperty("address") + "<br/>");
properties文件
name=\u4F20\u667A\u64AD\u5BA2
address=\u5929\u6CB3\u533A
结果:
WEB-INF下的资源是否可以直接访问呢?
为了安全起见,WEB-INF目录下的资源不能通过浏览器直接访问。如果要保护的文件,放在WEB-INF目录下。不然报404错误。
示范:
得到指定资源的真实地址,并且输出
//1. 得到ServletContext
ServletContext application = getServletContext();
//2. 调用方法得到资源的真实地址
// 如果这个资源不存在?会怎么样?资源不存在,也不受影响,但后面的代码会有影响。
//只是相当于做了一个拼接
String path = application.getRealPath("/WEB-INF/company.properties");
FileInputStream inStream = new FileInputStream(path);
//显示内容
Properties info = new Properties();
info.load(inStream);
out.print("资源的真实地址:" + path + "<br/>");
//输出到浏览器上
out.print("公司名:" + info.getProperty("name") + "<br/>");
out.print("地址:" + info.getProperty("address") + "<br/>");
inStream.close();
获取全局的配置参数
在ServletConfig中也有同名的方法
String getInitParameter(“参数名”)
- 得到全局的配置参数
- Enumeration getInitParameterNames()
- 得到所有的全局配置参数的名字
ServletConfig的配置:
初始的参数是局部变量,只能给某个Servlet来使用
局部参数:<init-param>
<servlet>
<servlet-name>Demo3ConfigServlet</servlet-name>
<servlet-class>com.itheima.servlet.Demo3ConfigServlet</servlet-class>
<!-- 配置初始的参数 -->
<init-param>
<param-name>name</param-name>
<param-value>Jack</param-value>
</init-param>
<init-param>
<param-name>age</param-name>
<param-value>22</param-value>
</init-param>
</servlet>
ServletContext的配置
全局参数:<context-param>
<!-- 配置全局的参数 -->
<context-param>
<param-name>name</param-name>
<param-value>Rose</param-value>
</context-param>
<context-param>
<param-name>age</param-name>
<param-value>18</param-value>
</context-param>
上下文域
作用域对象的功能:不同的Servlet这间共享数据
上下文域的作用范围:
所有的用户,所有的请求都可以共享同一个数据,只要Web容器((服务器)不关闭,上下文域中的数据就一直存在。
void setAttribute(“键”,Object数据)
- 向上下文域中添加数据
Object getAttribute(“键”)
- 从上下文域中取出指定的数据
- void removeAttribute(“键”)
- 删除一个键和值
案例:得到当前是第几个登录的用户
分析过程
1.在LoginServlet的init()方法中创建count=0,并且将值放入上下文域中.
2.在登录成功的代码中从上下文域中取出count,并且加1,再更新上下文域中的值。
3.跳转到另一个CountServlet,在另一个CountServlet中取出上下文域中的值,显示在页面上。
代码:
LoginServlet:
/**
* 1. 判断是否登录成功
* 2. 如果成功,从上下文域中取出值,加1,放回到上下文域中
* @author NewBoy
*
*/
public class LoginServlet extends HttpServlet {
//init()方法中创建count=0,并且将值放入上下文域中
@Override
public void init() throws ServletException {
int count = 0;
getServletContext().setAttribute("count", count);
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//得到上下文对象
ServletContext application = getServletContext();
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
// 得到用户名和密码
String name = request.getParameter("name");
String password = request.getParameter("password");
//判断是否登录成功
if ("admin".equals(name) && "123".equals(password)) {
//如果成功,从上下文域中取出值,加1,放回到上下文域中
int count = (Integer) application.getAttribute("count");
application.setAttribute("count", ++count);
//跳转到另一个servlet并且显示登录的人数
response.sendRedirect(request.getContextPath() + "/count");
}
else {
out.print("<h3>登录失败</h3>");
out.print("<a href='login.html'>重新登录</a>");
}
}
}
CountServlet:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.print("<h1>当前登录的人数是:" + getServletContext().getAttribute("count") + "</h1>");
out.close();
}
登陆表单.html
<!DOCTYPE html>
<html>
<head>
<title>登录页面</title>
<meta charset="UTF-8">
</head>
<body>
<h2>用户登录</h2>
<form action="login" method="post">
用户名: <input type="text" name="name"/><br/>
密码: <input type="password" name="password"/><br/>
<input type="submit" value="登录"/>
</form>
</body>
</html>