ServletConfig和ServletContext
来源:互联网 发布:三星软件下载中心 编辑:程序博客网 时间:2024/06/05 01:00
一、ServletConfig类
代表当前Servlet在web.xml中的配置信息
String getServletName() -- 获取当前Servlet在web.xml中配置的名字String getInitParameter(String name) -- 获取当前Servlet指定名称的初始化参数的值
Enumeration getInitParameterNames() -- 获取当前Servlet所有初始化参数的名字组成的枚举
***(重要)ServletContext getServletContext() -- 获取代表当前web应用的ServletContext对象
public class SConfigServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//1.获取ServletConfig对象ServletConfig servletConfig = this.getServletConfig();//2.获取servlet名字 String getServletName() String servletName = servletConfig.getServletName();System.out.println(servletName);//3.获取初始化参数值String value1 = getInitParameter("name1");String value2 = getInitParameter("name2");System.out.println(value1);System.out.println(value2);System.out.println("-----------------");//4.获取当前Servlet所有初始化参数的名字组成的枚举Enumeration namesEnum = this.getInitParameterNames();while (namesEnum.hasMoreElements()) {String name = (String) namesEnum.nextElement();String value = servletConfig.getInitParameter(name);System.out.println(name+":"+value);}}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}}
二、ServletContext
代表当前web应用
1.做为域对象可以在整个web应用范围内共享数据
域对象:在一个可以被看见的范围内共享数据用到对象作用范围:整个web应用范围内共享数据
生命周期:当服务器启动web应用加载后创建出ServletContext对象后,域产生。当web应用被移除出容器或服务器关闭,随着web应用的销毁域销毁。
void setAttribute(String,Object);
Object getAttribute(String);
void removeAttribute(String);
//设置属性
public class Demo2Servlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {this.getServletContext().setAttribute("chairman", "XiDaDa");}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}}//获取属性值
public class Demo3Servlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {String v = (String) this.getServletContext().getAttribute("chairman");System.out.println(v);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}}
控制台输出:XiDaDa
2.用来获取web应用的初始化参数
整个web应用的初始化参数,位于web.xml文件中的<web-app>节点下。
请求参数 parameter --- 浏览器发送过来的请求中的参数信息初始化参数 initparameter --- 在web.xml中为Servlet或ServletContext配置的初始化时带有的基本参数
域属性 attribute --- 四大作用于中存取的键值对
public class Demo4Servlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {/** * 获取整个web应用的初始化信息 * ServletContext是整个web应用唯一的,所有的servlet都共享整个context * ServletConfig是每个servlet可以获取的,从而得到当前servlet的一些信息 */ServletContext sContext = this.getServletContext();Enumeration names = sContext.getInitParameterNames();while (names.hasMoreElements()) {String name = (String) names.nextElement();String value = sContext.getInitParameter(name);System.out.println(name+":"+value);}}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}}
3.实现Servlet的请求转发
重定向 : 302+Location (比喻:你来借我钱,我没有,我让你去找我的助理借钱)
请求转发 : 服务器内不进行资源流转 (比喻:你来找我借钱,我没有,我帮你找我的助理借钱)
*请求转发是一次请求一次响应实现资源流转.请求重定向两次请求两次响应.
public class Demo5Servlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {RequestDispatcher requestDispatcher = this.getServletContext().getRequestDispatcher("/servlet/Demo6Servlet"); //转发到Demo6ServletrequestDispatcher.forward(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}}Demo6Servlet文件:
public class Demo6Servlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.getWriter().write("$99999999999999999999");}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}}
4.加载资源文件
①第一种情况是在在Servlet中读取资源文件
在Servlet中读取资源文件时:如果写相对路径和绝对路径,由于路径将会相对于程序启动的目录--在web环境下,就是tomcat启动的目录即tomcat/bin--所有找不到资源
如果写硬盘路径,可以找到资源,但是只要一换发布环境,这个硬盘路径很可能是错误的,同样不行.
为了解决这样的问题ServletContext提供了getRealPath方法,在这个方法中传入一个路径,这个方法的底层会在传入的路径前拼接当前web应用的硬盘路径从而得到当前资源的硬盘路径,这种方式即使换了发布环境,方法的底层也能得到正确的web应用的路径从而永远都是正确的资源的路径
this.getServletContext().getRealPath("config.properties")
举例:先在WEB-INF文件夹下建立config.properties文件,在其中写入以下代码:
username=zhang
password=123
servlet访问代码:
public class Demo7Servlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {Properties prop = new Properties();//prop.load(new FileReader("D:\\tomcat6\\webapps\\Day0202\\config.properties")); //这种方法可以,但是不好!!!prop.load(new FileReader(this.getServletContext().getRealPath("config.properties")));//推荐使用的方法System.out.println(prop.getProperty("username"));System.out.println(prop.getProperty("password"));}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}}
②第二种情况是在非Servlet环境下
比如,我又单开一个包,包中有个service方法,需要加载资源文件。如何解决?
如果在非Servlet环境下要读取资源文件时可以采用类加载器加载文件的方式读取资源Service.class.getClassLoader().getResource("../../../config
包com.jnu509中的文件:
package com.jnu509;import com.jnu509.service.Service;public class Demo8Servlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {Service service = new Service();service.method();}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}}
包com.jnu509.service中的文件,可以看到,我们现在要访问项目中的config.properties文件。
package com.jnu509.service;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.util.Properties;public class Service {public void method() throws FileNotFoundException, IOException {Properties prop = new Properties();prop.load(new FileReader(Service.class.getClassLoader().getResource("../../config.properties").getPath()));System.out.println(prop.getProperty("user"));System.out.println(prop.getProperty("password"));}}
上面提到,要使用java的类加载器,getClassLoader,类加载器既然能将类加载进内存那么也可以加载资源文件。具体访问方式看下图:
0 0
- ServletContext和servletconfig区别
- ServletContext和ServletConfig分析
- ServletContext,ServletConfig和Sesion
- ServletConfig和ServletContext
- ServletConfig和ServletContext
- ServletConfig和ServletContext
- ServletConfig和ServletContext
- ServletConfig和ServletContext
- (24)ServletConfig和ServletContext
- ServletConfig和ServletContext
- ServletConfig和ServletContext
- ServletConfig和ServletContext
- ServletConfig和ServletContext
- ServletConfig和ServletContext
- ServletConfig和ServletContext
- servletConfig和servletContext
- ServletConfig和ServletContext
- ServletConfig和ServletContext
- Genymotion
- csdn网盘入口
- 平衡二叉树之二(删除节点)
- Java密码学原型算法实现——第二部分:单钥加密算法
- MSSQL、Mysql数据库容灾灾备
- ServletConfig和ServletContext
- java中this这个概念初学者非常难理解,请举例说明
- MYSQLdump参数详解
- struts2示例代码-多个文件上传
- Codeforces 109 Div1
- DbCAD dev CAD/DWG/DXF编辑与展示控件CAD控件网
- .net完成.net后台和手机客户端对接的接口程序
- 定义一种新的图片格式.gnet
- pppoe 协议相关