ServletContext
- web容器在启动的时候,它会为每个web程序都创建一个对应的ServletContext对象,它代表了当前的web应用;
1.共享数据
Servlet1可以将数据存放在ServletContext中,Servlet2和Servlet3可以在ServletContext中取得Servlet1在ServletContext中存放的数据。
在HelloServlet中存放String数据 伊泽瑞尔。
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取servletContext对象
ServletContext servletContext = this.getServletContext();
String name = "伊泽瑞尔";
//以键值对的形式将数据放入servletContext对象中
servletContext.setAttribute("name",name);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
在GetNameServlet中取得数据并显示在网站上
public class GetNameServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取ServletContext对象
ServletContext servletContext = this.getServletContext();
//获取HelloServlet中在获取ServletContext对象中存放的数据
String name = (String) servletContext.getAttribute("name");
//设置响应信息为中文
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
resp.getWriter().write(name);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
先访问HelloServlet页面将数据存放在ServletContext中,再访问GetNameServlet拿出数据并显示在网页。
2.获取初始化参数
<!--配置一些web应用初始化参数-->
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
</context-param>
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
String url = context.getInitParameter("url");
resp.getWriter().print(url);
}
3.请求转发
public class ServletDemo01 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//创建servletContext对象
ServletContext servletContext = this.getServletContext();
//请求转发
servletContext.getRequestDispatcher("/getName").
forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
运行,显示的是GetNameServlet的页面,这里null是由于没有访问hello的缘故。
显示的是GetNameServlet的页面,但是路径却还是自己的。
如图,A向B请求资源,B向C请求资源,B将C返回的资源再返回给A,A自始至终没有接触C,所以显示的还是B的路径。
4.读取资源文件
Properties
- 在resources目录下新建db.properties文件
username=ez
password=123456
public class ServletDemo02 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//创建servletContext对象
ServletContext servletContext = this.getServletContext();
//获取properties文件的字节输入流对象
InputStream is = servletContext.getResourceAsStream("/WEB-INF/classes/db.properties");
//创建properies对象
Properties prop = new Properties();
//加载流
prop.load(is);
String user = prop.getProperty("username");
String pwd = prop.getProperty("password");
//显示
resp.getWriter().write(user+":"+pwd);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
转载:https://blog.csdn.net/qq_43601784/article/details/106788546
查看评论