ServletContext也被称为servlet上下文。WEB容器再启动时,它会为每个WEB应用程序都创建一个ServletContext对象,他代表当前web应用,并且这个对象是全局唯一,多个Servlet都共享这个对象。
功能
我们可以使用this.getServletContext()来获得。可以实现数据的共享
从上面的图中可以知道
第一步编写两个Servlet类
第一个Servlet
在这里插入代码片package org.westos.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 ServletDemo3 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html");
//通过Servlet设置一个公共属性
ServletContext servletContext = this.getServletContext();
servletContext.setAttribute("password","123456");
}
}
第二个Servlet
package org.westos.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 ServletDemo2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html");
resp.setCharacterEncoding("UTF-8");
ServletContext servletContext = this.getServletContext();
String password = (String) servletContext.getAttribute("password");
resp.getWriter().println("密码为"+password);
}
}
第二步配置web.xml
<servlet>
<servlet-name>Servlet1servlet-name>
<servlet-class>org.westos.servlet.ServletDemo2servlet-class>
servlet>
<servlet>
<servlet-name>Servlet2servlet-name>
<servlet-class>org.westos.servlet.ServletDemo3servlet-class>
servlet>
<servlet-mapping>
<servlet-name>Servlet1servlet-name>
<url-pattern>/q2url-pattern>
servlet-mapping>
<servlet-mapping>
<servlet-name>Servlet2servlet-name>
<url-pattern>/q3url-pattern>
servlet-mapping>
第三部,先运行ServletDemo,然后再运行ServletDemo2
结果为:
第一步
首先再src目录下建立一个resources包,里面写入配置文件
driver=com.mysql.jdbc.Driver
username=root
password=123456
url=jdbc:mysql://localhost:3306/school
第二步
编写Servlet类
package org.westos.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class ServletDemo1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//读取properties配置文件
//获取配置文件路径
String realPath = this.getServletContext().getRealPath("/WEB-INF/classes/resources/database.properties");
Properties properties = new Properties();
//获取文件输入流
FileInputStream is= new FileInputStream(realPath);
//加载文件流
properties.load(is);
String driver = properties.getProperty("driver");
String username = properties.getProperty("username");
String password = properties.getProperty("password");
String url = properties.getProperty("url");
//响应到网页
resp.getWriter().println(driver);
resp.getWriter().println(username);
resp.getWriter().println(password);
resp.getWriter().println(url);
System.out.println("控制它输出为:");
System.out.println(driver);
System.out.println(username);
System.out.println(password);
System.out.println(url);
}
}
第三步
配置web.xml
<servlet>
<servlet-name>Servletservlet-name>
<servlet-class>org.westos.servlet.ServletDemo1servlet-class>
servlet>
<servlet-mapping>
<servlet-name>Servletservlet-name>
<url-pattern>/q1url-pattern>
servlet-mapping>