ServletContext
ServletContext对象
Web容器在启动的时候,它会为每个web程序都创建一个ServletContext对象,它代表了当前的web应用
1. 共享数据
我在这个Servlet中保存的数据,可以在另外一个Servlet中使用
HelloServlet:
package com.ch1.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;
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();//创建ServletContext对象
String username = "ch1";
context.setAttribute("username",username);//将一个数据保存在了ServletContext中,名字为:username
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
GetServlet:
package com.ch1.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;
public class GetServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();//取到ServletContext对象
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);
}
}
运行时,先运行localhost:8080/hello页面,让HelloServlet执行,设置username的值;然后再运行localhost:8080/get页面,即可得到存在ServletContext中的数据,得到 名字:ch1
2. 获取初始化参数
我们可以现在web.xml文件中配置初始化参数
<!--配置一些web应用的初始化参数-->
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
</context-param>
然后可以创建一个类,来获取该初始化参数
//(部分代码)
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);
}
同样的,web.xml中添加映射路径
<servlet>
<servlet-name>url</servlet-name>
<servlet-class>com.ch1.servlet.ServletDemo03</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>url</servlet-name>
<url-pattern>/url</url-pattern>
</servlet-mapping>
最后从localhost:8080/url路径可以得到
3. 请求转发
创建一个类,来获得请求转发
public class ServletDemo04 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
context.getRequestDispatcher("/hello").forward(req,resp);
//获得/hello路径下的内容并显示,url不变
}
背景:用户想要获取Hello的资源却又无法直接调用Hello
同样的,web.xml中添加映射路径
<servlet>
<servlet-name>demo04</servlet-name>
<servlet-class>com.ch1.servlet.ServletDemo04</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>demo04</servlet-name>
<url-pattern>/demo04</url-pattern>
</servlet-mapping>
最后从localhost:8080/demo04路径可以得到localhost:8080/hello的资源
4. 读取资源文件
首先,创建一个资源项,如我们这里存放的时账号密码
然后,我们创建一个类,创建Properties对象
我们通过
InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
来获取资源项,注意:/WEB-INF/classes/db.properties为资源项目在target目录下的路径 (如果没有可以使用Maven来clean一下再连接服务器即可)
引入Properties类:
public class ServletDemo05 extends HelloServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
Properties prop = new Properties();
prop.load(is);
String user = prop.getProperty("username");
String pwd = prop.getProperty("password");
resp.getWriter().print("username:"+user);
resp.getWriter().print("\n");
resp.getWriter().print("password:"+pwd);
}
同样的,web.xml中添加映射路径
<servlet>
<servlet-name>demo05</servlet-name>
<servlet-class>com.ch1.servlet.ServletDemo05</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>demo05</servlet-name>
<url-pattern>/demo05</url-pattern>
</servlet-mapping>
最后从localhost:8080/demo05路径可以得到