JavaWeb_ServletContext 上下文对象
web容器在启动是,它会为每个web程序都建立一个对应的servletContext对象,它代表了当前的web应用;
1. 共享数据
hello.class里将username传入服务器的ServletContext中
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
Object obj = context.getAttribute("username");
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
resp.getWriter().print("名字为"+obj.toString());
}
在get.class中调用服务器ServletContext中的数据,得注意,hello没有运行,那么服务器中的数据上传,先运行get会报错
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("Hello,王大帅!");
/**
* 将一个数据宝尊在了ServletContext中,名字为username
*/
//this表示HttpServlet以及其父类包含的方法
ServletContext context = this.getServletContext();
String username = "建圣"; //数据,也可以是对象
context.setAttribute("username",username);//设置一个键值对
}
index.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0"
metadata-complete="true">
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>com.wjs.Servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>getServlet</servlet-name>
<servlet-class>com.wjs.Servlet.getServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>getServlet</servlet-name>
<url-pattern>/get</url-pattern>
</servlet-mapping>
</web-app>
2.获取初始化参数
//配置初始化参数
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
</context-param>
<servlet>
<servlet-name>demo03</servlet-name>
<servlet-class>com.wjs.Servlet.ServletDemo03</servlet-class>
</servlet>
//映射
<servlet-mapping>
<servlet-name>demo03</servlet-name>
<url-pattern>/gc</url-pattern>
</servlet-mapping>
/**
*查询初始化参数
*/
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取context
ServletContext context = this.getServletContext();
//查询初始化参数
String url = context.getInitParameter("url");
resp.getWriter().print(url);
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
}
3.请求转发
ServletContext context = this.getServletContext();
//请求转发,将当前页面转发到指定的/gp页面下
//RequestDispatcher requestDispatcher = context.getRequestDispatcher("/gp");
//requestDispatcher.forward(req,resp);
context.getRequestDispatcher("/gp").forward(req,resp);
4.读取资源文件
Properties
- 在resources目录下创建properties
- 在java目录下创建properties
- 在java下创建,由于maven的约定大于配置,里面的properties文件无法导出,需要加入build代码,网上看
发现:都被打包到了同一个路径下:classes,我们俗称为classPath
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Properties properties = new Properties();
//因为外部服务器锁定不了绝地地址
//properties.load(new FileReader(""));
ServletContext context = this.getServletContext();
//获取字节流的方法,默认主
InputStream in = context.getResourceAsStream("/WEB-INF/classes/db.properties");
properties.load(in);
resp.getWriter().print(properties.getProperty("username"));
//加了反而乱码
// resp.setContentType("text/html");
// resp.setCharacterEncoding("utf-8");
}
username = 王建圣
password = 123456
context.getResourceAsStream默认的文件夹位置在项目servlet的snapshot(简介)