ServletContext

web容器启动的时候,他会为每一个web容器都创建一个对应的ServletContext,它待变了当前的Web应用。

  • 共享数据

    我在这个Servlet中保存的数据,可以在另外一个中读取到。

在这里插入图片描述

public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        this.getInitParameter() 初始化参数
//        this.getServletConfig()  servlet的配置
//        this.getServletContext()  servlet的上下文
        resp.setContentType("text/html");
        resp.setCharacterEncoding("utf-8");
        String username = "西风";
        ServletContext context = this.getServletContext();

        context.setAttribute("username",username); //将一个数据保存在ServletContext中,名字为username,只为username
        System.out.println("hello");
    }

}
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 context = this.getServletContext();
        String username = (String) context.getAttribute("username");
        resp.getWriter().print("名字"+username);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
    <servlet>
        <servlet-name>Hello</servlet-name>
        <servlet-class>org.westos.HelloServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Hello</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>getc</servlet-name>
        <servlet-class>org.westos.GetServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>getc</servlet-name>
        <url-pattern>/getc</url-pattern>
    </servlet-mapping>
参数获取getInitParameter

配置JDBC数据库

    <context-param>
        <param-name>url</param-name>
        <param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
    </context-param>

    <servlet-mapping>
        <servlet-name>geturl</servlet-name>
        <url-pattern>/geturl</url-pattern>
    </servlet-mapping>
获取web的配置信息
public class ServletDemo03 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        resp.setCharacterEncoding("utf-8");
        ServletContext conext = this.getServletContext();
        String url = conext.getInitParameter("url");
        resp.getWriter().print(url);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
请求转发

在这里插入图片描述
代码示例

public class ServletDemo04 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = this.getServletContext();
        System.out.println("进入了servletDemo04");
        RequestDispatcher requestDispatcher = context.getRequestDispatcher("/geturl");//转发请求路径
        requestDispatcher.forward(req,resp);  //调用forword实现请求转发
    }
}
读取文件

properties

  • 在java目录下新建
  • 在Resources目录下新建properties

发现都被打包到同一个路径下:这个路劲下叫做Classes 我们俗称这个路径为classpath:

思路:需要一个文件流

访问测试结果

username=root
password=Asimov
#classpath;
public class ServletDemo05 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        resp.setCharacterEncoding("utf-8");
        InputStream rs = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
        Properties properties = new Properties();
        properties.load(rs);
        String username = properties.getProperty("username");
        String password = properties.getProperty("password");
        resp.getWriter().print(username+password);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
    <servlet>
        <servlet-name>sd5</servlet-name>
        <servlet-class>org.westos.ServletDemo05</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>sd5</servlet-name>
        <url-pattern>/sd5</url-pattern>
    </servlet-mapping>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值