Servlet中读取参数的几种方式
为每一Servlet设置初始化参数
可以为每一个Servlet在对应的web.xml中的Servlet节点下编写初始化参数,格式如下:
<init-param>
<param-name>userName</param-name>
<param-value>admin</param-value>
</init-param>
然后在servlet中用如下代码获取相应的参数:
ServletConfig config = this.getServletConfig();
this.username = config.getInitParameter("userName");
为所有的Servlet设置公用的初始化参数
可以为所有的Servlet设置公用初始化参数,该参数和上面的参数有所不同,上面的要放在对应的Servlet节点下,而公用参数不用也不能放在Servlet节点下。
<context-param>
<param-name>userName</param-name>
<param-value>admin</param-value>
</context-param>
同样在Servlet中可以通过如下代码获取我们设置的全局配置信息对象:
ServletContext context = this.getServletContext();
String userNameInGlobal = context.getInitParameter("userName");
在代码中设置公用属性
可以在代码中为ServletContext设置属性,然后就可以在任意地方获取了。该属性是全局属性,一旦设置成功后,在整个容器中均可以使用。
ServletContext context = this.getServletContext(); context.setAttribute("maxNumber", 999); int maxNumebrInContext = (int)context.getAttribute("maxNumber"); |
读取外部文件中的配置信息
通过ServletContext对象读取外部文件中的配置信息主要有三种形式:
- getResource
- 返回一个URL对象,然后调用openStream()方法获取InputStream
- getResourceAsStream
- 直接返回InputStream对象
- getRealPath
- 返回资源文件的绝对路径,然后通过绝对路径用FileInputStream类读取数据
在classes目录下有一个Person.properties文件,内容如下:
getResource获取外部文件
@Override public void init() throws ServletException { ServletContext ctx = this.getServletContext(); String resourcePath = "/WEB-INF/classes/Person.properties"; try { URL url = ctx.getResource(resourcePath); InputStream is = url.openStream(); String name = getPropertiesByKey("name", is); System.out.println(name); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //从Stream中读取properties信息 public static String getPropertiesByKey(String key, InputStream is) { Properties properties = new Properties(); try { properties.load(is); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } String value = (String) properties.get(key); return value; } |
getResourceAsStream获取外部文件
public void init() throws ServletException { ServletContext ctx = this.getServletContext(); String resourcePath = "/WEB-INF/classes/Person.properties"; InputStream is = ctx.getResourceAsStream(resourcePath); String age = getPropertiesByKey("age", is); System.out.println("年龄是: "+ age); } |
getRealPath获取外部文件
public void init() throws ServletException { ServletContext ctx = this.getServletContext(); String resourcePath = "/WEB-INF/classes/Person.properties"; String resourceRealPath = ctx.getRealPath(resourcePath); try { InputStream is = new FileInputStream(new File(resourceRealPath)); String address = getPropertiesByKey("address", is); System.out.println("地址是:" + address); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } |
Servlet中读取参数的几种方式的更多相关文章
- SpringBoot中读取配置文件的几种方式
1.读取application文件 在application.yml或者properties文件中添加: info: name: xiaoming age: 13 sex: 1 读取方式如下: imp ...
- tornado中传递参数的几种方式
方法一 :tornado路由可以使用正则表达式中的子表达式传递url参数.比如:(r"/member//(\w*)/([01]*)", MemberHandler)匹配以后,tor ...
- spring boot中读取配置文件的两种方式
application.properties test.name=测试 test.url=www.test.com 1.@Value注解 在controller里可以这样直接调用 @Value(&qu ...
- Spring MVC中forward请求转发2种方式(带参数)
Spring MVC中forward请求转发2种方式(带参数) http://www.51gjie.com/javaweb/956.html
- php获取post参数的几种方式 RPC 规定接收取值方式 $GLOBALS['HTTP_RAW_POST_DATA'];
http://www.cnblogs.com/zhepama/p/4022606.html PHP默认识别的数据类型是application/x-www.form-urlencoded标准的数据类型. ...
- JavaWeb应用中初始化Log4j的两种方式
本文主要介绍了普通JavaWeb应用(基于Tomcat)中初始化Log4j的两种方式: 1.通过增加 InitServlet ,设置令其自启动来初始化 Log4j . 2.通过监听器 ServletC ...
- asp传递参数的几种方式
把下列代码分别加入a.asp和b.asp的<body></body>中,点提交,就可以将a.asp文本框的内容传给b.asp并显示出来 a.ASP <form actio ...
- linux内核分析作业4:使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用
系统调用:库函数封装了系统调用,通过库函数和系统调用打交道 用户态:低级别执行状态,代码的掌控范围会受到限制. 内核态:高执行级别,代码可移植性特权指令,访问任意物理地址 为什么划分级别:如果全部特权 ...
- JavaScript学习12 JS中定义对象的几种方式
JavaScript学习12 JS中定义对象的几种方式 JavaScript中没有类的概念,只有对象. 在JavaScript中定义对象可以采用以下几种方式: 1.基于已有对象扩充其属性和方法 2.工 ...
随机推荐
- request.getParameter与request.getAttribute()
这里就request为例,不去考虑session. request对象是javax.servlet.http.HttpServletRequest接口的一个实例,request表示调用JSP页面的请求 ...
- unity, Animation crossfade需要两动画在时间上确实有交叠
unity现在播动画都用Animator了,但公司的老项用的还是Animation,今天遇到一个bug,是两个动画的衔接处不连贯. 最后发现是由于A动画已经播完之后B动画才开始播,而且还用了cross ...
- jQuery.retryAjax
Overload method for $.ajax that provides the ability to try the request over if it fails the first t ...
- Android中用双缓存技术,加载网络图片
最近在学校参加一个比赛,写的一个Android应用,里面要加载大量的网络图片,可是用传统的方法图片一多就会造成程序出现内存溢出而崩溃.因为自己也在学习中,所以看了很多博客和视频,然后参照这些大神的写源 ...
- Struts2中s:set标签和s:if标签小结
1. s:set标签 格式:<s:set name="" value="" scope=””/> 说明:把jsp页面中的一个值,以name存储起来 ...
- python file operation
file.open(name[,mode[,buffering]]) 模式的类型有: r 默认只读 w 以写方式打开,如果文件不存在则会先创建,如果文件存在则先把文件内容清空(truncate ...
- Oracle视图详解
转载自:http://blog.itpub.net/29785807/viewspace-1270120/ 一. 视图的定义 视图(view),也称虚表, 不占用物理空间,这个也是相对概念,因为视图本 ...
- Java compiler level does not match the version of the installed Java project facet.问题
从同事那里拷贝过来的web项目,导入到eclipse中,出现Java compiler level does not match the version of the installed Java p ...
- Redis集群方案介绍
由于Redis出众的性能,其在众多的移动互联网企业中得到广泛的应用.Redis在3.0版本前只支持单实例模式,虽然现在的服务器内存可以到100GB.200GB的规模,但是单实例模式限制了Redis没法 ...
- Zabbix agent on Microsoft Windows
1.在Windows上创建目录: C:\Windows\zabbix\ 2.下载安装包并解压到新建的目录 3.下载地址:http://www.zabbix.com/downloads/3.0.0/za ...