web.xml (添加init-param)

 <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>GetInitParameterServlet</servlet-name>
<servlet-class>servlet.GetInitParameterServlet</servlet-class>
<init-param>
<param-name>username</param-name>
<param-value>admin</param-value>
</init-param>
<init-param>
<param-name>password</param-name>
<param-value>123456</param-value>
</init-param>
</servlet> <servlet-mapping>
<servlet-name>GetInitParameterServlet</servlet-name>
<url-pattern>/servlet/GetInitParameterServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

index.jsp

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

GetInitParameterServlet.java:

 package servlet;

 import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class GetInitParameterServlet extends HttpServlet {
private String username;
private String password; public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} /**
* Constructor of the object.
*/
public GetInitParameterServlet() {
super();
} /**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
} /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { doPost(request, response);
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.println("<h2>用户名:"+this.getUsername()+"</h2>");
out.println("<h2>密码:"+this.getPassword()+"</h2>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
} /**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
this.setUsername(this.getInitParameter("username"));
this.setPassword(this.getInitParameter("password"));
} }

servlet中获取配置文件中的参数.的更多相关文章

  1. Servlet中获取POST请求的参数

    在servlet.filter等中获取POST请求的参数 form表单形式提交post方式,可以直接从 request 的 getParameterMap 方法中获取到参数 JSON形式提交post方 ...

  2. Java代码中获取配置文件(config.properties)中内容的两种方法

    方法千千万,本人暂时只总结了两种方法. (1)config.properties中的内容如图 在applicationContext.xml中配置 <!-- 引入配置文件 --> < ...

  3. Spring中抽象类中使用EmbeddedValueResolverAware和@PostConstruct获取配置文件中的参数值

    我的需求: 我有一个 abstract class 中包含了很多子类中需要用到的公共方法和变量,我想在抽象类中 使用@Value获取*.properties中的值.但是@Value必须要在Spring ...

  4. 记录一次bug解决过程:velocity中获取url中的参数

    一.总结 在Webx的Velocity中获取url中参数:$rundata.getRequest().getParameter('userId') 在Webx项目中,防止CSRF攻击(Cross-si ...

  5. ThinkPHP 获取配置文件中的值

    C('SPECIAL_USER'):获取配置文件中的值 存入数组

  6. 如何在Silverlight应用程序中获取ASP.NET页面参数

    asp.net Silverlight应用程序中获取载体aspx页面参数 有时候SL应用中需要使用由aspx页面中传递过来的参数值,此时通常有两种方法获取 1. 使用InitParameters属性, ...

  7. 获取配置文件中key=value

    之前一直是写一个方法获取配置文件中的key=value值得,现在提供更简单的. ResourceBundle 是java.utl中的一个专门针对.properties文件的. //获取配置文件对象 R ...

  8. electron-vue 项目启动动态获取配置文件中的后端服务地址

    前言 最近的项目迭代中新增一个需求,需要在electron-vue 项目打包之后,启动exe 可执行程序的时候,动态获取配置文件中的 baseUrl 作为服务端的地址.electron 可以使用 no ...

  9. CI框架在辅助函数中使用配置文件中的变量

    问题: 现有一个自定义的辅助函数,想要获取配置文件中的配置项(配置文件路径为application/config/config.php) 分析: 辅助函数并不是定义在一个class中,而是很多个可供外 ...

随机推荐

  1. idea JMX 连接器服务器通信错误

    错误描述:错误: JMX 连接器服务器通信错误: service:jmx:rmi://DESKTOP-46OA4KK 打开Edit Configurations 发现vm options那一栏居然是空 ...

  2. Leetcode 64

    class Solution { public: int minPathSum(vector<vector<int>>& grid) { int n = grid.si ...

  3. MyBatise代码自动生成时候Oralce的number类型BigDecimal问题

    使用MyBatise的代码自动生成工具时候,即便在配置文件中定义了 <javaTypeResolver> <property name="forceBigDecimals& ...

  4. 【Matplotlib】概要总览第一讲

    之前一直使用 matplotlib, 但都是随用随查,现在特开此系列帖子已记录其学习过程. Matplotlib可能是Python 扩展包中仅有的最流行的 2D 绘图库.她不仅提供了快速的方式可视化P ...

  5. Thinking in Java之衍生类和基础类的初始化顺序

    <Thinking in Java>书里的例子,我又稍微修改了下代码: class Real{ public Real(String index) { // TODO Auto-gener ...

  6. intent 系统设置界面

      开发Android软件时,常常需要打开系统设置或信息界面,来设置相关系统项或查看系统的相关信息,这时我们就可以使用以下语句来实现:(如打开“无线和网络设置”界面) Intent intent = ...

  7. java Object对象的clone方法

    参考copy链接:http://blog.csdn.net/bigconvience/article/details/25025561 在看原型模式,发现要用到clone这个方法,以前和朋友聊过,没怎 ...

  8. L200

    Last week, I read a story about a 34-year-old British woman who is extremely afraid of metal forks.S ...

  9. MySQL 5.7忘记密码

    关闭正在运行的 MySQL : 1 [root@www.woai.it ~]# service mysql stop 运行 1 [root@www.woai.it ~]# mysqld_safe -- ...

  10. 从开发者的角度分析iOS应如何省电

    从开发者的角度分析iOS应如何省电 说明 网上关于iPhone如何省电的文章很多.但是基本没有讲原理.而在生活中,很多人在使用iPhone中有着明显的错误的省电习惯. 本文从iOS开发者的角度,对iO ...