java_web之ServletContext应用

来源:互联网 发布:常用国家域名 编辑:程序博客网 时间:2024/05/20 05:25

WEB容器在启动时,它会为每个WEB应用程序都创建一个对应的ServletContext对象,它代表当前web应用。

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);

}

[java] view plaincopy
  1. public void doGet(HttpServletRequest request, HttpServletResponse response)  
  2. throws ServletException, IOException {  
  3. //获取ServletContext的两种方式  
  4. //第一种  
  5.          ServletContext sc=this.getServletConfig().getServletContext();  
  6.          //第二种  
  7.         ServletContext sc2=this.getServletContext();  
  8.           
  9. }  
  10. public void doGet(HttpServletRequest request, HttpServletResponse response)  
  11. throws ServletException, IOException {  
  12. //多个Servlet实现数据共享  
  13.    String data="abcd";  
  14.            this.getServletContext().setAttribute("lenovo", data);  
  15. }  
  16. public void doGet(HttpServletRequest request, HttpServletResponse response)  
  17. throws ServletException, IOException {  
  18. ////配合Context2使用,通过属性值lenovo调出data的值;  
  19.        String value = (String)this.getServletContext().getAttribute("lenovo");  
  20.        System.out.println(value);  
  21. public void doGet(HttpServletRequest request, HttpServletResponse response)  
  22. throws ServletException, IOException {  
  23.      
  24. //在web.xml文件中查找data属性相应的值;    System.out.println(this.getServletContext().getInitParameter("data"));  
  25. }  

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类,提供增删改查;

0 0