- 1、ServletContext介绍
- 2、servletContext常见操作
- 1、共享数据
- 2、获取初始化参数
- 3、请求转发
- 4、读取资源文件
前言:此文为javaweb学习笔记,仅供参考
ServletContext介绍及常见操作
1、ServletContext介绍servletContext,是Servlet中最大的一个接口,呈现了web应用的Servlet视图。
当web容器启动时,每个应用都会有一个ServletContext对象与之关联,当容器分布在多个虚拟机上时,web应用在所分布的每个虚拟机上都拥有一个ServletContext实例,每个实例只代表当前的web应用。如图所示。
通过对servletcontext接口查看源码,里面有许多的方法,以下方法举例一般用到以下的get方法。
| 返回值类型 | 方法 | 描述 |
|---|---|---|
| String | getInitParameter(String name) | 根据初始化参数名 name,返回对应的初始化参数值。 |
| Enumeration | getInitParameterNames() | 返回 Web 应用所有上下文初始化参数名的枚举集合,如果该 Web 应用没有上下文初始化参数,则返回一个空的枚举集合。 |
| Object | getAttribute(String var1) | 从 ServletContext 的域属性空间中获取指定名称的数据。 |
| RequestDispatcher | getRequestDispatcher(String var1) | 这个方法将请求从一个 Servlet or JSP目标资源 上 转发到服务器上的另一个资源(servlet、JSP 文件或 HTML 文件,这些资源必须是当前Web上下文中的),让其它的资源去生成响应数据。 |
| InputStream | getResourceAsStream(String var1) | 根据参数给定路径,查找文件资源,并且进行加载为字节输入流,通常配合Properties类来获取当前路径下传来的信息。 |
2、servletContext常见操作 1、共享数据
在一个Servlet中保存的数据,可以在另一个servlet中拿到
public class HelloServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
String username = "李思强";
context.setAttribute("username",username);//将一个数据保存在了ServletContext中,名字为:username,值为:username
System.out.println("hello");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
public class GetServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
String username = (String) context.getAttribute("username");
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
resp.getWriter().println("名字:"+username);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
web.xml中hello com.cetus.servlet.HelloServlet hello /hello getc com.cetus.servlet.GetServlet getc /getc
测试访问结果。需要先对访问/hello,此时servletcontext容器里的Attribute才赋值成功,才能获得到/getc下的内容。
2、获取初始化参数
url jdbc:mysql://localhost:3306/mybatis gp com.cetus.servlet.ServletDemo03 gp /gp sd4 com.cetus.servlet.ServletDemo04 sd4 /sd4
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);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
3、请求转发
public class ServletDemo04 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
System.out.println("进入了de");
RequestDispatcher requestDispatcher = context.getRequestDispatcher("/gp"); //转发的请求路经
requestDispatcher.forward(req,resp); //调用forward实现请求转发;
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
转发是服务器行为,重定向是浏览器行为,请求转发在浏览器的url输入框不会发生改变。
properties
- 在java目录下新建properties(此处需要在pom.xml中配置Module,包含java路径)
- 在resources目录下新建properties
发现:都被打包到了同一个路径下:classes。我们俗称这个路径为classpath。
思路:需要一个文件流;
静态资源 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 {
super.doGet(req, resp);
}
}
sd5 com.cetus.servlet.ServletDemo05 sd5 /sd5
测试即可。



