Servlet-Context

本文详细介绍了Servlet的四大核心应用场景:通过ServletContext实现数据共享,获取web.xml中的初始化参数,使用请求转发以及读取资源文件。通过实例展示了如何在不同Servlet间传递数据,配置并读取初始化参数,以及如何进行内部请求的转发,同时讲解了如何读取项目的资源文件,如db.properties。这些内容对于理解和掌握Servlet的实用技巧至关重要。
摘要由CSDN通过智能技术生成

ServletContext

ServletContext对象

Web容器在启动的时候,它会为每个web程序都创建一个ServletContext对象,它代表了当前的web应用

1. 共享数据

我在这个Servlet中保存的数据,可以在另外一个Servlet中使用

在这里插入图片描述

HelloServlet:

package com.ch1.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = this.getServletContext();//创建ServletContext对象
        String username = "ch1";
        context.setAttribute("username",username);//将一个数据保存在了ServletContext中,名字为:username

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

GetServlet:

package com.ch1.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class GetServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = this.getServletContext();//取到ServletContext对象
        String username = (String) 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 {
        doGet(req, resp);
    }
}

运行时,先运行localhost:8080/hello页面,让HelloServlet执行,设置username的值;然后再运行localhost:8080/get页面,即可得到存在ServletContext中的数据,得到 名字:ch1

2. 获取初始化参数

我们可以现在web.xml文件中配置初始化参数

<!--配置一些web应用的初始化参数-->
    <context-param>
        <param-name>url</param-name>
        <param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
    </context-param>

然后可以创建一个类,来获取该初始化参数

//(部分代码)
public class ServletDemo03 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);
    }

同样的,web.xml中添加映射路径

    <servlet>
        <servlet-name>url</servlet-name>
        <servlet-class>com.ch1.servlet.ServletDemo03</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>url</servlet-name>
        <url-pattern>/url</url-pattern>
    </servlet-mapping>

最后从localhost:8080/url路径可以得到

在这里插入图片描述

3. 请求转发

创建一个类,来获得请求转发

public class ServletDemo04 extends HttpServlet {
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = this.getServletContext();
        context.getRequestDispatcher("/hello").forward(req,resp);
        //获得/hello路径下的内容并显示,url不变
  
    }

背景:用户想要获取Hello的资源却又无法直接调用Hello
在这里插入图片描述

同样的,web.xml中添加映射路径

	<servlet>
        <servlet-name>demo04</servlet-name>
        <servlet-class>com.ch1.servlet.ServletDemo04</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>demo04</servlet-name>
        <url-pattern>/demo04</url-pattern>
    </servlet-mapping>

最后从localhost:8080/demo04路径可以得到localhost:8080/hello的资源

4. 读取资源文件

首先,创建一个资源项,如我们这里存放的时账号密码
在这里插入图片描述

在这里插入图片描述

然后,我们创建一个类,创建Properties对象

我们通过

	InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");

来获取资源项,注意:/WEB-INF/classes/db.properties为资源项目在target目录下的路径 (如果没有可以使用Maven来clean一下再连接服务器即可)

引入Properties类:

public class ServletDemo05 extends HelloServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");

        Properties prop = new Properties();
        prop.load(is);
        String user = prop.getProperty("username");
        String pwd = prop.getProperty("password");

        resp.getWriter().print("username:"+user);
        resp.getWriter().print("\n");
        resp.getWriter().print("password:"+pwd);

    }

同样的,web.xml中添加映射路径

    <servlet>
        <servlet-name>demo05</servlet-name>
        <servlet-class>com.ch1.servlet.ServletDemo05</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>demo05</servlet-name>
        <url-pattern>/demo05</url-pattern>
    </servlet-mapping>

最后从localhost:8080/demo05路径可以得到
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值