Servlet(19)web.xml 配置之Servlet参数配置和读取资源文件,获取文件全路径
来源:互联网 发布:php技术文档 编辑:程序博客网 时间:2024/05/21 23:31
如果涉及到不同用户共享数据,而这些数据量不大,同时又不希望写入数据库中,我们可以考虑使用ServletContext来实现。
// ------------为servlet1配置相关参数<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>Servlet1</servlet-name> <servlet-class>com.test.Servlet1</servlet-class> <!-- 这样配置,相当于只给该servlet使用 --> <init-param> <param-name>username</param-name> <param-value>scott</param-value> </init-param></servlet><!-- 这样配置,相当于在整个应用有效 --><context-param> <param-name>name</param-name> <param-value>jiaozl</param-value></context-param>//--------在Servlet1中获取配置好的参数----------String val1 = this.getServletConfig().getInitParameter("username"); out.println(val1 + "<br />");//--------获取在web应用中配置好的参数----------String val = this.getServletContext().getInitParameter("name");out.println(val + "<br />");
读取src目录下的文件,采用类加载器读取:http://blog.csdn.net/u013943420/article/details/70161253
①读取资源文件(.properties文件 web目录或web-inf目录下的文件)
// 读取文件InputStream is = this.getServletContext().getResourceAsStream("dbinfo.properties");// 创建属性对象Properties pp = new Properties();pp.load(is);out.println(pp.getProperty("name") + "<br/>");
②获得文件的全路径
// 获得文件的全路径String path = this.getServletContext().getRealPath("dbinfo.properties");out.println(path + "<br/>");
0 0
- Servlet(19)web.xml 配置之Servlet参数配置和读取资源文件,获取文件全路径
- servlet xml文件配置
- servlet 包的 web。xml文件配置
- Servlet过滤器web.xml文件配置
- web.xml文件配置servlet的过程
- Servlet之读取资源文件
- servlet web.xml文件配置servlet-class可为jsp
- web.xml之Servlet配置
- 20160321servlet学习笔记获取web.xml配置参数
- servlet获取web.xml中配置的初始化参数
- Servlet--ServletContext(三)获取web.xml配置参数
- Servlet之ServletContext获取获取web项目的资源文件
- Servlet之ServletContext读取web应用中的资源文件
- [错误]web.xml文件配置错误 servlet-name配置不一致
- servlet web.xml文件的路径问题
- (18)servletContext应用:获取web应用的初始化参数、实现servlet转发、利用servletContext对象读取资源文件
- Servlet 中 web.xml 文件的配置问题
- 在web.xml 文件中servlet的配置
- Codeforces Round #410 (Div. 2)(Codeforces 798 ABCD)
- webDriverAgent Test到iphone遇到的坑
- 聊聊clean code
- Android基本布局-FrameLayout
- 直接内存访问DMA
- Servlet(19)web.xml 配置之Servlet参数配置和读取资源文件,获取文件全路径
- 融合主题模型和协同过滤的多样化移动应用推荐
- AtomicLong.lazySet是如何工作的?
- 安卓开发——ProgressBar反向进度条(进度条从右向左走)
- AutoCAD二次开发三种添加插件按钮的方法
- Longest Line of Consecutive One in Matrix
- Java 8新的日期库
- Jekyll: 给博客加个随机BGM
- java面试题-javaweb基础