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

    访问测试结果


RYGAR
1 声望0 粉丝