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. eclipse中导入maven项目:org.apache.maven.archiver.MavenArchiver.getManifest(org.apache.maven.project.Maven

    org.codehaus.plexus.archiver.jar.Manifest.write(java.io.PrintWriter) 解决方法为:更新eclipse中的maven插件 1.help ...

  2. pycharm同时使用python2.7版本和python3.6版本

    最近在看爬虫的专题,很多爬虫的教程是python2的,电脑上装的是3.6版本,而且python不向下兼容,这就很麻烦,最简单的print要加括号啊,等等.于是分享一个在windows环境下pychar ...

  3. Tensorboard 的使用笔记

    参考的教程: https://www.tensorflow.org/guide/summaries_and_tensorboard 遇到的错误: File "/usr/local/lib/p ...

  4. Sum Problem

    2018-04-22 19:59:52 Sum系列的问题是Leetcode上的一个很经典的系列题,这里做一个简单的总结. 167. Two Sum II - Input array is sorted ...

  5. 雷林鹏分享:Ruby XML, XSLT 和 XPath 教程

    Ruby XML, XSLT 和 XPath 教程 什么是 XML ? XML 指可扩展标记语言(eXtensible Markup Language). 可扩展标记语言,标准通用标记语言的子集,一种 ...

  6. 两个cookie的合并

    这里为什么会想到这个问题呢? 1.我们在对一个商品下订单之前需要2个步骤,1---登录,2---加入购物车 2.那么我们到底是用哪一个cookie呢?实际测试的时候, a.发现只用了登录cookie, ...

  7. Eclipse中配置Solr源码

    转自 http://hongweiyi.com/2013/03/configurate-solr-src-in-eclipse/ 1. 下载solr的src包,并解压 2. 解压后,在解压后的根目录执 ...

  8. C++设计模式之桥接模式

    [DP]书上定义:将抽象部分与它的实现部分分离,使它们都可以独立地变化.考虑装操作系统,有多种配置的计算机,同样也有多款操作系统.如何运用桥接模式呢?可以将操作系统和计算机分别抽象出来,让它们各自发展 ...

  9. git 忽略不提交的文件3种情形

    1..gitignore文件 :从未提交过的文件,从来没有被 Git 记录过的文件 也就是添加之后从来没有提交(commit)过的文件,可以使用.gitignore忽略该文件.只能作用于未跟踪的文件( ...

  10. linux下之mysql篇

    网上查到的一般是 yum install mysql yum install mysql-server yum intall mysql-devel 但是在centos7下  mysql-server ...