ServletContext
Web容器在启动的时候,它会为每个web程序都创建一个对应的ServletContext对象,它代表了当前的web应用
提示:Servlet之间要通信就必须经过一个中间商,ServletContext就是这个中间商,就是一个在中间的媒介
1.共享数据
我在这个Servlet保存的数据,可以在另外一个Servlet拿到(这就需要ServletContext来做到),如图所示:
对上图的解释:
servlet1,servlet2,servlet3之间是独立的,互不影响
Servlet1将数据存放到ServletContext进行保存,然后Servlet2和Servlet3再到ServletContext去取这些数据,必须得先访问servlet1的路径,然后servlet2和servlet3在进行访问,不然浏览器请求的时候会出现错误,读不出数据,会显示null
三部曲:
1创建一个要在ServletContext上放置数据的类
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//this.ServletContext(); --> Servlet上下文
ServletContext context = this.getServletContext();
String username = "李四";
//将一个数据保存在ServletContext中,名字为:username,值为:username
context.setAttribute("username", username);
}
}
2.在创建一个读取它的类
public class GetServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//与HelloServlet里面的context是同一个对象,这样才可以拿到ServletContext里面的数据
ServletContext context = this.getServletContext();
String username = (String) context.getAttribute("username");
//解决浏览器显示的乱码问题
resp.setContentType("text/html;charset=utf-8");
resp.setCharacterEncoding("utf-8");
resp.getWriter().print("名字"+username);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
3.配置web.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></servlet>和<servlet-mapping></servlet-mapping>-->
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.kuang.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>getc</servlet-name>
<servlet-class>com.kuang.servlet.GetServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>getc</servlet-name>
<url-pattern>/getc</url-pattern>
</servlet-mapping>
</web-app>
三部曲完,测试访问结果
2.获取初始化参数
在web.xml里面配置context-param
<!--配置一些web应用初始化参数-->
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
</context-param>
建一个普通java类获取初始化参数
public class ServletDemo03 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
String url = context.getInitParameter("url");
resp.getWriter().print(url);
}
3.请求转发
RequestDispatcher就是请求转发:下面的代码会直接跳转到/gp(url-pattern)下的页面
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
//RequestDispatcher requestDispatcher = context.getRequestDispatcher("/gp");//转发的请求路径
//requestDispatcher.forward(req,resp);//调用forward实现请求转发
context.getRequestDispatcher("/gp").forward(req,resp);//一次性写好,还没有返回值
}
请求转发和重定向的示意图:(上面的图是转发,下面的图是重定向)
4.读取资源文件(开发中比较常用)
Properties
- 在java目录下新建properties
- 在resources目录下新建properties
发现:都被打包到了同一个路径下:classes,我们俗称这个路径为classpath:
思路:需要一个文件流:
这里可能需要在子项目的pom.xml文件中的build中配置resources,来防止资源导出失败,如图所示:
<!--在build中配置resources , 来防止我们资源导出失败的问题-->
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
下面的例子就是访问db.properties里面的内容:username和password的值
username=root
password=123456
public class ServletDemo05 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
Properties prop = new Properties();
prop.load(is);
String user = prop.getProperty("username");
String pwd = prop.getProperty("password");
resp.getWriter().print(user+":"+pwd);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
访问测试即可ok!