1. ServletConfig 接口作用和使用
ServletConfig
接口,是一个配置对象
,通过 web.xml 或 注解方式配置 Servlet 参数后,可以通过 ServletConfig 对象获取初始化参数。
1.1 ServletConfig API 详解
1 2 3 4 5 6 7 8 9 10
| public interface ServletConfig { String getServletName(); ServletContext getServletContext(); String getInitParameter(String var1); Enumeration<String> getInitParameterNames(); }
|
ServletConfig 使用:
- ServletConfig 对象是由服务器创建的,通过 Servlet 的 init 方法传递到 Servlet 中;
- 作用:
① 获取 Servlet 名称使用 getServletName();
② 获取 Servlet 初始化参数 getInitParameter(); getInitParameterNames();
③ 获取 ServletContext 对象
- 获取 ServletConfig 对象:继承 HttpServlet 后直接调用 Servlet 接口的
getServletConfig()
方法即可返回一个该对象。
代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| @WebServlet(name = "ConfigServlet", value = "/config", initParams = { @WebInitParam(name = "username", value = "root"), @WebInitParam(name = "password", value = "1234")}) public class ConfigServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletConfig servletConfig = getServletConfig(); System.out.println(servletConfig.getServletName() + "在运行..."); String username = servletConfig.getInitParameter("username"); String password = servletConfig.getInitParameter("password"); System.out.println("直接-servlet初始化参数 "+ username + ":" + password); Enumeration<String> initParameterNames = servletConfig.getInitParameterNames(); while (initParameterNames.hasMoreElements()) { String name = initParameterNames.nextElement(); String value = servletConfig.getInitParameter(name); System.out.println("动态-servlet初始化参数 "+ username + ":" + password); }
ServletContext servletContext = servletConfig.getServletContext(); }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } }
|
2. ServletContext 接口作用和使用
ServletContext
接口是一个域对象
(或叫 上下文对象),用来存储数据。
2.1 ServletContext API 详解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| public interface ServletContext { public String getContextPath(); public ServletContext getContext(String uripath); public String getMimeType(String file); public java.util.Set<E> getResourcePaths(String path); public java.net.URL getResource(String path); public java.io.InputStream getResourceAsStream(String path); public RequestDispatcher getRequestDispatcher(String path); public RequestDispatcher getNamedDispatcher(String name); public void log(String msg); public void log(String message, Throwable throwable); public String getRealPath(String path); public String getServerInfo(); public String getInitParameter(String name); public java.util.Enumeration<E> getInitParameterNames(); public Object getAttribute(String name); public java.util.Enumeration<E> getAttributeNames(); public void setAttribute(String name, Object object); public void removeAttribute(String name); public String getServletContextName(); }
|
使用:
- 当服务器启动时,会为服务器中的每一个web应用创建一个 ServletContext 对象;
- 作用:
① 实现多个 Servlet 数据共享
② 获取全局初始化参数
③ 获取资源在服务器上的真实磁盘路径
- 获取 ServletContext 对象:继承 HttpServlet 后直接调用 Servlet 接口的
getServletContext()
方法 或 servletConfig.getServletContext()
(通过 ServletConfig 实例对象) 即可返回一个该对象。
2.2 多个 Servlet 数据共享
public Object getAttribute(String name) 获取域中指定参数名称的值
public void removeAttribute(String name) 将指定的参数和值移除
public void setAttribute(String name, Object object) 存储参数和值到到域中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String msg = "hello, Servlet"; ServletConfig servletConfig = getServletConfig(); ServletContext servletContext = servletConfig.getServletContext(); servletContext.setAttribute("message", msg); ServletContext servletContextGet = getServletContext(); Object msgGet = servletContextGet.getAttribute("message"); System.out.println("其他的Servlet获取到的共享数据为:" + msgGet); servletContextGet.removeAttribute("message"); msgGet = servletContextGet.getAttribute("message"); System.out.println("其他的Servlet获取到的共享数据为:" + msgGet);
}
|
2.3 获取全局初始化参数
public String getInitParameter(String name) 获取初始化参数
public java.util.Enumeration getInitParameterNames() 获取初始化参数集合
web.xml
1 2 3 4 5 6 7 8 9 10
| <context-param> <param-name>username</param-name> <param-value>root</param-value> </context-param> <context-param> <param-name>password</param-name> <param-value>1234</param-value> </context-param>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| @WebServlet(name = "ContextServlet02", value = "/context02", initParams = { @WebInitParam(name = "username", value = "admin"), @WebInitParam(name = "password", value = "888888")}) public class ContextServlet02 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext servletContext = getServletContext(); Enumeration<String> initParameterNames = servletContext.getInitParameterNames(); while (initParameterNames.hasMoreElements()) { String name = initParameterNames.nextElement(); String value = servletContext.getInitParameter(name); System.out.println("name: " + name + ", value:" + value); } }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } }
|
2.4 获取资源在服务器上的真实磁盘路径
public String getRealPath(String path) 获取资源的真实路径
1 2 3 4 5 6 7 8 9 10 11 12 13
| @WebServlet(name = "ContextServlet03", value = "/context03") public class ContextServlet03 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext servletContext = getServletContext(); String imgPath = servletContext.getRealPath("img"); System.out.println(imgPath); }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } }
|
根据项目的当前路径 / 拼接,注意参数给的时候也是相对于 / 当前路径。
2.5 案例:统计站点访问次数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| @WebServlet(name = "VisitTimesServlet", value = "/count") public class VisitTimesServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext servletContext = getServletContext(); Integer count = (Integer) servletContext.getAttribute("count"); count = count == null ? 1 : (count += 1); servletContext.setAttribute("count", count); System.out.println("站点访问次数:" + count + "次数!"); }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } }
|