Web容器在启动的时候,它会为每个Web程序都创建一个对应的ServletContext对象,它代表了当前的Web应用
应用:
1 共享数据
在某个Servlet中保存的数据,可以在其他Servlet中读取到
HelloServlet
package com.hao.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 {
//this.getInitParameter(); 初始化参数
//this.getServletConfig(); Servlet配置
//this.getServletContext(); Servlet上下文
ServletContext servletContext = this.getServletContext();
String username = "源浩";
servletContext.setAttribute("username",username);//将一个数据保存在ServletContext中,名字为username,值为源浩
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
GetServlet
package com.hao.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 servletContext = this.getServletContext();
String username = (String) servletContext.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);
}
}
注册Servlet 配置映射
<!--注册Servlet-->
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.hao.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.hao.servlet.GetServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>get</servlet-name>
<url-pattern>/get</url-pattern>
</servlet-mapping>
测试访问结果(先访问hello,将数据存入ServletContext,再访问get取出数据)
2
获取初始化参数
在web.xml中配置初始化参数
<!--配置一些Web应用的初始化参数-->
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql//localhost:3306/mybatis</param-value>
</context-param>
注册Servlet
<servlet>
<servlet-name>getParameter</servlet-name>
<servlet-class>com.hao.servlet.ServletDemo03</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>getParameter</servlet-name>
<url-pattern>/getParameter</url-pattern>
</servlet-mapping>
ServletDemo03
package com.hao.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 ServletDemo03 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext = this.getServletContext();
String url = servletContext.getInitParameter("url");
resp.getWriter().print(url);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
测试访问
3 请求转发
ServletDemo04
package com.hao.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 ServletDemo04 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext = this.getServletContext();
System.out.println("进入了ServletDemo04");
servletContext.getRequestDispatcher("/getParameter").forward(req,resp);//调用forward实现请求转发
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
注册Servlet
<servlet>
<servlet-name>ServletDemo04</servlet-name>
<servlet-class>com.hao.servlet.ServletDemo04</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletDemo04</servlet-name>
<url-pattern>/ServletDemo04</url-pattern>
</servlet-mapping>
测试访问
4 读取资源文件
Properties
1)在Java目录下新建properties
2)在resources目录下新建properties
发现:都被打包到同一个路径下:classes(俗称classpath)
db.properties
username=root
password=123456
ServletDemo05
package com.hao.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class ServletDemo05 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
InputStream resourceAsStream = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
Properties properties = new Properties();
properties.load(resourceAsStream);
String username = properties.getProperty("username");
String password = properties.getProperty("password");
resp.getWriter().print(username+":"+password);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
注册Servlet
<servlet>
<servlet-name>ServletDemo05</servlet-name>
<servlet-class>com.hao.servlet.ServletDemo05</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletDemo05</servlet-name>
<url-pattern>/ServletDemo05</url-pattern>
</servlet-mapping>
访问测试