ServletConfig 和 ServletContext
2013-08-25 22:16
344 查看
一:ServletConfig
1、程序启动的时候可能需要提前设定一些参数(如servlet采用哪个码表,servlet连接哪个库,servlet哪个配置文件),但是在程序中写死不太好,所以可以采用在配置文件中进行配置的方式,Servlet 的配置文件中,可以使用一个或多个<init-param>标签为 Servlet配置一些初始化参数
2、当Servlet 配置了初始化参数后,web 容器在创建 Servlet 实例对象时,会自动将这些初始化参数封装到 ServletConfig 对象中,并在调用 servlet 的init 方法时,将 ServletConfig对象传给 servlet ,进而,程序员通过 ServletConfig 对象就可以得到当前 servlet 的初始化参数信息。
范例:
web.xml
二:ServletContext
1、WEB 容器在启动是,它会在每个 WEB 应用程序都创建一个对应的 ServletContext 对象,它代表当前 WEB应用,所有的Servlet共享同一个 ServletContext 对象,所以多个 Servlet 通过 ServleetContext 对象实现数据共享,ServletContext 对象,通常也被称之为 context 域对象。
2、ServletConfig对象中维护了 ServletContext 对象的应用,在编写 servlet时,可以通过 ServletConfig.getServletContext() 方法获得 ServletCotext对象
3、ServletContext 可以实现的功能:
a、获取应用的初始化参数,
b、实现Servlet 的转发,
c、利用 ServletContext 对象读取资源文件:得到文件路基你,读取资源文件的三种方式方式,.properties(属性文件)
4、除了servletContext 还有request、session、page 三个域对象,也就是全局共享的对象。
范例1、数据共享:
web.xml
setServletContextDemo.java
ServletContextDemo.java
需要先执行 SetServletContextDemo.java,这样 域对象 ServletContext 才能被赋值,便于后面 ServletContextDemo.java运行的时候取自己设定的值不为空。
范例2、实现servlet 转发
我们通过一个JSP交互页面传递过来一个参数,将这个参数交给WEB 容器处理,WEB容器处理之后返回的一定要是JSP页面的形式,浏览器才能识别,但是我们在java 语句中直接拼接一个JSP 页面是非常麻烦的一件事情,那么我们可以将那些固定的JSP代码写成一个*.jsp 文件,我们只需要将WEB容器 返回的动态的数据加入到这个“静态”的JSP页面中就可以了,这就是转发技术,在页面中接收这个WEB容器返回的参数有很多种形式,这里直接使用JAVA代码的形式,因为JSP页面中可以直接书写java代码,当然这个代码要写在“<%
…… %>”中。
web.xml
ServletForwardDemo.java
ServletForward.jsp
范例3:读取properties配置文件
一般的配置文件只有两种,xml 和 properties。当配置的数据有内在关系的时候使用xml 类型,当配置的数据没有关系的时候,使用properties 类型。下面我们来配置一个数据库的数据文件,使用properties 文件类型。
web.xml
properties 文件放置在src 根目录下,注意访问的路径一定是编译后的路径,因为WEB工程发布之后就没有src目录了,所有的代码都被编译到WEB-INF 下面的classes目录下了。
jdbc.properties
ServletPropertiesDemo.java
结果:
1、程序启动的时候可能需要提前设定一些参数(如servlet采用哪个码表,servlet连接哪个库,servlet哪个配置文件),但是在程序中写死不太好,所以可以采用在配置文件中进行配置的方式,Servlet 的配置文件中,可以使用一个或多个<init-param>标签为 Servlet配置一些初始化参数
2、当Servlet 配置了初始化参数后,web 容器在创建 Servlet 实例对象时,会自动将这些初始化参数封装到 ServletConfig 对象中,并在调用 servlet 的init 方法时,将 ServletConfig对象传给 servlet ,进而,程序员通过 ServletConfig 对象就可以得到当前 servlet 的初始化参数信息。
范例:
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <servlet> <servlet-name>ServletConfigDemo</servlet-name> <servlet-class>servletDemo.ServletConfigDemo</servlet-class> <!-- 参数 --> <init-param> <!-- 参数名称 --> <param-name>configData</param-name> <!-- 参数内容 --> <param-value>myFirstConfigData</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>ServletConfigDemo</servlet-name> <url-pattern>/ServletConfigDemo</url-pattern> </servlet-mapping> </web-app>ServletConfigDemo.java
package servletDemo; import java.io.IOException; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletConfigDemo extends HttpServlet { private ServletConfig config; // 如果是 GET 请求方式,就执行这个方法 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // configData 是在 web.xml 配置文件中配置的参数名 String value = config.getInitParameter("configData"); System.out.println(value); } // 如果是 POST 请求方式,就执行这个方法 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 当请求时 post 的时候,也是执行 get 的方法 doGet(request,response); } // 初始化方法,接收 ServletConfig 的对象,这是为了按照配置文件的信息进行初始化 public void init(ServletConfig config) throws ServletException { this.config = config; } }上面覆写了init() 方法,其实可以直接调用父类中已经实现的方法,如下所示:
package servletDemo; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletConfigDemo extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 调用父类 HttpServlet 中的方法 // 可以先取 ServletConfig,再取参数 String configParam = this.getServletConfig().getInitParameter("configData"); // 也可以直接取参数 String param = this.getInitParameter("configData"); System.out.println(param); System.out.println(configParam); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } }
二:ServletContext
1、WEB 容器在启动是,它会在每个 WEB 应用程序都创建一个对应的 ServletContext 对象,它代表当前 WEB应用,所有的Servlet共享同一个 ServletContext 对象,所以多个 Servlet 通过 ServleetContext 对象实现数据共享,ServletContext 对象,通常也被称之为 context 域对象。
2、ServletConfig对象中维护了 ServletContext 对象的应用,在编写 servlet时,可以通过 ServletConfig.getServletContext() 方法获得 ServletCotext对象
3、ServletContext 可以实现的功能:
a、获取应用的初始化参数,
b、实现Servlet 的转发,
c、利用 ServletContext 对象读取资源文件:得到文件路基你,读取资源文件的三种方式方式,.properties(属性文件)
4、除了servletContext 还有request、session、page 三个域对象,也就是全局共享的对象。
范例1、数据共享:
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <servlet> <servlet-name>ServletContextDemo</servlet-name> <servlet-class>servletDemo.ServletContextDemo</servlet-class> </servlet> <servlet> <servlet-name>SetServletContextDemo</servlet-name> <servlet-class>servletDemo.SetServletContextDemo</servlet-class> </servlet> <servlet-mapping> <servlet-name>ServletContextDemo</servlet-name> <url-pattern>/ServletContextDemo</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>SetServletContextDemo</servlet-name> <url-pattern>/SetServletContextDemo</url-pattern> </servlet-mapping> <!-- 初始化 ServletContext 参数 --> <context-param> <param-name>webContextData</param-name> <param-value>HiWebContextData</param-value> </context-param> </web-app>
setServletContextDemo.java
package servletDemo; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class SetServletContextDemo extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 设置 servletContext 内容 this.getServletContext().setAttribute("contextData", "myFirstContextData"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } }
ServletContextDemo.java
package servletDemo; import java.io.IOException; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletContextDemo extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 调用父类 HttpServlet中的方法,取得ServletContext的两种方法 // 先取 ServletConfig,再取 ServletContext ServletContext configContext = this.getServletConfig().getServletContext(); // 直接取 ServletContext ServletContext context = this.getServletContext(); // 分别打印这两种方法获取的 自己设置的参数 String configContextSetText = (String) configContext.getAttribute("contextData"); // 自己设置的参数 取值用 getAttribute String contextSetText = (String) context.getAttribute("contextData"); System.out.println(configContextSetText); System.out.println(contextSetText); // 分别打印两种方法获取的 初始化时的参数 String configContextInitText = (String) configContext.getInitParameter("webContextData"); // 初始化的参数取值 用 getInitParameter String contextInitText = (String) context.getInitParameter("webContextData"); System.out.println(configContextInitText); System.out.println(contextInitText); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } }
需要先执行 SetServletContextDemo.java,这样 域对象 ServletContext 才能被赋值,便于后面 ServletContextDemo.java运行的时候取自己设定的值不为空。
范例2、实现servlet 转发
我们通过一个JSP交互页面传递过来一个参数,将这个参数交给WEB 容器处理,WEB容器处理之后返回的一定要是JSP页面的形式,浏览器才能识别,但是我们在java 语句中直接拼接一个JSP 页面是非常麻烦的一件事情,那么我们可以将那些固定的JSP代码写成一个*.jsp 文件,我们只需要将WEB容器 返回的动态的数据加入到这个“静态”的JSP页面中就可以了,这就是转发技术,在页面中接收这个WEB容器返回的参数有很多种形式,这里直接使用JAVA代码的形式,因为JSP页面中可以直接书写java代码,当然这个代码要写在“<%
…… %>”中。
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <servlet> <servlet-name>ServletForwardDemo</servlet-name> <servlet-class>com.haizhu.reward.ServletForwardDemo</servlet-class> </servlet> <servlet-mapping> <servlet-name>ServletForwardDemo</servlet-name> <url-pattern>/ServletForwardDemo</url-pattern> </servlet-mapping> </web-app>
ServletForwardDemo.java
package com.haizhu.reward; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletForwardDemo extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 假设这个是从访问页面传递过来的参数 String data = "myStringData"; // 将这个数据封装在 ServletContext中,但是注意:在市级开发中,一定不能这么做 this.getServletContext().setAttribute("data", data); // 设置跳转路径,这样的话就可以不用拼接HTML代码了,直接将固定的代码部分卸载页面中, // 只需要将web容器中返回的参数传递给这个页面中进行显示就行了,在页面中参数的接收方法,以后再探讨,比如使用标签接收 // 本例子JSP中接收参数的方法是直接使用 java代码的形式,将java代码嵌在 “ <% …… %>” 中 RequestDispatcher rq = this.getServletContext().getRequestDispatcher("/ServletForward.jsp"); // 跳转页前有个“/”符号 rq.forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } }
ServletForward.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'ServletForward.jsp' starting page</title> </head> <body> <h1> <font color="red"> <% // 这里的 application 相当于 ServletContext String data = (String)application.getAttribute("data"); out.write(data); %> </font> </h1> </body> </html>
范例3:读取properties配置文件
一般的配置文件只有两种,xml 和 properties。当配置的数据有内在关系的时候使用xml 类型,当配置的数据没有关系的时候,使用properties 类型。下面我们来配置一个数据库的数据文件,使用properties 文件类型。
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <servlet> <servlet-name>ServletPropertiesDemo</servlet-name> <servlet-class>com.haizhu.properties.ServletPropertiesDemo</servlet-class> </servlet> <servlet-mapping> <servlet-name>ServletPropertiesDemo</servlet-name> <url-pattern>/ServletPropertiesDemo</url-pattern> </servlet-mapping> </web-app>
properties 文件放置在src 根目录下,注意访问的路径一定是编译后的路径,因为WEB工程发布之后就没有src目录了,所有的代码都被编译到WEB-INF 下面的classes目录下了。
jdbc.properties
url=jdbc:mysql://localhost:3306:test username=root password=root
ServletPropertiesDemo.java
package com.haizhu.properties; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletPropertiesDemo extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 将文件作为 流 返回,“/”代表 WEB工程,注意文件的路径 InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/jdbc.properties"); // 使用 properties 对象,将文件内容读取出来 Properties props = new Properties(); props.load(in); String url = props.getProperty("url"); String username = props.getProperty("username"); String password = props.getProperty("password"); // 控制台输出 System.out.println(url); System.out.println(username); System.out.println(password); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } }
结果:
jdbc:mysql://localhost:3306:test root root
相关文章推荐
- Servlet第二篇【Servlet调用图、Servlet细节、ServletConfig、ServletContext】
- Servlet学习笔记(二)之Servlet路径映射配置、Servlet接口、ServletConfig、ServletContext
- getServletContext() and getServletConfig()
- 05-Servletconfig&ServletCOntext
- javaweb_ServletConfig&ServletContext
- ServletConfig与ServletContext对象详解
- ServletContext与ServletConfig的深度分析
- getServletConfig()与getServletContext()
- 什么是Servlet,Servlet的生命周期、servletconfig、servletContext
- Servlet、servletConfig、ServletContext简介
- 宋利兴_笔记_ServletConfig和ServletContext
- ServletContext与ServletConfig的深度分析
- ServletContext与ServletConfig的详解及差异
- 大数据WEB阶段 (十)Response、ServletConfig、ServletContext、资源跳转三种方式
- getServletContext()以及getServletConfig() 与getServletContext() 的区别
- 黑马程序员--servletConfig和servletContext
- ServletConfig和ServletContext的区别
- 关于ServletConfig与ServletContext的访问
- ServletConfig与ServletContext对象详解
- ServletConfig和ServletContext接口