java_web之ServletContext应用

来源:互联网 发布:手机知乎怎么看提问者 编辑:程序博客网 时间:2024/05/20 03:39
Servlet容器在启动时会加载Web应用,并为每个Web应用创建唯一的对应的ServletContext对象.它代表当前web应用,可以把ServletContext看成是一个Web应用的服务器端组件的共享内存.在ServletContext中可以存放共享数据,它提供了4个读取或设置共享数据的方法.其方法有:

  • setAttribute(String name,Objectobject):把一个对象和一个属性名绑定,将这个对象存储在ServletContext中
  • getAttribute(string name):根据给定的属性名返回所绑定的对象
  • removeAttribute(Stringname):根据给定的属性名从ServletContext中删除相应的属性
  • getattributeNames():返回一个Enumeration对象,它包含了存储在Servlet对象中的所有属性名

ServletConfig对象中维护了ServletContext对象的引用,开发人员在编写servlet时,可以通过ServletConfig.getServletContext方法获得ServletContext对象。

   也可以使用 this.getServletContext方法

由于一个WEB应用中的所有Servlet共享同一个ServletContext对象,因此Servlet对象之间可以通过ServletContext对象来实现通讯。多个Servlet通过ServletContext对象实现数据共享。

ServletContext对象通常也被称之为context域对象。(requestsessionpage

  setAttribute(),getAttribute();

相关举例:


实现Servlet的转发。

 RequestDispatcher rd = getServletContext().getRequestDispatcher(“/1.jsp”);

rd.forward(request,response);

如何把数据传到 1.jsp ?(可以通过request域,不能用context域)

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

/* 返回一个RequestDispatcher对象,作为包装器为资源位于给定的路径。

 一个RequestDispatcher对象可用于转发请求资源或包括资源在一个响应。资源可以是动态的还是静态的。 

路径名必须开始以一个“/”和被解释为相对于当前上下文根。使用getContext获取对资源RequestDispatcher在外国上下文。

  该方法返回null如果ServletContext RequestDispatcher不能返回。*/

this.getServletContext().setAttribute("today","Yestoday");

RequestDispatcher rd=this.getServletContext().getRequestDispatcher("/index.jsp");

/* 从一个servlet将请求转发到另一个资源(servlet、JSP文件,或HTML文件)在服务器。

 这种方法允许一个servlet来做初步的请求处理和另一个资源生成响应。*/

         rd.forward(request, response);

}

    public void doGet(HttpServletRequest request, HttpServletResponse response)      throws ServletException, IOException {      //获取ServletContext的两种方式      //第一种               ServletContext sc=this.getServletConfig().getServletContext();               //第二种              ServletContext sc2=this.getServletContext();                    }      public void doGet(HttpServletRequest request, HttpServletResponse response)      throws ServletException, IOException {      //多个Servlet实现数据共享         String data="abcd";                 this.getServletContext().setAttribute("lenovo", data);      }      public void doGet(HttpServletRequest request, HttpServletResponse response)      throws ServletException, IOException {      ////配合Context2使用,通过属性值lenovo调出data的值;             String value = (String)this.getServletContext().getAttribute("lenovo");             System.out.println(value);      public void doGet(HttpServletRequest request, HttpServletResponse response)      throws ServletException, IOException {               //在web.xml文件中查找data属性相应的值;    System.out.println(this.getServletContext().getInitParameter("data"));      }  

ServletContext应用

获取WEB应用的初始化参数。

web.xml文件中插入;

<context-param>

    <param-name> data</param-name>

   <param-value> xxxx</param-value>

</context-param>

ServletConfigServletContext的区别

整个Web应用只有一个ServletContext,在部署Web应用的时候,容器会建立这一个ServletContext对象,这个上下文对Web应用中的每个ServletJSP都可用。 

Web应用中的各个Servlet都有自己的ServletConfig,它只对当前Servlet有效。

还有一个是通过一般的java类读取配置文件的内容:

利用ServletContext对象读取资源文件。

得到文件路径

读取资源文件的三种方式

.properties文件(属性文件)

这个首先必须写配置文件:

driver=com.mysql.jdbc.Driver

url=jdbc:mysql://localhost:3306/test

username=root

password=root

public void test1() throws IOException {

InputStream in= this.getServletContext().getResourceAsStream(

"/WEB-INF/classes/db.properties");

Properties prop = new Properties();

prop.load(in);

String driver = prop.getProperty("driver");

String url = prop.getProperty("url");

String username = prop.getProperty("username");

String password = prop.getProperty("password");

System.out.println(url);

}

// 通过ServletContext读取配置文件 方法2,获取了文件的绝对路径

public void test2() throws IOException {

String path = this.getServletContext().getRealPath(

"/WEB-INF/classes/db.properties");

int i = path.lastIndexOf("\\");

System.out.println(path.substring(i + 1));

FileInputStream fis = new FileInputStream(path);

Properties prop = new Properties();

prop.load(fis);

String driver = prop.getProperty("driver");

System.out.println(driver);

}

也可以再写一个工具类:

InputStream in = StudentDao.class.getClassLoader().getResourceAsStream("db.properties");

Properties prop = new Properties();

try {

prop.load(in);

catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

 driver = prop.getProperty("driver");

String url = prop.getProperty("url");

String username = prop.getProperty("username");

String password = prop.getProperty("password");

然后再写一个dao类,提供增删改查;


转载自:http://blog.csdn.net/liuxiaogangqq/article/details/8099068

原创粉丝点击