得到ServletContext的方式:
public class ServletDemo6 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//得到ServletContext的方式1
ServletContext context = this.getServletConfig().getServletContext();
//得到ServletContext的方式2
context = this.getServletContext();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
通过ServletContext实现多个servlet的数据共享:
public class ServletDemo7 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String data = "aaa";
this.getServletContext().setAttribute("data", data);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
/**
* ServletContext域:
* 1.这是一个容器
* 2.ServletContext域这句话说明了这个容器作用范围,也就是应用程序范围
*/
//用过ServletContext实现ServletDemo7和ServletDemo8的数据共享
public class ServletDemo8 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String value = (String)this.getServletContext().getAttribute("data");
System.out.println(value);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
获得Web应用的初始化参数:
web.xml配置文件:
<context-param>
<param-name>data1</param-name>
<param-value>xxxx</param-value>
</context-param>
<context-param>
<param-name>data2</param-name>
<param-value>yyyy</param-value>
</context-param>
<context-param>
<param-name>data3</param-name>
<param-value>zzzz</param-value>
</context-param>
Servlet代码:
//获取web应用的初始化参数
public class ServletDemo9 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String value =(String)this.getServletContext().getInitParameter("data1");
response.getOutputStream().write(("<font color='red'>" + value + "</font>").getBytes());
Enumeration e = this.getServletContext().getInitParameterNames();
while(e.hasMoreElements()){
String name = (String)e.nextElement();
String value1 = (String)this.getInitParameter(name);
System.out.println(name+" = "+value1);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
实现Servlet转发:
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 '1.jsp' starting page</title>
</head>
<body>
<h1>
<font color="red">
<%
//在jsp中,application就是ServletContext
String data = (String)application.getAttribute("data");
out.write(data);
%>
</font>
</h1>
</body>
</html>
Servlet代码:
//通过ServletContext实现请求转发
public class ServletDemo10 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String data = "aaaaaaaaaa";
//把数据带给1.jsp (不能通过context域,要通过request域)
this.getServletContext().setAttribute("data", data);
//转发到1.jsp
RequestDispatcher rd = this.getServletContext().getRequestDispatcher("/1.jsp");
rd.forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
利用ServletContext读取资源文件:
db.properties属性文件内容:
url=jdbc:mysql://localhost:3306/test
username=root
password=root
Servlet代码:
//通过ServletContext读取资源文件
public class ServletDemo11 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
test5();
}
//通过servletContext的getRealPath得到资源的绝对路径后,再通过传统流读取资源文件
//此方法适用于下载,因为可以得到资源名称
private void test5() throws IOException {
String path = this.getServletContext().getRealPath("/WEB-INF/classes/db.properties");
String filename = path.substring(path.lastIndexOf("\\")+1);
System.out.println("当前读取到的资源名称是:"+filename);
FileInputStream in = new FileInputStream(path);
//java专门用来读取properties文件的对象
Properties props = new Properties();//内部用map保存数据
props.load(in);
System.out.println("当前读取到的资源数据是:");
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去读
private void test4() throws IOException {
FileInputStream in = new FileInputStream("classes/db.properties");
//java专门用来读取properties文件的对象
Properties props = new Properties();//内部用map保存数据
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);
}
//db.properties属性文件在Web应用(WebRoot)目录下
private void test3() throws IOException {
InputStream in = this.getServletContext().getResourceAsStream("/db.properties");
//java专门用来读取properties文件的对象
Properties props = new Properties();//内部用map保存数据
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);
}
//db.properties属性文件在cn.itcast包目录下
private void test2() throws IOException {
InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/cn/itcast/db.properties");
//java专门用来读取properties文件的对象
Properties props = new Properties();//内部用map保存数据
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);
}
//db.properties属性文件在src目录下
private void test1() throws IOException {
InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
//java专门用来读取properties文件的对象
Properties props = new Properties();//内部用map保存数据
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);
}
}
Servlet调用其他程序,在其他程序中通过类装载器读取资源文件(文件不能太大):
Servlet代码:
//Servlet调用其他程序,在其他程序中如何读取资源文件(通过类装载器,文件不能太大)
public class ServletDemo12 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
UserDao dao = new UserDao();
dao.update();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
//如果读取资源文件的程序不是Servlet的话,就只能通过类装载器去读了
public class UserDao {
private static Properties dbconfig = new Properties();
//静态代码块
static{
try{
InputStream in = UserDao.class.getClassLoader().getResourceAsStream("db.properties");
dbconfig.load(in);
}
catch(Exception e){
//抛出初始化错误
throw new ExceptionInInitializerError(e);
}
}
public void update(){
System.out.println(dbconfig.getProperty("url"));
}
public void find()throws IOException{
//以下代码虽然可以读取资源文件的数据,但是无法获取更新后的数据,
//因为类装载器只装载一次资源文件到内存中,即使修改了资源文件,这种方法仍然读取内存中的原资源文件数据
//所以此方法适用于数据不会更新的情况
// Properties dbconfig = new Properties();
// InputStream in = UserDao.class.getClassLoader().getResourceAsStream("db.properties");
// dbconfig.load(in);
// System.out.println(dbconfig.getProperty("url"));
//如果要读取更新后的资源文件数据,则先用类装载器得到资源路径,再用传统文件流方式读取资源文件数据
String path = UserDao.class.getClassLoader().getResource("db.properties").getPath();
FileInputStream in = new FileInputStream(path);
Properties dbconfig = new Properties();
dbconfig.load(in);
System.out.println(dbconfig.getProperty("url"));
}
public void delete(){
}
}