1.基本概念
说起ServletContext,一些人会产生误解,以为一个servlet对应一个ServletContext。其实不是这样的,事实是一个web应用对应一个ServletContext,所以ServletContext的作用范围是整个应用,明确这点很重要,这是基础中的基础。
一个web应用对应一个ServletContext实例,这个实例是应用部署启动后,servlet容器为应用创建的。ServletContext实例包含了所有servlet共享的资源信息。通过提供一组方法给servlet使用,用来和servlet容器通讯,比如获取文件的MIME类型、分发请求、记录日志等。
这里需要注意一点,如果你的应用是分布式部署的,那么每台服务器实例上部署的应用实例都各自拥有一个ServletContext实例。
2.ServletContext应用
2.1共享数据
java代码:
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获得ServletContext对象
ServletContext servletContext = getServletContext();
//向ServletContext对象存放数据
servletContext.setAttribute("name","xulidang");
//设置编码,防止中文乱码,默认是ISO-8859-1
resp.setContentType("text/html"); //ajax异步请求为application/json格式
resp.setCharacterEncoding("utf-8");
PrintWriter out = resp.getWriter(); //响应流
//输出数据
out.print("ServletContext存放数据成功!");
}
}
public class GetServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获得ServletContext对象
ServletContext servletContext = getServletContext();
//从ServletContext对象中取出数据
String str = servletContext.getAttribute("name").toString();
//设置编码,防止中文乱码,默认是ISO-8859-1
resp.setContentType("text/html"); //ajax异步请求为application/json格式
resp.setCharacterEncoding("utf-8");
PrintWriter out = resp.getWriter(); //响应流
//输出数据
out.print(str);
}
}
xml配置代码:
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>servlets.HelloServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>get</servlet-name>
<servlet-class>servlets.GetServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>get</servlet-name>
<url-pattern>/get</url-pattern>
</servlet-mapping>
注:需先访问HelloServlet,才能通过GetServlet获得数据
2.2 获取初始化参数
java代码:
public class GetParamServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获得ServletContext对象
ServletContext servletContext = getServletContext();
//从ServletContext对象中取出数据
String str = servletContext.getInitParameter("name");
//设置编码,防止中文乱码,默认是ISO-8859-1
resp.setContentType("text/html"); //ajax异步请求为application/json格式
resp.setCharacterEncoding("utf-8");
PrintWriter out = resp.getWriter(); //响应流
//输出数据
out.print(str);
}
}
xml文件:
<context-param>
<param-name>name</param-name>
<param-value>xulidangaaaa</param-value>
</context-param>
2.3、请求转发
public class RequestDispatcherServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获得ServletContext对象
ServletContext servletContext = getServletContext();
//请求转发
servletContext.getRequestDispatcher("/getparam").forward(req,resp);
}
}
2.4、读取资源文件
test.properties内容:
java代码:
public class GetPropertiesServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获得ServletContext对象
ServletContext servletContext = getServletContext();
InputStream is = this.getServletContext().getResourceAsStream("src/main/resources/test.properties");
Properties properties = new Properties();
properties.load(is);
String name = properties.getProperty("name");
resp.getWriter().print(name);
}
}
部分内容转载至:JavaWeb–ServletContext