Servlet to get the parameters in the configuration file. servlet configuration file
Web. xml (add 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
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 g EtUsername () {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 Servle TException 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 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 ou T = 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 ("