【学习笔记】ServletContext对象
ServletContext对象:web容器启动的时候,它会为每个web程序都创建一个对应的ServletContext对象,它代表了当前的web应用。
ServletContext 有很多功能:
共享数据
共享数据的意思是 在一个Servlet中保存的数据,可以在另一个Servlet中,读取到这个数据
举例:
我们创建两个Servlet ,一个用来放数据,一个用来取数据
放数据:
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
public class SetContext extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
String username = "用户名";
//将数据保存在ServletContext中,用键值对的方式,前面是键,键是String类型,后面是值,值是Object类型
context.setAttribute("username",username);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
取数据:
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
public class GetContext extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置网页字符编码和显示类型
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
//ServletConstext 对象是共享的,只有一个
ServletContext context = this.getServletContext();
//从ServletContext中读取数据,需要输入键
String username = (String)context.getAttribute("username"); //返回的是Object类型,需要强转
resp.getWriter().print("名字为:"+username);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
配置:
<servlet>
<servlet-name>setContext</servlet-name>
<servlet-class>SetContext</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>setContext</servlet-name>
<url-pattern>/set</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>getContext</servlet-name>
<servlet-class>GetContext</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>getContext</servlet-name>
<url-pattern>/get</url-pattern>
</servlet-mapping>
必须先访问 localhost:8080/s2/set
然后访问 localhost:8080/s2/get
因为如果不放数据就去取的话,就会返回null
获取初始化参数
在web.xml中配置初始化参数,使用context-param 标签
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/school</param-value>
</context-param>
调用ServletContext对象的getInitParameter方法,传入在配置文件中的参数的name
package com.wang.context;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
public class Context01 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);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
请求转发
-
我们新建一个Servlet,使它转发到 Context01,打印出初始化参数
-
首先调用 ServletContext 的 getRequestDispatcher() 方法,参数就是要转发的页面的路径,返回值为RequestDispatcher
-
然后调用RequestDispatcher 对象的forword()方法,参数为请求参数和响应参数,实现请求转发
package com.wang.context;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
public class Context02 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
RequestDispatcher requestDispatcher = context.getRequestDispatcher("/getParam"); //转发请求路径
requestDispatcher.forward(req,resp); //forword 实现请求转发
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
注册配置:
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>com.wang.context.Context02</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/dis</url-pattern>
</servlet-mapping>
访问:
我们发现访问的是 /dis,但内容是 /getParam ,这就是请求转发
读取资源文件
在resources文件夹下新建一个properties文件
username=zhangsan
password=123456
执行后,在target文件夹中,该项目的文件夹下(servlet-02)的WEB-INF下的classes文件夹中会有该配置文件,classes文件夹称为classpath
读取配置文件:
-
调用 ServletContext对象的getResourceAsStream() 方法,参数为配置文件的路径,返回值是输入流.
-
new 一个 Properties 对象,调用load()方法,参数为输入流。
-
调用Properties对象的 getProperties() 方法,获得配置文件中的内容,参数为配置文件中的值
package com.wang.context;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class Context03 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
Properties prop = new Properties();
prop.load(is);
String username = prop.getProperty("username");
String password = prop.getProperty("password");
resp.getWriter().print("用户名:"+ username + "\n" + "密码:" + password);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
需要注意的问题:
如果配置文件,没有放在resources 文件夹下,而是放在了java 文件夹下,执行后,有可能读取不到该配置文件,在classes文件夹下,也不会有该配置文件
我们需要在pom.xml 下配置
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>**/*.properties</exclude>
<exclude>**/*.xml</exclude>
</excludes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
保证它能够找到 配置文件,运行后,在classes文件下的项目文件中就有该配置文件了