在Servlet的配置文件web.xml中,可以使用一个或多个
例如:
12 ServletConfigDemo1 3gacl.servlet.study.ServletConfigDemo1 4 56 9name 7gacl 810 13password 11123 1214 17charset 15UTF-8 16
当servlet配置了初始化参数后,web容器在创建servlet实例对象时,会自动将这些初始化参数封装到ServletConfig对象中,并在调用servlet的init方法时,将ServletConfig对象传递给servlet。进而,我们通过ServletConfig对象就可以得到当前servlet的初始化参数信息。
例如:
1 package gacl.servlet.study; 2 3 import java.io.IOException; 4 import java.util.Enumeration; 5 import javax.servlet.ServletConfig; 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 public class ServletConfigDemo1 extends HttpServlet { 12 13 /** 14 * 定义ServletConfig对象来接收配置的初始化参数 15 */ 16 private ServletConfig config; 17 18 /** 19 * 当servlet配置了初始化参数后,web容器在创建servlet实例对象时, 20 * 会自动将这些初始化参数封装到ServletConfig对象中,并在调用servlet的init方法时, 21 * 将ServletConfig对象传递给servlet。进而,程序员通过ServletConfig对象就可以 22 * 得到当前servlet的初始化参数信息。 23 */ 24 @Override 25 public void init(ServletConfig config) throws ServletException { 26 this.config = config; 27 } 28 29 public void doGet(HttpServletRequest request, HttpServletResponse response) 30 throws ServletException, IOException { 31 //获取在web.xml中配置的初始化参数 32 String paramVal = this.config.getInitParameter("name");//获取指定的初始化参数 33 response.getWriter().print(paramVal); 34 35 response.getWriter().print("
"); 36 //获取所有的初始化参数 37 Enumeratione = config.getInitParameterNames(); 38 while(e.hasMoreElements()){ 39 String name = e.nextElement(); 40 String value = config.getInitParameter(name); 41 response.getWriter().print(name + "=" + value + "
"); 42 } 43 } 44 45 public void doPost(HttpServletRequest request, HttpServletResponse response) 46 throws ServletException, IOException { 47 this.doGet(request, response); 48 } 49 50 }
运行结果如下:
WEB容器在启动时,它会为每个WEB应用程序都创建一个对应的ServletContext对象,它代表当前web应用。
ServletConfig对象中维护了ServletContext对象的引用,开发人员在编写servlet时,可以通过ServletConfig.getServletContext方法获得ServletContext对象。
由于一个WEB应用中的所有Servlet共享同一个ServletContext对象,因此Servlet对象之间可以通过ServletContext对象来实现通讯。ServletContext对象通常也被称之为context域对象。
范例:ServletContextDemo1和ServletContextDemo2通过ServletContext对象实现数据共享
1 package gacl.servlet.study; 2 3 import java.io.IOException; 4 import javax.servlet.ServletContext; 5 import javax.servlet.ServletException; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 public class ServletContextDemo1 extends HttpServlet { 11 12 public void doGet(HttpServletRequest request, HttpServletResponse response) 13 throws ServletException, IOException { 14 String data = "xdp_gacl"; 15 /** 16 * ServletConfig对象中维护了ServletContext对象的引用,开发人员在编写servlet时, 17 * 可以通过ServletConfig.getServletContext方法获得ServletContext对象。 18 */ 19 ServletContext context = this.getServletConfig().getServletContext();//获得ServletContext对象 20 context.setAttribute("data", data); //将data存储到ServletContext对象中 21 } 22 23 public void doPost(HttpServletRequest request, HttpServletResponse response) 24 throws ServletException, IOException { 25 doGet(request, response); 26 } 27 }
1 package gacl.servlet.study; 2 3 import java.io.IOException; 4 import javax.servlet.ServletContext; 5 import javax.servlet.ServletException; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 public class ServletContextDemo2 extends HttpServlet { 11 12 public void doGet(HttpServletRequest request, HttpServletResponse response) 13 throws ServletException, IOException { 14 ServletContext context = this.getServletContext(); 15 String data = (String) context.getAttribute("data");//从ServletContext对象中取出数据 16 response.getWriter().print("data="+data); 17 } 18 19 public void doPost(HttpServletRequest request, HttpServletResponse response) 20 throws ServletException, IOException { 21 doGet(request, response); 22 } 23 }
先运行ServletContextDemo1,将数据data存储到ServletContext对象中,然后运行ServletContextDemo2就可以从ServletContext对象中取出数据了,这样就实现了数据共享,如下图所示:
在web.xml文件中使用
1 24 5 6 7 10 11url 8jdbc:mysql://localhost:3306/test 912 14index.jsp 13
获取Web应用的初始化参数,代码如下:
1 package gacl.servlet.study; 2 3 import java.io.IOException; 4 import javax.servlet.ServletContext; 5 import javax.servlet.ServletException; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 11 public class ServletContextDemo3 extends HttpServlet { 12 13 public void doGet(HttpServletRequest request, HttpServletResponse response) 14 throws ServletException, IOException { 15 16 ServletContext context = this.getServletContext(); 17 //获取整个web站点的初始化参数 18 String contextInitParam = context.getInitParameter("url"); 19 response.getWriter().print(contextInitParam); 20 } 21 22 public void doPost(HttpServletRequest request, HttpServletResponse response) 23 throws ServletException, IOException { 24 doGet(request, response); 25 } 26 27 }
运行结果:
ServletContextDemo4
1 package gacl.servlet.study; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 import javax.servlet.RequestDispatcher; 6 import javax.servlet.ServletContext; 7 import javax.servlet.ServletException; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 public class ServletContextDemo4 extends HttpServlet { 13 14 public void doGet(HttpServletRequest request, HttpServletResponse response) 15 throws ServletException, IOException { 16 String data = "abcdefghjkl
"; 17 response.getOutputStream().write(data.getBytes()); 18 ServletContext context = this.getServletContext();//获取ServletContext对象 19 RequestDispatcher rd = context.getRequestDispatcher("/servlet/ServletContextDemo5");//获取请求转发对象(RequestDispatcher) 20 rd.forward(request, response);//调用forward方法实现请求转发 21 } 22 23 public void doPost(HttpServletRequest request, HttpServletResponse response) 24 throws ServletException, IOException { 25 } 26 }
ServletContextDemo5
1 package gacl.servlet.study; 2 3 import java.io.IOException; 4 import javax.servlet.ServletException; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 9 public class ServletContextDemo5 extends HttpServlet { 10 11 public void doGet(HttpServletRequest request, HttpServletResponse response) 12 throws ServletException, IOException { 13 response.getOutputStream().write("servletDemo5".getBytes()); 14 } 15 16 public void doPost(HttpServletRequest request, HttpServletResponse response) 17 throws ServletException, IOException { 18 this.doGet(request, response); 19 } 20 21 }
运行结果:
访问的是ServletContextDemo4,浏览器显示的却是ServletContextDemo5的内容,这就是使用ServletContext实现了请求转发
项目目录结构如下:
按 Ctrl+C 复制代码
按 Ctrl+C 复制代码
运行结果如下:
1 package gacl.servlet.study; 2 3 import java.io.FileOutputStream; 4 import java.io.IOException; 5 import java.io.InputStream; 6 import java.io.OutputStream; 7 import java.text.MessageFormat; 8 import java.util.Properties; 9 10 import javax.servlet.ServletException; 11 import javax.servlet.http.HttpServlet; 12 import javax.servlet.http.HttpServletRequest; 13 import javax.servlet.http.HttpServletResponse; 14 15 /** 16 * 用类装载器读取资源文件 17 * 通过类装载器读取资源文件的注意事项:不适合装载大文件,否则会导致jvm内存溢出 18 * @author gacl 19 * 20 */ 21 public class ServletContextDemo7 extends HttpServlet { 22 23 public void doGet(HttpServletRequest request, HttpServletResponse response) 24 throws ServletException, IOException { 25 /** 26 * response.setContentType("text/html;charset=UTF-8");目的是控制浏览器用UTF-8进行解码; 27 * 这样就不会出现中文乱码了 28 */ 29 response.setHeader("content-type","text/html;charset=UTF-8"); 30 test1(response); 31 response.getWriter().println("
"); 32 test2(response); 33 response.getWriter().println("
"); 34 //test3(); 35 test4(); 36 37 } 38 39 /** 40 * 读取类路径下的资源文件 41 * @param response 42 * @throws IOException 43 */ 44 private void test1(HttpServletResponse response) throws IOException { 45 //获取到装载当前类的类装载器 46 ClassLoader loader = ServletContextDemo7.class.getClassLoader(); 47 //用类装载器读取src目录下的db1.properties配置文件 48 InputStream in = loader.getResourceAsStream("db1.properties"); 49 Properties prop = new Properties(); 50 prop.load(in); 51 String driver = prop.getProperty("driver"); 52 String url = prop.getProperty("url"); 53 String username = prop.getProperty("username"); 54 String password = prop.getProperty("password"); 55 response.getWriter().println("用类装载器读取src目录下的db1.properties配置文件:"); 56 response.getWriter().println( 57 MessageFormat.format( 58 "driver={0},url={1},username={2},password={3}", 59 driver,url, username, password)); 60 } 61 62 /** 63 * 读取类路径下面、包下面的资源文件 64 * @param response 65 * @throws IOException 66 */ 67 private void test2(HttpServletResponse response) throws IOException { 68 //获取到装载当前类的类装载器 69 ClassLoader loader = ServletContextDemo7.class.getClassLoader(); 70 //用类装载器读取src目录下的gacl.servlet.study包中的db4.properties配置文件 71 InputStream in = loader.getResourceAsStream("gacl/servlet/study/db4.properties"); 72 Properties prop = new Properties(); 73 prop.load(in); 74 String driver = prop.getProperty("driver"); 75 String url = prop.getProperty("url"); 76 String username = prop.getProperty("username"); 77 String password = prop.getProperty("password"); 78 response.getWriter().println("用类装载器读取src目录下的gacl.servlet.study包中的db4.properties配置文件:"); 79 response.getWriter().println( 80 MessageFormat.format( 81 "driver={0},url={1},username={2},password={3}", 82 driver,url, username, password)); 83 } 84 85 /** 86 * 通过类装载器读取资源文件的注意事项:不适合装载大文件,否则会导致jvm内存溢出 87 */ 88 public void test3() { 89 /** 90 * 01.avi是一个150多M的文件,使用类加载器去读取这个大文件时会导致内存溢出: 91 * java.lang.OutOfMemoryError: Java heap space 92 */ 93 InputStream in = ServletContextDemo7.class.getClassLoader().getResourceAsStream("01.avi"); 94 System.out.println(in); 95 } 96 97 /** 98 * 读取01.avi,并拷贝到e:\根目录下 99 * 01.avi文件太大,只能用servletContext去读取 100 * @throws IOException 101 */ 102 public void test4() throws IOException { 103 // path=G:\Java学习视频\JavaWeb学习视频\JavaWeb\day05视频\01.avi 104 // path=01.avi 105 String path = this.getServletContext().getRealPath("/WEB-INF/classes/01.avi"); 106 /** 107 * path.lastIndexOf("\\") + 1是一个非常绝妙的写法 108 */ 109 String filename = path.substring(path.lastIndexOf("\\") + 1);//获取文件名 110 InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/01.avi"); 111 byte buffer[] = new byte[1024]; 112 int len = 0; 113 OutputStream out = new FileOutputStream("e:\\" + filename); 114 while ((len = in.read(buffer)) > 0) { 115 out.write(buffer, 0, len); 116 } 117 out.close(); 118 in.close(); 119 } 120 121 public void doPost(HttpServletRequest request, HttpServletResponse response) 122 throws ServletException, IOException { 123 124 this.doGet(request, response); 125 } 126 127 }
运行结果如下:
对于不经常变化的数据,在servlet中可以为其设置合理的缓存时间值,以避免浏览器频繁向服务器发送请求,提升服务器的性能。例如:
按 Ctrl+C 复制代码
按 Ctrl+C 复制代码
分类: JavaWeb学习总结