1.简介:
servletContext接口是Servlet中最大的一个接口,呈现了web应用的Servlet视图。ServletContext实例是通过 getServletContext()方法获得的,由于HttpServlet继承GenericServlet的关系,GenericServlet类和HttpServlet类同时具有该方法。每个应用都会有一个ServletContext对象与之关联,当容器分布在多个虚拟机上时,web应用在所分布的每个虚拟机上都拥有一个ServletContext实例。缺省情况下,ServletContext不是分布式的,并且只存在于一个虚拟机上。
2.共享数据:
//存数据
public class ServletTest01 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("test01");
//this.getInitParameter() 初始化参数
//this.getServletConfig() servlet配置
//this.getServletContext() servlet上下文
ServletContext servletContext = this.getServletContext();
String username="秦疆";
servletContext.setAttribute("username",username);//将一个数据保存在了ServletContext中,键值对username:"秦疆"
}
}
//取数据
public class ServletTest02 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext = this.getServletContext();
String username = (String) servletContext.getAttribute("username");
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
resp.getWriter().print("名字:"+username);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
<!--web.xml中是配置我们web的核心应用-->
<!--注册Servlet-->
<servlet>
<servlet-name>test01</servlet-name>
<servlet-class>com.beyond.servlet.ServletTest01</servlet-class>
</servlet>
<!--一个Servlet对应一个mapping(映射)-->
<servlet-mapping>
<servlet-name>test01</servlet-name>
<url-pattern>/test01</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>test02</servlet-name>
<servlet-class>com.beyond.servlet.ServletTest02</servlet-class>
</servlet>
<!--一个Servlet对应一个mapping(映射)-->
<servlet-mapping>
<servlet-name>test02</servlet-name>
<url-pattern>/test02</url-pattern>
</servlet-mapping>
3.获取初始化参数
<!--配置一些web应用初始化参数-->
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
</context-param>
public class ServletTest03 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext = this.getServletContext();
String url = servletContext.getInitParameter("url");
resp.getWriter().print(url);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
4.请求转发
public class ServletTest04 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext = this.getServletContext();
// RequestDispatcher requestDispatcher = servletContext.getRequestDispatcher("/test03");//转发的请求路径
// requestDispatcher.forward(req,resp);//调用forward方法实现请求转发
servletContext.getRequestDispatcher("/test03").forward(req,resp);
System.out.println("进入了ServletTest04");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
5.读取资源文件
- classpath:
- 在java目录下建立的资源文件和在resources目录下建立的资源文件,最终都会被打包到target/servlet-项目-SNAPSHOT/WEB-INF/classes目录下,这几个目录都被称为classpath。
public class PropertiesTest05 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/com/beyond/servlet/test05.properties");
InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/pt5.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);
}
}