1、ServletContext
web 容器在启动的时候,它会为每个 web 程序都创立一个对应的 ServletContext 对象,它代表了以后 web 利用;
1、共享数据
我在这个 Servlet 中保留的数据,能够在另一个 servlet 中拿到
例子:给 Servlet 搁置数据
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {//this.getInitParameter();// 初始化参数
//this.getServletConfig();//Servlet 配置
//this.getServletContext();//Servlet 上下文
ServletContext context = this.getServletContext();
String username = "rygar";
context.setAttribute("username",username);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {super.doPost(req, resp);
}
在另一个 Servlet 去取进去
public class GetServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {ServletContext context = this.getServletContext();
Object username = context.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 {super.doPost(req, resp);
}
}
web.xml 配置
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.jialidun.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<servlet-class>hello</servlet-class>
</servlet-mapping>
<servlet>
<servlet-name>getc</servlet-name>
<servlet-class>com.jialidun.servlet.GetServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>getc</servlet-name>
<servlet-class>getc</servlet-class>
</servlet-mapping>
2、获取初始化参数
<!-- 配置一些 web 利用初始化参数 -->
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
<context-param>
public class GetServlet 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、申请转发
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {ServletContext context = this.getServletContext();
RequestDispatcher requestDispatcher = context.getRequestDispatcher("/gp");// 转发的申请门路
requestDispatcher.forward(req,resp);// 调用 forword 实现申请转发
}
4、读取资源文件
Properties
- 在 java 目录下新建 properties
-
在 resources 目录下新建 properties
发现:都被打包到了同一个门路下:classes,咱们俗称这个门路为 classpath
思路:须要一个文件流username=root123 password=123456
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");// / 代表以后 web 我的项目 Properties prop = new Properties(); prop.load(is); String username = prop.getProperty("username"); String password = prop.getProperty("password"); resp.getWriter().print(username+":"+password); }
拜访测试后果