目录
- 目录
- ServletConfig对象
- ServletContext对象
- ServletContext对象实现多个Servlet数据共享
- 获取Web应用的初始化参数
- 实现Servlet的转发
- 利用ServletContext对象读取资源文件
- 在非Servlet程序中读取资源文件
ServletConfig对象
ServletConfig对象,用于封装Servlet的配置信息。
当Servlet在Web.xml中配置初始化参数后,Web容器在创建Servlet实例对象时,会自动将这些初始化参数封装到ServletConfig对象中,并在调用Servlet的init方法时,将ServletConfig对象传递给Servlet。进而,程序员通过ServletConfig对象就可以得到当前Servlet的初始化参数。
Servlet配置初始化参数,如:
<servlet>
<servlet-name>ServletDemo4</servlet-name>
<servlet-class>com.wm103.ServletDemo4</servlet-class>
<init-param>
<param-name>username</param-name>
<param-value>DreamBoy</param-value>
</init-param>
<init-param>
<param-name>password</param-name>
<param-value>123456</param-value>
</init-param>
</servlet>
ServletDemo4.java 获取Servlet的配置参数:
package com.wm103;
import javax.servlet.ServletConfig;
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.util.Enumeration;
/**
* Created by DreamBoy on 2017/4/27.
*/
/**
* ServletConfig对象,用于封装Servlet的配置信息
*/
public class ServletDemo4 extends HttpServlet {
/*private ServletConfig config;
@Override
public void init(ServletConfig config) {
this.config = config;
}*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletConfig config = this.getServletConfig();
String value = config.getInitParameter("username");
System.out.println(value);
Enumeration<String> e = config.getInitParameterNames();
while (e.hasMoreElements()) {
String name = e.nextElement();
String val = config.getInitParameter(name);
System.out.println(val);
}
}
}
注解方式如下:
package com.wm103;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
/**
* Created by DreamBoy on 2018/3/3.
*/
@WebServlet(name = "ServletDemo2", urlPatterns = {"/ServletDemo2"}, initParams = {
@WebInitParam(name = "data1", value = "data1value"),
@WebInitParam(name = "data2", value = "data2value"),
@WebInitParam(name = "data3", value = "data3value")
})
public class ServletDemo2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletConfig config = this.getServletConfig();
Enumeration<String> e = config.getInitParameterNames();
while (e.hasMoreElements()) {
String name = e.nextElement();
String value = config.getInitParameter(name);
System.out.println(name + "=" + value);
}
}
}
ServletContext对象
- Web容器在启动时,它会为每个Web应用程序都创建一个对应的ServletContext对象,它代表当前Web应用。Web容器关闭或Web应用被删除时,对应的Web应用的ServletContext对象将被销毁。
- ServletConfig对象中维护了ServletContext对象的引用,开发人员在编写Servlet时,可以通过ServletConfig.getServletContext方法获取ServletContext对象。
package com.wm103;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* Created by DreamBoy on 2017/4/27.
*/
@WebServlet(name = "ServletDemo5", urlPatterns={"/ServletDemo5"})
public class ServletDemo5 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取ServletContext对象 1:
// ServletContext context = this.getServletConfig().getServletContext();
// 获取ServletContext对象 2:
ServletContext context = this.getServletContext();
response.getWriter().println("This is ServletDemo5.");
}
}
ServletContext对象实现多个Servlet数据共享
由于一个Web应用中的所有Servlet共享同一个ServletContext对象,因此Servlet对象之间可以通过ServletContext对象来实现通讯。ServletContext对象通常也被称之为context域对象。
通过ServletContext对象的setAttribute
方法设置共享的数据;通过ServletContext对象的getAttribute
方法获取共享的数据;通过ServletContext对象的removeAttribute
方法删除共享的数据。
获取Web应用的初始化参数
在web.xml文件中配置Web应用的初始化参数:
<!-- 为整个Web应用配置初始化参数 -->
<context-param>
<param-name>data1</param-name>
<param-value>data1value</param-value>
</context-param>
<context-param>
<param-name>data2</param-name>
<param-value>data2value</param-value>
</context-param>
在Servlet中获取Web应用的初始化参数:
package com.wm103;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
/**
* Created by DreamBoy on 2017/4/27.
*/
/**
* 获取Web应用的初始化参数
*/
@WebServlet(name = "ServletDemo6", urlPatterns={"/ServletDemo6"})
public class ServletDemo6 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = this.getServletContext();
String value = context.getInitParameter("data1");
System.out.println(value);
Enumeration<String> en = context.getInitParameterNames();
while (en.hasMoreElements()) {
String name = en.nextElement();
String val = context.getInitParameter(name);
System.out.println("name: " + name + ", value: " + val);
}
}
}
实现Servlet的转发
RequestDispatcher rd = this.getServletContext().getRequestDispatcher("/1.jsp");
rd.forward(request, response);
利用ServletContext对象读取资源文件
如读取 .properties文件。在Web工程的src创建db.properties文件,内容如下:
url=jdbc:mysql://localhost:3306/test
username=root
password=root
使用ServletContext对象读取db.properties文件的内容:
package com.wm103;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
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.io.InputStream;
import java.util.Properties;
/**
* Created by DreamBoy on 2017/4/27.
*/
/**
* 通过ServletContext读取资源文件
*/
@WebServlet(name = "ServletDemo8", urlPatterns={"/ServletDemo8"})
public class ServletDemo8 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
test2();
}
/**
* 读取资源文件
* @throws IOException
*/
private void test1() throws IOException {
InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.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);
}
/**
* 通过ServletContext的getRealPath得到资源的绝对路径后,再通过读取资源文件
* @throws IOException
*/
private void test2() throws IOException {
String path = this.getServletContext().getRealPath("/WEB-INF/classes/db.properties");
System.out.println(path);
String filename = path.substring(path.lastIndexOf("\\") + 1);
System.out.println(filename);
FileInputStream in = new FileInputStream(path);
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);
}
}
在非Servlet程序中读取资源文件
通过类装载器的方式,读取资源文件。示例:
UserDao.java
package com.wm103.dao;
/**
* Created by DreamBoy on 2017/4/28.
*/
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* 如果读取资源文件的程序不是servlet的话,就只能通过类装载器进行读取了
* 通过类装载器读取资源文件,资源文件不能太大!!!
*/
public class UserDao {
private static Properties dbconfig = new Properties();
// 静态代码块
static {
try {
// 获取当前类的类装载器
// 通过类装载器加载,资源文件只被装载一次。当我们修改/WEB-INF/classes/db.properties文件时,读取结果仍是修改前的结果。
InputStream in = UserDao.class.getClassLoader().getResourceAsStream("db.properties");
dbconfig.load(in);
} catch (IOException e) {
throw new ExceptionInInitializerError(e);
}
}
public void update() throws IOException {
// 以下代码虽然可以读取资源文件的数据,但是无法获取更新后的数据。
// 通过类装载器加载,资源文件只被装载一次。当我们修改/WEB-INF/classes/db.properties文件时,读取结果仍是修改前的结果。
/*InputStream in = UserDao.class.getClassLoader().getResourceAsStream("db.properties");
dbconfig.load(in);*/
// 通过类装载的方式得到资源文件的位置,再通过传统方式读取资源文件的数据,这样可以读取到更新后的数据。
// 通过这样的方式,Web应用运行过程中,修改/WEB-INF/classes/db.properties文件后,将读取到修改后的内容。
/*String path = UserDao.class.getClassLoader().getResource("db.properties").getPath();
FileInputStream in = new FileInputStream(path);
dbconfig.load(in);*/
String url = dbconfig.getProperty("url");
String username = dbconfig.getProperty("username");
String password = dbconfig.getProperty("password");
System.out.println(url);
System.out.println(username);
System.out.println(password);
}
public void find() {
}
public void delete() {
}
}