【ServletConfig、ServletContext】使用ServletContext、类加载器读取资源文件
2014-04-28 09:50
501 查看
1、在实际开发中有一些参数不适合在servlet中写死,这时候就可以在servlet中的init中配置。
2、在dopost/doget方法中调用this.getServletConfig()分析
查看GenericServlet代码
使用ServletConfig得到参数
3、web容器在启动的时候,他会为每一个web应用程序创建一个ServletContext对象,它代表当前的web应用。因此ServletContext对象也被称为context域对象。
4、ServletContext的两种得到方式:
5、ServletContext实现请求转发
6、ServletContext实现多个servlet之间通讯
7、使用ServletContext读取资源文件
<init-param> <param-name>username</param-name> <param-value>jingtian</param-value> </init-param>
2、在dopost/doget方法中调用this.getServletConfig()分析
查看GenericServlet代码
private transient ServletConfig config; @Override public ServletConfig getServletConfig() { return config; } @Override public void init(ServletConfig config) throws ServletException { this.config = config; this.init(); }
使用ServletConfig得到参数
this.getServletConfig().getInitParameter("username");
得到所有参数: Enumeration e= this.getInitParameterNames(); while(e.hasMoreElements()){ String a=(String) e.nextElement(); String v=this.getServletConfig().getInitParameter(a); System.out.println(a+"="+v); }
3、web容器在启动的时候,他会为每一个web应用程序创建一个ServletContext对象,它代表当前的web应用。因此ServletContext对象也被称为context域对象。
<context-param> <param-name>data</param-name> <param-value>xxxx</param-value> </context-param>
4、ServletContext的两种得到方式:
ServletContext context=this.getServletConfig().getServletContext(); context=this.getServletContext();
5、ServletContext实现请求转发
context.getRequestDispatcher("/index.html").forward(request, response);
6、ServletContext实现多个servlet之间通讯
setAttribute() getAttribute()
7、使用ServletContext读取资源文件
package com.xiaozhi.controller; import java.io.FileInputStream; import java.io.FileNotFoundException; 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; import com.xiaozhi.dao.UserDao; public class ServletDemo extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { UserDao dao=new UserDao(); dao.test2(); } //下面讲解的是Servlet读取资源文件 public void test3() throws FileNotFoundException, IOException { //FileINputStream使用相对路径读取classes下的db.properties FileInputStream fileInputStream=new FileInputStream("../webapps/HelloJavaWeb/WEB-INF/classes/db.properties"); Properties properties=new Properties(); properties.load(fileInputStream); System.out.println(properties.getProperty("username")); System.out.println(properties.getProperty("password")); } public void test2() throws FileNotFoundException, IOException { //FileINputStream使用绝对路径读取classes下的db.properties String path=this.getServletContext().getRealPath("/WEB-INF/classes/db.properties"); FileInputStream fileInputStream=new FileInputStream(path); Properties properties=new Properties(); properties.load(fileInputStream); System.out.println(properties.getProperty("username")); System.out.println(properties.getProperty("password")); } public void test1() throws IOException { //使用ServletContext读取classes下的db.properties InputStream inputStream=this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties"); Properties properties=new Properties(); properties.load(inputStream); System.out.println(properties.getProperty("username")); System.out.println(properties.getProperty("password")); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }8、使用类加载器读取资源文件
package com.xiaozhi.dao; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.util.Properties; public class UserDao { private static Properties properties=new Properties(); static{ //类加载器以类的方式读取资源文件,资源文件会永驻内存,如果资源文件发生改变,服务器不重启,则不能实时更新 //UserDao.class也是永驻内存 InputStream inputStream=UserDao.class.getClassLoader().getResourceAsStream("db.properties"); try { properties.load(inputStream); } catch (IOException e) { throw new ExceptionInInitializerError(e); } } //资源资源发生改变,实时更新 public void test2() throws IOException { //使用类加载器去读资源文件的位置 String path=UserDao.class.getClassLoader().getResource("db.properties").getPath(); FileInputStream inputStream=new FileInputStream(path); Properties properties=new Properties(); properties.load(inputStream); System.out.println(properties.getProperty("username")); System.out.println(properties.getProperty("password")); } //使用类加载器读取资源文件 public void test1() throws IOException { //classe下的所有类都使用一个加载器,这个类加载器可以直接加载classes下的文件 InputStream inputStream=UserDao.class.getClassLoader().getResourceAsStream("db.properties"); Properties properties=new Properties(); properties.load(inputStream); System.out.println(properties.getProperty("username")); System.out.println(properties.getProperty("password")); } }
相关文章推荐
- Servlet之ServletContext读取web应用中的资源文件
- web项目中servlet利用servletContext对象读取资源文件
- (18)servletContext应用:获取web应用的初始化参数、实现servlet转发、利用servletContext对象读取资源文件
- 在servlet中通过servletcontext对象读取资源文件的模板代码
- 使用servletContext读取资源文件
- ServletConfig和ServletContext以及读取资源的方法
- ServletContext笔记和servlet和传统方法读取资源文件
- Servlet接口的实现类,路径配置映射,ServletConfig对象,ServletContext对象及web工程中文件的读取
- ServletConfig和ServletContext以及读取资源的方法
- DirectX11 With Windows SDK--19 模型加载:obj格式的读取及使用二进制文件提升读取效率
- ServletContext加载资源文件
- 使用C#语言读取config配置文件
- servlet学习笔记---读取资源文件
- ServletContext读取资源文件
- [原创]java WEB学习笔记55:Struts2学习之路---详解struts2 中 Action,如何访问web 资源,解耦方式(使用 ActionContext,实现 XxxAware 接口),耦合方式(通过ServletActionContext,通过实现 ServletRequestAware, ServletContextAware 等接口的方式)
- 从资源文件里加载文件或读取资源(C#)
- JAVA类加载器二 通过类加载器读取资源文件
- Servlet读取properties资源文件
- ServletConfig读取web.xml文件的应用实例
- spring使用ApplicationContext读取资源文件