当前位置: 代码迷 >> Java相关 >> servlet中获取配置文件中的参数
  详细解决方案

servlet中获取配置文件中的参数

热度:47   发布时间:2016-04-22 20:34:42.0
servlet中获取配置文件中的参数.

 

web.xml (添加init-param)

 1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.5"  3     xmlns="http://java.sun.com/xml/ns/javaee"  4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  6     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 7   <display-name></display-name> 8   <servlet> 9     <description>This is the description of my J2EE component</description>10     <display-name>This is the display name of my J2EE component</display-name>11     <servlet-name>GetInitParameterServlet</servlet-name>12     <servlet-class>servlet.GetInitParameterServlet</servlet-class>13     <init-param>14         <param-name>username</param-name>15         <param-value>admin</param-value>16     </init-param>17     <init-param>18         <param-name>password</param-name>19         <param-value>123456</param-value>20     </init-param>21   </servlet>22 23   <servlet-mapping>24     <servlet-name>GetInitParameterServlet</servlet-name>25     <url-pattern>/servlet/GetInitParameterServlet</url-pattern>26   </servlet-mapping>    27   <welcome-file-list>28     <welcome-file>index.jsp</welcome-file>29   </welcome-file-list>30 </web-app>

 

 

index.jsp

1   <h1>获取初始化参数演示案例</h1>2     <hr>3     <a href="servlet/GetInitParameterServlet">获取初始化Servlet</a>

GetInitParameterServlet.java:

 1 package servlet; 2  3 import java.io.IOException; 4 import java.io.PrintWriter; 5  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 GetInitParameterServlet extends HttpServlet {12     private String username;13     private String password;    14         15     public String getUsername() {16         return username;17     }18 19     public void setUsername(String username) {20         this.username = username;21     }22 23     public String getPassword() {24         return password;25     }26 27     public void setPassword(String password) {28         this.password = password;29     }30 31     /**32      * Constructor of the object.33      */34     public GetInitParameterServlet() {35         super();36     }37 38     /**39      * Destruction of the servlet. <br>40      */41     public void destroy() {42         super.destroy(); // Just puts "destroy" string in log43         // Put your code here44     }45 46     /**47      * The doGet method of the servlet. <br>48      *49      * This method is called when a form has its tag value method equals to get.50      * 51      * @param request the request send by the client to the server52      * @param response the response send by the server to the client53      * @throws ServletException if an error occurred54      * @throws IOException if an error occurred55      */56     public void doGet(HttpServletRequest request, HttpServletResponse response)57             throws ServletException, IOException {58 59         doPost(request, response);60     }61 62     /**63      * The doPost method of the servlet. <br>64      *65      * This method is called when a form has its tag value method equals to post.66      * 67      * @param request the request send by the client to the server68      * @param response the response send by the server to the client69      * @throws ServletException if an error occurred70      * @throws IOException if an error occurred71      */72     public void doPost(HttpServletRequest request, HttpServletResponse response)73             throws ServletException, IOException {74 75         response.setContentType("text/html;charset=utf-8");76         PrintWriter out = response.getWriter();77         out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");78         out.println("<HTML>");79         out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");80         out.println("  <BODY>");81     out.println("<h2>用户名:"+this.getUsername()+"</h2>");82     out.println("<h2>密码:"+this.getPassword()+"</h2>");83         out.println("  </BODY>");84         out.println("</HTML>");85         out.flush();86         out.close();87     }88 89     /**90      * Initialization of the servlet. <br>91      *92      * @throws ServletException if an error occurs93      */94     public void init() throws ServletException {        95         this.setUsername(this.getInitParameter("username"));96         this.setPassword(this.getInitParameter("password"));97     }98 99 }

 

  相关解决方案