ServletContext
时间:2021-05-24 22:44:23
收藏:0
阅读:29
ServletContext
-
web容器在启动的时候,它会为每个web程序都创建一个对应的ServletContext对象,它代表了当前的web应用
-
共享数据:在Servlet-1中设置保存的数据,可以在Servlet-02中拿到
-
用于设置参数的Servlet类-HelloServlet
@WebServlet("/hello") public class HelloServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //this.getInitParameter() 初始化参数 //this.getServletConfig() Servlet配置 //this.getServletContext() Servlet上下文 ServletContext servletContext = this.getServletContext(); servletContext.setAttribute("name","saxon"); System.out.println("hello :HelloServlet"); } }
-
用于获取参数的Servlet类-GetServlet
@WebServlet("/get") public class GetServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //解决中文乱码 resp.setContentType("text/html"); resp.setCharacterEncoding("utf-8"); ServletContext servletContext = this.getServletContext(); String username = (String) servletContext.getAttribute("name"); resp.getWriter().write(username); System.out.println("hello GetServlet"); } }
必须先访问hello接口,然后访问get接口,才能获取name的值。
-
获取初始化参数
-
web.xml中配置
<!--配置一些web应用初始化参数--> <context-param> <param-name>url</param-name> <param-value>jdbc:mysql://localhost:3306/test</param-value> </context-param>
@WebServlet("/demo01")
public class ServletDemo01 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);
}
}
请求转发:
-
该类实现了请求的转发,转发至了/url这个映射路径
-
@WebServlet("/demo02") public class DemoServlet02 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext servletContext = getServletContext(); //请求转发 servletContext.getRequestDispatcher("/demo1").forward(req, resp); // RequestDispatcher requestDispatcher = req.getRequestDispatcher("/demo1"); // requestDispatcher.forward(req,resp); } }
读取资源文件
Properties
- 在java目录下新建properties
- 在resources目录下新建properties
发现:都被打包到同一个路径下:classes,俗称为classpath:
思路:需要一个文件流
- 获取properties文件的Servlet类-PropertiesServlet
@WebServlet("/propertiesServlet")
public class PropertiesServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
Properties properties = new Properties();
properties.load(is);
String username = properties.getProperty("username");
String password = properties.getProperty("psw");
resp.getWriter().write("username="+username);
resp.getWriter().write("psw="+password);
}
}
-
properties文件(此处放置在resources目录下)
username = root psw = 123456
然后测试访问即可。http://localhost:8080/servlet02_war/propertiesServlet
原文:https://www.cnblogs.com/saxonsong/p/14805147.html
评论(0)