(JavaWeb)ServletContext对象

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

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值