javaweb学习总结二十四(servlet经常用到的对象)

一:ServletConfig对象

1:用来封装数据初始化参数,在服务器web.xml配置文件中可以使用<init-param>标签配置初始化参数。

2:实例演示

web.xml文件中配置初始化参数:

 1 <servlet>
 2     <servlet-name>ServletDemo</servlet-name>
 3     <servlet-class>com.hlcui.servlet.ServletDemo</servlet-class>
 4     <init-param>
 5         <param-name>name</param-name>
 6         <param-value>Tom</param-value>
 7     </init-param>
 8     <init-param>
 9         <param-name>age</param-name>
10         <param-value>26</param-value>
11     </init-param>
12     <init-param>
13         <param-name>salary</param-name>
14         <param-value>12000</param-value>
15     </init-param>
16   </servlet>

servlet类中读入参数:

 1 public void doGet(HttpServletRequest request, HttpServletResponse response) {
 2         // 根据参数名,获取指定属性值
 3         String value = this.getServletConfig().getInitParameter("name");
 4         System.out.println("value=" + value);
 5         System.out.println("..........");
 6         // 获取多个属性值
 7         Enumeration e = this.getServletConfig().getInitParameterNames();
 8         while (e.hasMoreElements()) {
 9             String name = (String) e.nextElement();
10             String value2 = this.getServletConfig().getInitParameter(name);
11             System.out.println(name + "=" + value2);
12         }
13     }

在web.xml中配置初始化参数,然后在创建servlet实例时调用init()方法将servletconfig对象传给servlet类。

 1 private ServletConfig config;
 2 
 3     public void doGet(HttpServletRequest request, HttpServletResponse response) {
 4         String value = config.getInitParameter("name");
 5         System.out.println(value);
 6     }
 7 
 8     public void init(ServletConfig config) {
 9         this.config = config;
10     }

只是httpServlet的实现类GenericServlet已经帮我们做好了这些工作。

 

二:ServletContext对象

1:ServletContext对象代表一个web应用,多个servlet共享servletContext对象的数据

2:ServletContext对象的生命周期,当服务器启动的时候,会为服务器内的每一个web应用

创建servletContext对象,当服务器关闭的时候,会销毁servletcontext对象。

 

3:web应用中的多个servlet共享servletContext对象资源

示例如下:

servletDemo1将数据绑定在servletContext对象上
1 public class ServletDemo1 extends HttpServlet {
2 
3 
4     public void doGet(HttpServletRequest request, HttpServletResponse response)
5             throws ServletException, IOException {
6         //servletDemo1将数据绑定在servletContext对象上
7         this.getServletContext().setAttribute("name", "warrior");
8     }
9 }
ServletDemo2获取servletContext中的数据
1 public class ServletDemo2 extends HttpServlet {
2 
3     public void doGet(HttpServletRequest request, HttpServletResponse response)
4             throws ServletException, IOException {
5         //ServletDemo2获取servletContext中的数据
6         String name = (String) this.getServletContext().getAttribute("name");
7         System.out.println(name);
8     }
9 }

 

4:服务器会将配置文件中的参数会自动封装到servletContext中,可以使用一个或者多个<context-param></context-param>标签

配置参数,context-param标签配置的参数是属于整个web应用的,而init-param标签配置的参数是属于某个servlet对象的。

示例如下:

web.xml中配置:一般情况下,可以在web.xml中配置属于context域的参数,这样所有的servlet都可以使用

 1 <context-param>
 2       <param-name>url</param-name>
 3       <param-value>jdbc:mysql://localhost:3306/test</param-value>
 4   </context-param>    
 5   <context-param>
 6       <param-name>name</param-name>
 7       <param-value>root</param-value>
 8   </context-param>    
 9   <context-param>
10       <param-name>password</param-name>
11       <param-value>root</param-value>
12   </context-param>    

servlet类中获取参数:

 1 public class ServletDemo3 extends HttpServlet {
 2     public void doGet(HttpServletRequest request, HttpServletResponse response)
 3             throws ServletException, IOException {
 4         // 获取某个参数
 5         String url = this.getServletContext().getInitParameter("url");
 6         System.out.println(url);
 7         System.out.println("..................");
 8         // 获取多个参数
 9         Enumeration e = getServletContext().getInitParameterNames();
10         while (e.hasMoreElements()) {
11             String name = (String) e.nextElement();
12             String value = this.getServletContext().getInitParameter(name);
13             System.out.println(name + "=" + value);
14         }
15     }
16 }

 

5:转发

转发:是客户端发送请求到服务器,然后启动一个servlet响应请求,如果这个servlet没有对应的资源,可以将请求转发给另一个资源,比如jsp。

就比如:A向B借钱,但是B没有,但是B说可以帮助向C借,这就是转发。

重定向:是客户端发送请求到服务器,然后启动一个servlet响应请求,这个servlet没有对应资源,它就给浏览器返回一个地址,让浏览器从新发送

请求到新的地址,就比如:A向B借钱,但是B没有,B告诉A,C有钱,让B去找C借。

转发:一次请求request               重定向:两次请求

示例如下:

 1 public class ServletDemo4 extends HttpServlet {
 2 
 3     public void doGet(HttpServletRequest request, HttpServletResponse response)
 4             throws ServletException, IOException {
 5         String data = "hello world!";
 6         request.setAttribute("data", data);   //将数据绑定到request请求上
 7         RequestDispatcher rd = this.getServletContext().getRequestDispatcher("/1.jsp");
 8         rd.forward(request, response);
 9     }
10 }

 

6:读取web中的资源文件

1):在web层读取配置文件,就是在servlet中读取配置文件

a:文件如果在src源文件下面,在doGet()方法中调用test1方法,那么可以使用servletContext对象读取配置

文件,路径为/WEB-INF/classess下面,因为servlet是读取服务器下面的文件,所以一定是class字节码文件

 1 /**
 2      * 当配置文件在src下面的时候
 3      */
 4     private void test1() throws IOException {
 5         // 1:读取配置文件内容
 6         InputStream in = this.getServletContext().getResourceAsStream(
 7                 "/WEB-INF/classes/db.properties");
 8         Properties props = new Properties();
 9         // 2:加载配置内容到properties对象
10         props.load(in);
11 
12         String url = props.getProperty("url");
13         String username = props.getProperty("username");
14         String password = props.getProperty("password");
15         System.out.println("url=" + url + "\nusername=" + username
16                 + "\npassword=" + password);
17     }

b:如果配置文件在包的下面,那么路径就要加上包名,因为在服务器中包名会被解释成目录,所以路径如下

 1 /**
 2      * 当配置文件在包下面的时候
 3      * 
 4      * @throws IOException
 5      */
 6     private void test2() throws IOException {
 7         // 1:读取配置文件内容
 8         InputStream in = this.getServletContext().getResourceAsStream(
 9                 "/WEB-INF/classes/com/hlcui/servlet/db.properties");
10         Properties props = new Properties();
11         // 2:加载配置内容到properties对象
12         props.load(in);
13 
14         String url = props.getProperty("url");
15         String username = props.getProperty("username");
16         String password = props.getProperty("password");
17         System.out.println("url=" + url + "\nusername=" + username
18                 + "\npassword=" + password);
19     }

c:如果配置文件在webroot下面,那么更简单,直接/文件,/ 就代表根目录

 1 /**
 2      * 当配置文件在webroot下面时
 3      * @throws IOException 
 4      */
 5     private void test3() throws IOException{
 6         // 1:读取配置文件内容
 7         InputStream in = this.getServletContext().getResourceAsStream("/db.properties");
 8         Properties props = new Properties();
 9         // 2:加载配置内容到properties对象
10         props.load(in);
11 
12         String url = props.getProperty("url");
13         String username = props.getProperty("username");
14         String password = props.getProperty("password");
15         System.out.println("url=" + url + "\nusername=" + username
16                 + "\npassword=" + password);
17     }
18     

 

2):如果想获取文件的名称,可以使用servletContext的getrealpath方法,如果配置文件路径放的位置不同,

可以参考上一步的路径

 1 /**
 2      * 获取文件名称并读取文件资源
 3      * 
 4      * @throws IOException
 5      */
 6     private void test4() throws IOException {
 7         // 1:获取文件绝对路径
 8         String path = this.getServletContext().getRealPath(
 9                 "/WEB-INF/classes/db.properties");
10         String filename = path.substring(path.lastIndexOf("\\"));
11         System.out.println("资源名称:" + filename);
12         InputStream in = new FileInputStream(path);
13         Properties props = new Properties();
14         // 2:加载配置内容到properties对象
15         props.load(in);
16 
17         String url = props.getProperty("url");
18         String username = props.getProperty("username");
19         String password = props.getProperty("password");
20         System.out.println("url=" + url + "\nusername=" + username
21                 + "\npassword=" + password);
22     }

3):使用类加载器读取配置文件

场景:在web层(servlet类中),我们可以通过获取servletContext读取应用中的资源文件,但是如果不是在

web层,或者是为了降低web层与其他层(业务层service或者数据访问层dao)的耦合性,可以使用类加载器读取

配置文件,使用类加载器读取配置文件要求配置文件不能太大,因为类加载器首先要把配置文件加载到内存中,文件

太大会对虚拟机造成一定的压力。

 1 public class StuDAO {
 2     
 3     //1:在非web层读取配置文件
 4     public void findAll() throws IOException {
 5         InputStream in = StuDAO.class.getClassLoader().getResourceAsStream("db.properties");
 6         Properties props = new Properties();
 7         props.load(in);
 8         
 9         String url = props.getProperty("url");
10         String username = props.getProperty("username");
11         String password = props.getProperty("password");
12         System.out.println("url=" + url + "\nusername=" + username
13                 + "\npassword=" + password);
14     }
15 
16 }

注意:这里的路径要和上面的web层做区分,web层时以web根目录为当前目录,而类加载器

读取文件是在classes目录中,而db.properties则是在classes目录中

 

如果我们直接在服务器上db.properties文件修改内容,则通过浏览器访问服务器时,读取的配置文件内容依然

是上一次的历史数据,因为类加载器只会加载一次,如果上一次已经加载,那么就不会再次加载,这里类加载器

的机制。要想使在服务器上面修改的内容生效,需要使用另一个API:

如下:

 1 public void findAll() throws IOException {
 2         URL url = StuDAO.class.getClassLoader().getResource("db.properties");
 3         String path = url.getPath();
 4         InputStream in = new FileInputStream(path);
 5         Properties props = new Properties();
 6         props.load(in);
 7         
 8         String url2 = props.getProperty("url");
 9         String username = props.getProperty("username");
10         String password = props.getProperty("password");
11         System.out.println("url=" + url2 + "\nusername=" + username
12                 + "\npassword=" + password);
13     }

使用getResource()是可以生效的!!!

 

posted @ 2016-10-24 22:23  warrior1234  阅读(334)  评论(0编辑  收藏  举报