首页 > 其他 > 详细

ServletContext

时间:2021-05-24 22:44:23      阅读:29      评论:0      收藏:0      [点我收藏+]

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);
    }
}

ServletContext

原文:https://www.cnblogs.com/saxonsong/p/14805147.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!