ServletContext
web容器在启动的时候,它会为每个web程序都创建一个对应的ServletContext对象,它代表了当前的web应用。
共享数据
每个servlet共享一个servletcontext,我在这个Servlet中保存的数据,可以在另外一个servlet中拿到;
示例:
我们先在HelloServlet中存一个数据username
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 context = this.getServletContext();
String username = "wx"; //数据
context.setAttribute("username",username);
//将一个数据保存在了ServletContext中,名字为:username 值 username
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
在GetServlet里获取HelloServlet里存的数据
public class GetServlet extends HelloServlet{
@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中注册servlet
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.wx.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.wx.servlet.GetServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>get</servlet-name>
<url-pattern>/get</url-pattern>
</servlet-mapping>
然后运行Tomcat
先访问GetServlet
我们发现获取到的username是null,因为我们并没有访问HelloServlet存参数
那我们先访问HelloServlet再访问GetServlet
获取初始化参数
现在web.xml中配置一些初始化参数
<!--配置一些web应用初始化参数-->
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
</context-param>
新建一个servlet来获取初始化参数
public class ServletDemo03 extends HelloServlet{
@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中注册这个servlet
<servlet>
<servlet-name>d3</servlet-name>
<servlet-class>com.wx.servlet.ServletDemo03</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>d3</servlet-name>
<url-pattern>/d3</url-pattern>
</servlet-mapping>
启动Tomcat
请求转发
通过一个servlet来访问另一个servlet
public class ServletDemo04 extends HelloServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
System.out.println("进入了ServletDemo04");
//RequestDispatcher requestDispatcher =context.getRequestDispatcher("/gp"); //转发的请求路径
//requestDispatcher.forward(req,resp); //调用forward实现请求转发;
context.getRequestDispatcher("/d3").forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
注册这个ServletDemo04后启动Tomcat
我们在地址栏访问的是ServletDemo04,内容显示的是ServletDemo03,这里就是将请求转发到ServletDemo03中
读取资源文件
Properties
首先在子servlet的pom中加上
<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>
然后在java目录下新建properties
然后输入username=root
写好servlet
public class ServletDemo05 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/com/wx/servlet/aa.properties");
Properties prop = new Properties();
prop.load(is);
String user = prop.getProperty("username");
resp.getWriter().print(user);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
然后注册servlet,启动Tomcat,然后就会在target文件夹中生成properties文件,如果没有在pom文件中配置,则properties文件不会生成
然后访问ServletDemo05就会读取资源文件
在resources目录下新建properties
接下来的操作和上面一样
只需要修改servlet里的路径就可以了
public class ServletDemo05 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/s.properties");
Properties prop = new Properties();
prop.load(is);
String user = prop.getProperty("username");
resp.getWriter().print(user);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
发现:都被打包到了同一个路径下:classes,我们俗称这个路径为classpath: