ServletContext
1、共享数据2、获取初始化参数3、请求转发4、读取资源文件
ServletContext1、共享数据web容器在启动的时候,它会为每个web程序都创建一个对应的ServletContext对象,它代表了当前的web应用;
先写一个servlet实现类HelloServlet,给他一个servletContext容器,去存放一些数据
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username="你好,汤姆!";
ServletContext context = this.getServletContext();//获取到一个容器
//这个setAttribute方法就将一个值保存在了ServletContext容器中,名字为:Username,值为username="你好,汤姆!"
//然后再去写一个Servlet实现类去获取到它
context.setAttribute("Username",username);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
再写一个servlet实现类GetServlet,同样也给他一个servletContext容器,去获取到上面容器中存放的数据
public class GetServlet 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);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
然后最后一步,要去web.xml文件中去配置两个类的映射
hello
com.liu.servlet.HelloServlet
hello
/hello
getcon
com.liu.servlet.GetServlet
getcon
/getcon
测试运行
2、获取初始化参数直接去请求GetServlet,发现返回一个null
所以这里先要去请求HelloServlet存放数据,才能够去获取数据
这样就ok了
配置一些初始化参数
url jdbc:mysql://localhost:3306/mybatis
Servlet实现类去获得初始化参数
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);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
web.xml中去配置一下这个类的映射
sd3
com.liu.servlet.ServletDemo03
sd3
/url
`
测试运行
3、请求转发1、还是先写一个servlet实现类
public class ServletDemo04 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
//请求转发,将/url页面上的内容转发到当前页面上
RequestDispatcher requestDispatcher = context.getRequestDispatcher("/url");
requestDispatcher.forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
2、web.xml中去配置该类的映射
sd4
com.liu.servlet.ServletDemo04
sd4
/sd4
3、测试运行
4、读取资源文件可以看到这里我们访问的路径是/sd4,显示的是转发过来的/url的内容
先写一个文件放在maven-web项目的resources文件夹下
#文件内容 username=root password=123456
但是在web项目下我们无法直接去读取这个文件,要看在服务器启动后,这个文件会被加载到target文件夹下的哪个位置
2、启动服务器
可以看到,该文件被放到了项目下的"/WEB-INF/classes/db.properties",这时候我们就可以去读取这个文件了
3、新建一个Servlet实现类
public class ServletDemo05 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext = this.getServletContext();//获取到一个ServletContext容器
InputStream resourceAsStream = servletContext.getResourceAsStream("/WEB-INF/classes/db.properties");//将文件路径生成一个流
Properties properties = new Properties();//新建一个properties对象
properties.load(resourceAsStream);//去加载这个流
String username = properties.getProperty("username");
String pwd = properties.getProperty("password");
resp.getWriter().print(username+":"+pwd);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
4、web.xml下去配置该类的映射
sd5
com.liu.servlet.ServletDemo05
sd5
/sd5
*5、重新启动服务器 !



