servletContext

Servlet 中的servletContext

一、什么是servletContext?

servletContext接口是Servlet中最大的一个接口,呈现了web应用的Servlet视图。ServletContext实例是通过 getServletContext()方法获得的,由于HttpServlet继承Servlet的关系GenericServlet类和HttpServlet类同时具有该方法。

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

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

也可以使用 this.getServletContext方法

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

4、ServletContext对象通常也被称之为context域对象。(request,session,page)

setAttribute(),getAttribute();

二、servletContext应用

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

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

xxxx

2、实现Servlet的转发。

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

rd.forward(request,response);

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

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

得到文件路径

读取资源文件的三种方式

.properties文件(属性文件)

三、ServletConfig和ServletContext的区别

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

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

四、案例:

  1. 写出获取ServletContext的两种方式

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

ServletContext context1=this.getServletConfig().getServletContext();

ServletContext context2=this.getServletContext();

}

2.使用ServletContext实现两个Servlet数据共享

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

ServletContext context1=this.getServletConfig().getServletContext();

ServletContext context2=this.getServletContext();

}

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

String value=(String)this.getServletConfig().getServletContext().getAttribute(“context1”);

System.out.println(value);

}

3.设置ServletContext初始化参数,然后对其之。

在web.xml中写上:

data

我的数据共享

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

String value=(String)this.getServletContext().getInitParameter(“data”);

System.out.println(value);

}

  1. 编写一个转发

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

//附一个初始值

this.getServletContext().setAttribute(“username”,”zhangsan”);

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

rd.forward(request, response);

}

<%=application.getAttribute("username")%>

5.通过ServletContext读取配置文件的内容。(使用两种方式)

public void test1(){

try {

InputStream in=this.getServletContext().getResourceAsStream(“/WEB-INF/classes/db.properties”);

Properties pro=new Properties();

pro.load(in);

String driver=pro.getProperty(“driver”);

String url=pro.getProperty(“url”);

String user=pro.getProperty(“user”);

String password=pro.getProperty(“password”);

//System.out.println(driver);

System.out.println(url);

System.out.println(user);

//System.out.println(password);

} catch (IOException e) {

e.printStackTrace();

}

}

public void test2() throws IOException{

String path=this.getServletContext().getRealPath(“/WEB-INF/classes/db.properties”);

//System.out.println(path);

int i=path.lastIndexOf(“\”);

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

FileInputStream fis=new FileInputStream(path);

Properties pro=new Properties();

pro.load(fis);

String driver=pro.getProperty(“driver”);

String url=pro.getProperty(“url”);

String user=pro.getProperty(“user”);

String password=pro.getProperty(“password”);

//System.out.println(driver);

System.out.println(url);

System.out.println(user);

//System.out.println(password);

}

6.通过一般的java类读取配置文件的内容。

private static String driver;

private static String url;

private static String user;

private static String password;

static{

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

Properties pro=new Properties();

try {

pro.load(in);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

driver=pro.getProperty(“driver”);

url=pro.getProperty(“url”);

user=pro.getProperty(“user”);

password=pro.getProperty(“password”);

}

posted @ 2015-06-16 19:55  lllini  阅读(99)  评论(0编辑  收藏  举报