概述
Servlet 上下文对象 – 项目的管家
WEB容器在启动时,它都会为每个WEB应用程序都创建一个对应的ServletContext对象,代表当前web应用
每个web工程都只有一个ServletContext对象。
不管在哪个servlet里面,获取到的这个类的对象都是同一个。
1:创建
服务器启动时候,tomcat为每一个web程序(项目)创建一个域对象ServletContext
–>管理整个项目的空间
在每一个servlet中,都有servlet
2.销毁
正常关闭服务器,或者移除项目
ServletContext 何时创建, 何时销毁?
创建: 服务器启动的时候,会为托管的每一个web应用程序,创建一个ServletContext对象
销毁: 从服务器移除托管,或者是关闭服务器。
ServletContext 的作用范围
–> 仅作用于同一个项目下
作用
1.用来实现多个servlet之间的额数据共享
2.获取项目下的资源文件
servlet中访问的项目资源,最好应该是发布路径的!
只要能知道该项目发布之后,这个文件的真实磁盘目录即可!
ServletContext.getRealPath(以web为基准的路径);
3.支持获取文件类型(原来比较重要,现在版本升高tomcat进化了)
4.获取全局的配置参数
如何得到对象
ServletConfig对象中维护了ServletContent对象的引用,开发人员编写servlet时
//1. 获取对象
ServletContext context = ServletConfig.getServletContext();
应用场景
获取全局配置参数
ServletContext context = ServletConfig.getServletContext();
String 变量名 = context.getInitParameter(“参数名”);
参数名
值
获取局部配置参数
oneServlet
cc.iservlet.oneServlet
lll
0348989
oneServlet
/one
Servlet的转发
可以获取Web应用中的资源
- 获取资源在tomcat里面的绝对路径
先得到路径,然后自己new InpuStream
context.getRealPath(“”) //这里得到的是项目在tomcat里面的根目录。
D:\tomcat\apache-tomcat-7.0.52\apache-tomcat-7.0.52\wtpwebapps\Demo03\
String path = context.getRealPath(“file/config.properties”);//这里获取的是全部路径
D:\tomcat\apache-tomcat-7.0.52\apache-tomcat-7.0.52\wtpwebapps\Demo03\file\config.properties
- getResourceAsStream 获取资源流对象
直接给相对的路径,然后获取流对象。
工程目录:D:\tomcat\apache-tomcat-7.0.52\apache-tomcat-7.0.52\wtpwebapps\Demo03\
绝对目录:D:\tomcat\apache-tomcat-7.0.52\apache-tomcat-7.0.52\wtpwebapps\Demo03\file\config.properties
相对路径:file\config.properties
代码实例
//获取web工程下的资源.转化成流对象,(前面隐藏当前工程的目录)
InputStream in = this.getServletContext().getResourceAsStream(“db.properties”);
//创建配置文件对象
Properties props = new Properties();
//加载配置文件
props.load(in);
//获取配置文件内部内容
String url = props.getProperty(“url”);
String username = props.getProperty(“username”);
String password = props.getProperty(“password”);
System.out.println(url+username+password);
}
通过classloader去获取web工程下的资源
//如果读取资源文件的程序不是servlet的话,只能通过类加载器来读
//该方法不能读取更新后的数据
//inputStream in = 类名.class.getclassloader().getResourceAsStream(“资源文件”)
//第二种
//类装载方式读取位置,通过传统方式获取更新后的数据
String path = 类名.class.getclassloader().getresource(“db.properties”).getPath();
FileInputStream in = new FileInputStream(path);
….–>加载配置文件/获取配置文件内容
classloader默认路径是
必须回到Demo03这个路径下,才能进入file目录
使用ServletContext存取数据。
context域对象
–> 一个web应用的所有servlet共享一个servletContext对象,所以可实现数据共享,servletContext对象通常被称为context域对象.
–> 这是一个容器,该容器作用范围也就是应用程序范围
如:聊天室
案例
定义一个登陆的html页面, 定义一个form表单
定义一个Servlet,名为LoginServlet
针对成功或者失败,进行判断,然后跳转到不一样的网页
LoginServlet
–> 判断登录的servlet(判断条件)
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取页面输入的value信息
String userName = request.getParameter(“username”);
String password = request.getParameter(“password”);
System.out.println(“username==” + userName + “,password” + password);
//获取页面输出打印流
PrintWriter pw = response.getWriter();
if(“admin”.equals(userName) && “123”.equals(password)) {
//System.out.println(“登录成功”);
pw.write(“login succse”);
//设置状态码,重新定位状态码
response.setStatus(302);
//定位位置是跳转到哪一个页面
response.setHeader(“Location”, “login_success.html”);
}else {
// System.out.println(“登录失败”);
pw.write(“login fail…”);
}
}
ServletContext存取值分析
CountServlet
–> 反馈数据的servlet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//取值
int count =(int) getServletContext().getAttribute(“count”);
//输出到界面
response.getWriter().write(“当前网站成功登录的总次数为:”+count);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
login.html配置
Servlet的路径
http://localhost:8080/Demo4/login
当前这个html的路径:
http://localhost:8080/Demo4/login.html
密码: