0%

ServletContext

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

ServletContext

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

1
2
3
//        this.getInitParameter()   初始化参数
// this.getServletConfig() Servlet配置
// this.getServletContext() Servlet上下文(获取当前web应用程序ServletContext对象)

ServletContext对象可以做什么呢?(都更好的替代方法,了解即可)

  • 共享数据

    • 我在一个Servlet类实例中保存的数据,可以在另一个Servlet类实例中拿到。
    • setAttributegetAttribute
    • Attribute,属性;性质;特征
  • 获取初始化参数

    • 获取提前配置在web.xml中的数据
    • getInitParametergetInitParameterNames
    • param,参数
  • 请求转发

    • 请求转发,浏览器地址栏路径不变
    • 路由重定向,地址会改变
  • 读取资源文件

    • 后缀.properties
    • resources bundle,资源包(文件)

共享数据

我在一个Servlet类实例中保存的数据,可以在另一个Servlet类实例中拿到。(有更好的替代方法,使用session

代码演示

新建两个Servlet类,HelloServlet与GetServletContext。

保存数据

类HelloServlet,将数据保存到ServletContext对象中。用到ServletContext类的setAttribute方法,可理解为键值对的形式保存数据。(Attribute,属性;性质;特征)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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();

String username = "qsdbl";//数据
//setAttribute,设置属性。将一个数据保存在了ServletContext中,名字为:username,值为username字符串变量的值"qsdbl"
servletContext.setAttribute("username",username);//可理解为键值对

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

获取数据

类GetServletContext,从ServletContext对象中获取数据。用到ServletContext类的getAttribute方法,可理解为键值对数据通过键名获取值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class GetServletContext extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext = this.getServletContext();
String username = (String) servletContext.getAttribute("username");
resp.setContentType("text/html;charset=utf-8");//告诉浏览器,数据类型和编码类型
//通过名字username,获取保存在ServletContext对象中的值然后发送给浏览器
resp.getWriter().print("<h1>"+"用户名:"+username+"</h1>");
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);//post请求,也放到doGet方法中处理
}
}

注册Servlet

为什么要注册Servlet,怎么注册Servlet,点这里:传送门。这里HelloServlet与GetServletContext的请求路径分别配置为/hello/gets

测试

测试之前需要设置Tomcat服务器,关于Tomcat服务器的配置,点这里:传送门。(若是该web应用程序配置过了则不需要重复配置)

点击IDEA工具栏的绿色三角形启动web应用程序。web应用程序启动后打开的页面是index.jsp文件中的hello world(默认的)。

我们访问获取数据的Servlet。在地址栏的后边,添加上在注册Servlet时绑定的请求路径gets,点击回车键。此时获取到的用户名是null,这是因为保存数据的Servlet还未执行过还没有往ServletContext对象中存储名为username的数据。

现在去访问添加(保存)数据的Servlet。将地址栏中新添的请求路径更改成hello,点击回车键。

再次去访问获取数据的Servlet。将地址栏中添加的请求路径更改回gets,点击回车键。这时获取到了保存在ServletContext对象中的数据。即通过ServletContext对象实现了数据共享

获取初始化参数

获取提前配置在web.xml中的数据。通过ServletContext对象,获取配置在web.xml中的数据。使用方法getInitParametergetInitParameterNames,前者返回字符串类型数据后者返回的是枚举类型数据。在web.xml中配置初始化参数:

1
2
3
4
<context-param>
<param-name>参数名</param-name>
<param-value>参数值</param-value>
</context-param>

代码演示

在web.xml中添加数据。参数名为url,参数值为jdbc:mysql://localhost:3306/mybatis

1
2
3
4
5
<!--配置一些web应用程序初始化参数-->
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
</context-param>

类ServletDemo03,获取配置在web.xml中的数据url。(别忘了注册servlet,这里ServletDemo03的请求路径配置为/getinit。)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class ServletDemo03 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext = this.getServletContext();
String url = (String) servletContext.getInitParameter("url");//获取自己配置的 初始化 参数(在web.xml文件中)

//通过名字username,获取保存在ServletContext对象中的值然后发送给浏览器
resp.getWriter().print(url);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);//post请求,也放到doGet方法中处理
}
}

运行结果(别忘了配置Tomcat,若是该web应用程序配置过了则不需要重复配置):

请求转发

通过ServletContext对象,实现请求转发(request对象也可以,使用方法相似)。使用ServletContext对象的方法getRequestDispatcher与RequestDispatcher对象的方法forward前者的形参为转发的路径,后者的形参为HttpServletRequest对象与HttpServletResponse对象。(将ServletContext对象换成HttpServletRequest对象也可以实现请求转发)

关于转发的路径

  • 请求转发:映射的地址包括当前web应用程序路径(内部资源跳转)

  • 重定向:映射的地址不包括当前web应用程序路径

类ServletDemo04,演示请求转发功能。当ServletDemo04接收到请求(get、post请求)后,将请求转发给ServletDemo03(其绑定的请求路径为/getinit),ServletDemo03会获取名为url的初始化参数值(见上一个例子)。即当访问ServletDemo04时,会被转发给ServletDemo03。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class ServletDemo04 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext = this.getServletContext();
// RequestDispatcher requestDispatcher = servletContext.getRequestDispatcher("/getinit");//转发的请求路径
// requestDispatcher.forward(req,resp);//调用forward方法,实现请求转发
servletContext.getRequestDispatcher("/getinit").forward(req,resp);

}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);//post请求,也放到doGet方法中处理
}
}

别忘了注册servlet,这里ServletDemo04的请求路径配置为/demo04

别忘了配置Tomcat,若是该web应用程序配置过了则不需要重复配置。

运行结果:

读取资源文件

资源文件后缀.properties,一般放在resources文件夹内。

通过ServletContext对象,读取Properties资源文件。使用ServletContext对象的方法getResourceAsStream将资源文件转换为输入流,使用Properties对象的方法load加载输入流,再使用方法getProperty通过键名获取值(键值对方式存储的数据)。

类ServletDemo05,演示读取资源文件。读取用户名和密码,并发送给浏览器。注意:打包后的web应用程序结构与开发时的项目结构是不一样的,资源文件和java代码文件会放到classpath下,classpath = /WEB-INF/classes(进一步了解程序结构,点这里)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class ServletDemo05 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String classpath = "/WEB-INF/classes/";
//通过ServletContext对象,加载Properties资源文件
InputStream resourceAsStream = this.getServletContext().getResourceAsStream(classpath+"db.properties");

Properties prop = new Properties();
prop.load(resourceAsStream);//将文件以流的形式加载到该Properties对象中

String user = prop.getProperty("username");//读取数据(键值对,通过键名获取值)
String pwd = prop.getProperty("passwd");

resp.setContentType("text/html;charset=utf-8");//设置文件类型,编码类型
resp.getWriter().print("姓名:"+user+"<br/>密码:"+pwd);//响应数据给浏览器
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);//post请求,也放到doGet方法中处理
}

在resources文件夹中新建一个名为“db.properties”的资源文件。

别忘了注册servlet,这里ServletDemo05的请求路径配置为/demo05

别忘了配置Tomcat,若是该web应用程序配置过了则不需要重复配置。

运行结果:

若图片不能正常显示,请在浏览器中打开

欢迎关注我的其它发布渠道