Servlet(三)ServletContext
ServletContextd对象:
web容器在启动的时候,它会为每个web程序都创建一个对象的ServletContext对象,它代表了当前的web应用;
-
共享数据
我在一个Servlet中保存的数据,可以在另一个Servlet中拿到
实际开发时,少用servletContext,用session
//放置数据的类 public class HelloServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // this.getInitParameter(); 初始化参数 // this.getServletConfig(); Servlet配置 // this.getServletContext(); Servlet上下文 ServletContext servletContext = this.getServletContext(); String username = "困了就睡"; servletContext.setAttribute("username",username);//将一个数据保存到ServletContext中,名字:username,值:username,看成键值对的形式 } }
//读取数据的类 public class GetServlet extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext servletContext = this.getServletContext(); String username = (String) servletContext.getAttribute("username"); resp.setCharacterEncoding("utf-8"); resp.setContentType("text/html"); resp.getWriter().print(username); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
//配置xml <servlet> <servlet-name>hello</servlet-name> <servlet-class>com.godwin.servlet.HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> <servlet> <servlet-name>get</servlet-name> <servlet-class>com.godwin.servlet.GetServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>get</servlet-name> <url-pattern>/get</url-pattern> </servlet-mapping>
测试访问时,
注意:读取数据的类,记得解决乱码问题
resp.setCharacterEncoding("utf-8"); resp.setContentType("text/html");
第一次访问http://localhost:8080/s2/get,页面显示为null,
接着访问http://localhost:8080/s2/hello,页面为空,但是拿了数据
在访问http://localhost:8080/s2/get,拿到数据,并且页面显示“困了就睡”,即设置好的数据
ServletContextd应用
获取初始化参数
实际开发中,几乎不用这么做
web.xml
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
</context-param>
<servlet>
<servlet-name>gp</servlet-name>
<servlet-class>com.godwin.servlet.ServletDemo01</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>gp</servlet-name>
<url-pattern>/gp</url-pattern>
</servlet-mapping>
public class ServletDemo01 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);
}
}
请求转发
实际开发中,我们用Request去做
请求转发时,路径不会变
web.xml
<servlet>
<servlet-name>tp</servlet-name>
<servlet-class>com.godwin.servlet.ServletDemo02</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>tp</servlet-name>
<url-pattern>/tp</url-pattern>
</servlet-mapping>
public class ServletDemo02 extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
// RequestDispatcher requestDispatcher = context.getRequestDispatcher("/gp");//转发的请求路径
// requestDispatcher.forward(req,resp);//调用forward实现请求转发
context.getRequestDispatcher("/gp").forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
读取资源文件
实际开发中,用类加载或者反射等去做,几乎不用这个方式去做
Properties:
- 在java目录下新建properties
- 在resources目录下新建properties
发现都被打包到同一路径下:classes,俗称:类路径 。
思路:需要一个文件流
解决资源导出问题:
<!--在build中配置resources,来防止我们资源导出失败的问题-->
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
public class ServletDemo03 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/dp.properties");
Properties prop = new Properties();
prop.load(is);
String username = prop.getProperty("username");
String password = prop.getProperty("password");
resp.getWriter().print(username + ":" + password);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
<servlet>
<servlet-name>sd3</servlet-name>
<servlet-class>com.godwin.servlet.ServletDemo03</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>sd3</servlet-name>
<url-pattern>/sd3</url-pattern>
</servlet-mapping>
username=root
password=root
复习笔记资料参考B站UP主狂神说