servlet中获取配置文件中的参数.
2023-09-11 14:22:24 时间
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 log 43 // Put your code here 44 } 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 server 52 * @param response the response send by the server to the client 53 * @throws ServletException if an error occurred 54 * @throws IOException if an error occurred 55 */ 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 server 68 * @param response the response send by the server to the client 69 * @throws ServletException if an error occurred 70 * @throws IOException if an error occurred 71 */ 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 occurs 93 */ 94 public void init() throws ServletException { 95 this.setUsername(this.getInitParameter("username")); 96 this.setPassword(this.getInitParameter("password")); 97 } 98 99 }
相关文章
- ssm(Spring+Spring mvc+mybatis)Spring配置文件——applicationContext-servlet.xml
- Servlet使用适配器模式进行增删改查案例(IBaseDaoUtil.java)
- 新闻发布项目——Servlet类(doNewsModifyServlet )
- 《Head First Servlet JSP》web服务器,容器,servlet的职责
- [Servlet&JSP] HttpServletResponse的使用
- springboot配置servlet容器的两种途径:配置文件及编码方式
- Eclipse — 如何快速生成servlet类和配置文件(web里)
- ServletContext(Servlet上下文对象)作用 生命周期
- 严重: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Reque
- Java学习路线-48:Servlet学习
- javaweb核心技术servlet
- 基于Jsp+Servlet的图书管理系统
- java web开发中Servlet、Request、Response常用的代码
- Servlet — 线程安全问题
- Servlet Filter实现全站统一编码,解决中文乱码问题
- idea 中 tomcat 部署 servlet启动