码迷,mamicode.com
首页 > 编程语言 > 详细

javaee学习-servlet初始化参数

时间:2016-07-01 20:07:31      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:

1.需要定义ServletConfig对象来接收servlet配置的初始化参数。

2.当servlet配置了初始化参数后,web容器在创建servlet实例对象时, 会自动将这些初始化参数封装到ServletConfig对象中,并在调用servlet的init方法时,将ServletConfig对象传递给servlet。进而,程序员通过ServletConfig对象就可以得到当前servlet的初始化参数信息。

3.注意:获取servlet初始值(需要在init()方法中初始化)//注意,这里必须设置,自动加载,<load-on-startup>1</load-on-startup>//或者配置映射才能初始化servlet初始化参数,直接在类中不能初始化。

以下是代码:

*helloworld.java

 1 package myweb;
 2 
 3 import java.io.IOException;
 4 import java.io.PrintWriter;
 5 import java.util.Enumeration;
 6 
 7 import javax.servlet.ServletConfig;
 8 import javax.servlet.ServletException;
 9 import javax.servlet.annotation.WebServlet;
10 import javax.servlet.http.HttpServlet;
11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpServletResponse;
13 
14 /**
15  * Servlet implementation class helloworld
16  */
17 public class helloworld extends HttpServlet {
18     private static final long serialVersionUID = 1L;
19     /**
20      * 定义ServletConfig对象来接收servlet配置的初始化参数
21      */
22     private ServletConfig config;
23     /**
24           * 当servlet配置了初始化参数后,web容器在创建servlet实例对象时,
25           * 会自动将这些初始化参数封装到ServletConfig对象中,并在调用servlet的init方法时,
26           * 将ServletConfig对象传递给servlet。进而,程序员通过ServletConfig对象就可以
27           * 得到当前servlet的初始化参数信息。
28           */
29          @Override
30          public void init(ServletConfig config) throws ServletException {
31              this.config = config;
32          }
33     /**
34      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
35      */
36     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
37         
38         //.获取servlet初始值(需要在init()方法中初始化)
39         //注意,这里必须设置,自动加载,<load-on-startup>1</load-on-startup>
40         //或者配置映射才能初始化servlet初始化参数,直接在类中不能初始化
41         //获取在web.xml中配置的初始化参数
42                  String paramVal = this.config.getInitParameter("name");//获取指定的初始化参数
43                  response.getWriter().print(paramVal);
44                  response.getWriter().print("<hr/>");
45                  //获取所有的初始化参数
46                  Enumeration<String> e = config.getInitParameterNames();
47                  while(e.hasMoreElements()){
48                      String name = e.nextElement();
49                      String value = config.getInitParameter(name);
50                      response.getWriter().print(name + "=" + value + "<br/>");
51                  }
52     }
53     
54 
55     /**
56      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
57      */
58     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
59         // TODO Auto-generated method stub
60     }
61 
62 }

web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
 3 
 4 <servlet>
 5     <servlet-name>helloworld</servlet-name>
 6     <servlet-class>myweb.helloworld</servlet-class>
 7 
 8     <!--配置helloworld(servlet)的初始化参数 -->
 9     <init-param>
10         <param-name>name</param-name>
11         <param-value>xianghu</param-value>
12     </init-param>
13      <init-param>
14         <param-name>password</param-name>
15         <param-value>123456</param-value>
16     </init-param>
17     <init-param>
18         <param-name>charset</param-name>
19         <param-value>UTF-8</param-value>
20     </init-param>
21 </servlet>
22 <servlet-mapping>
23     <servlet-name>helloworld</servlet-name>
24     <url-pattern>/Default</url-pattern>
25 </servlet-mapping>
26 </web-app>

 

javaee学习-servlet初始化参数

标签:

原文地址:http://www.cnblogs.com/xianghu/p/5634111.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!