一、简介
功能:在一个ServletContext中保存的变量,可以在另一个ServletContext中取到
实验:
建立两个Servlet类,在第一个中设置值,在第二个中取值(要先执行设置值的Servlet)
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
String username = "年年";
context.setAttribute("username",username);
}
}
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 getContext extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
String username = (String) context.getAttribute("username");
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
resp.getWriter().print("名字"+username);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<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对应一个mapping-->
<servlet>
<servlet-name>helloServlet</servlet-name>
<servlet-class>HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloServlet</servlet-name>
<!--请求路径-->
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>getc</servlet-name>
<servlet-class>getContext</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>getc</servlet-name>
<!--请求路径-->
<url-pattern>/getc</url-pattern>
</servlet-mapping>
</web-app>
二、详解三个内置方法
Ctrl + Alt + 鼠标左键可以快速查看源代码
新建对象
![](https://img-blog.csdnimg.cn/20210621152100228.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Zsb3JhY3V1,size_16,color_FFFFFF,t_70)
(1)获取初始化参数
web.xml
java类
![](https://img-blog.csdnimg.cn/20210621151741539.png)
(2)请求转发(一般不用此方法,用request)
转发到/gp,但浏览器中的地址不会改变,仍旧显示原先的地址
![](https://img-blog.csdnimg.cn/20210621151741538.png)
![](https://img-blog.csdnimg.cn/20210621151741568.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Zsb3JhY3V1,size_16,color_FFFFFF,t_70)
(3)获取资源propertise
![](https://img-blog.csdnimg.cn/20210621152331277.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Zsb3JhY3V1,size_16,color_FFFFFF,t_70)
![](https://img-blog.csdnimg.cn/202106211531185.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Zsb3JhY3V1,size_16,color_FFFFFF,t_70)
读取资源
//读取资源
InputStreamis=this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
//加载资源
Propertiesprop=newProperties();
prop.load(is);
Stringuser=prop.getProperty("username");
Stringpwd=prop.getProperty("password");
resp.getWriter().print(user+":"+pwd);
如果properties文件非要不写在resource文件夹下,可能打包时会出错,需要在pom.xml文件中加入一段代码:(因为我的没出错,无法实验)
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>**/*.properties</exclude>
<exclude>**/*.xml</exclude>
</excludes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
下一篇:JavaWeb学习笔记(五)HttpServletRequest和HttpServletResponse