javaee学习-servlet初始化参数

时间:2022-07-16 21:06:11

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>