- 1. 简 介
- 2. 应 用
- 2.1 共享数据
- 2.2 获取初始化参数
- 2.3 请求转发
- 2.4 读取资源文件
ServletContext ,全局共享的上下文,项目中所有 Servlet 都可以访问到它,可以把它理解为一个共享的存储空间,在 Java 中以接口(javax.servlet.ServletContext)的形式存在。
例如,Servlet A 把一些信息放到 ServletContext 中,然后 Servlet B 可以从 ServletContext 中获取到这些信息。
2. 应 用 2.1 共享数据(1)在 HelloServlet 这个类中,存入数据到 ServletContext
package com.yuhuofei.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;
/**
* @Description 继承HttpServlet,重写doGet方法
* @ClassName HelloServlet
* @Author yuhuofei
* @Date 2022/5/9 21:30
* @Version 1.0
*/
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//向ServletContext放入指定的字段信息
ServletContext servletContext = this.getServletContext();
servletContext.setAttribute("userName","yuhuofei2021");
}
}
(2)在 GetServletInfo 这个类中,获取前面存入的信息,并打印出来
package com.yuhuofei.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;
/**
* @Description
* @ClassName GetServletInfo
* @Author yuhuofei
* @Date 2022/5/11 23:10
* @Version 1.0
*/
public class GetServletInfo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//从ServletContext中获取指定的字段
ServletContext servletContext = this.getServletContext();
String userName = (String) servletContext.getAttribute("userName");
//设置内容类型
resp.setContentType("text/html");
//设置编码格式
resp.setCharacterEncoding("utf-8");
//输出页面
resp.getWriter().print(userName);
}
}
(3)web.xml 的配置如下
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>Setservlet-name>
<servlet-class>com.yuhuofei.servlet.HelloServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>Setservlet-name>
<url-pattern>/doSeturl-pattern>
servlet-mapping>
<servlet>
<servlet-name>Getservlet-name>
<servlet-class>com.yuhuofei.servlet.GetServletInfoservlet-class>
servlet>
<servlet-mapping>
<servlet-name>Getservlet-name>
<url-pattern>/doGeturl-pattern>
servlet-mapping>
web-app>
(4)启动 tomgcat 服务器,先访问 http://localhost:8080/01-servlet/doSet ,在访问 http://localhost:8080/01-servlet/doGet ,结果如下
(1)改造 GetServletInfo 类
package com.yuhuofei.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;
/**
* @Description
* @ClassName GetServletInfo
* @Author yuhuofei
* @Date 2022/5/11 23:10
* @Version 1.0
*/
public class GetServletInfo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取初始化参数
ServletContext servletContext = this.getServletContext();
String initParameter = servletContext.getInitParameter("mysql-url");
//设置内容类型
resp.setContentType("text/html");
//设置编码格式
resp.setCharacterEncoding("utf-8");
//输出页面
resp.getWriter().print(initParameter);
}
}
(2)改造 web.xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<context-param>
<param-name>mysql-urlparam-name>
<param-value>jdbc:mysql://localhost:3306/javaparam-value>
context-param>
<servlet>
<servlet-name>GetInitParameterservlet-name>
<servlet-class>com.yuhuofei.servlet.GetServletInfoservlet-class>
servlet>
<servlet-mapping>
<servlet-name>GetInitParameterservlet-name>
<url-pattern>/doGeturl-pattern>
servlet-mapping>
web-app>
(3)启动服务器,然后请求 http://localhost:8080/01-servlet/doGet ,结果如下
2.3 请求转发(1)新建一个 GetForward 类,并改造 GetServletInfo 类
- GetForward.java 类
package com.yuhuofei.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;
/**
* @Description
* @ClassName GetForward
* @Author yuhuofei
* @Date 2022/5/11 23:10
* @Version 1.0
*/
public class GetForward extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//从ServletContext中获取指定的字段
ServletContext servletContext = this.getServletContext();
String userName = (String) servletContext.getAttribute("userName");
//设置内容类型
resp.setContentType("text/html");
//设置编码格式
resp.setCharacterEncoding("utf-8");
//输出页面
resp.getWriter().print(userName);
}
}
- GetServletInfo.java 类
package com.yuhuofei.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;
/**
* @Description 转发请求
* @ClassName GetServletInfo
* @Author yuhuofei
* @Date 2022/5/11 23:10
* @Version 1.0
*/
public class GetServletInfo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//转发请求
ServletContext servletContext = this.getServletContext();
servletContext.getRequestDispatcher("/doGetForward").forward(req,resp);
}
}
(2)改造 web.xml 文件
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>Setservlet-name>
<servlet-class>com.yuhuofei.servlet.HelloServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>Setservlet-name>
<url-pattern>/doSeturl-pattern>
servlet-mapping>
<servlet>
<servlet-name>GetInitParameterservlet-name>
<servlet-class>com.yuhuofei.servlet.GetServletInfoservlet-class>
servlet>
<servlet-mapping>
<servlet-name>GetInitParameterservlet-name>
<url-pattern>/doGeturl-pattern>
servlet-mapping>
<servlet>
<servlet-name>GetForwardservlet-name>
<servlet-class>com.yuhuofei.servlet.GetForwardservlet-class>
servlet>
<servlet-mapping>
<servlet-name>GetForwardservlet-name>
<url-pattern>/doGetForwardurl-pattern>
servlet-mapping>
web-app>
(3)重启服务器,先后在浏览器访问 http://localhost:8080/01-servlet/doSet 、http://localhost:8080/01-servlet/doGet、http://localhost:8080/01-servlet/doGetForward ,结果如下:
可以发现,请求 http://localhost:8080/01-servlet/doGet 的结果和请求 http://localhost:8080/01-servlet/doGetForward 是一样的,说明前者成功将请求转发到了后者。
2.4 读取资源文件在 java 目录下 新建一个 properties 文件,同时也在 resources 目录下新建一个 properties 文件,当打包项目的时候,发现这两个文件,都被打包到了 classes 路径下,这个路径被称为 classpath ,也叫做项目的类路径。
下面实现一下读取 properties 配置文件。
(1)在 resources 目录下,新建一个 database.properties ,内容如下
userName=yuhuofei
passWord=123hello
(2)改造 HelloServlet.java 文件
package com.yuhuofei.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* @Description 继承HttpServlet,重写doGet方法
* @ClassName HelloServlet
* @Author yuhuofei
* @Date 2022/5/9 21:30
* @Version 1.0
*/
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//以流的方式从配置文件中读取数据
InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/database.properties");
//加载流数据,并获取参数
Properties properties = new Properties();
properties.load(is);
String user = properties.getProperty("userName");
String passWord = properties.getProperty("passWord");
//响应输出
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html");
resp.getWriter().print(user + ":" + passWord);
}
}
(3)在 web.xml 中新增以下内容
<servlet>
<servlet-name>Propertiesservlet-name>
<servlet-class>com.yuhuofei.servlet.HelloServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>Propertiesservlet-name>
<url-pattern>/getPropertiesurl-pattern>
servlet-mapping>
(4)启动服务器,然后访问 http://localhost:8080/01-servlet/getProperties ,得到的结果
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)