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

}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ServletContext是Java Web的一个重要接口,它代表了Web应用程序在服务器的上下文环境。在一个Web应用程序,每个Servlet都可以访问同一个ServletContext对象,从而实现Servlet之间的数据共享和通信。 在Java,可以使用ServletConfig对象的getServletContext()方法来获取ServletContext对象,然后使用该对象的方法来实现一系列操作,例如: 1. 获取Web应用程序的初始化参数:可以使用ServletContext对象的getInitParameter()方法来获取Web应用程序的初始化参数,例如数据库连接等配置信息。 ```java String username = context.getInitParameter("username"); ``` 2. 获取Web应用程序的真实路径:可以使用ServletContext对象的getRealPath()方法来获取Web应用程序的真实路径,例如获取Web应用程序的一个文件的绝对路径。 ```java String path = context.getRealPath("/WEB-INF/config.properties"); ``` 3. 在ServletContext保存数据:可以使用ServletContext对象的setAttribute()方法来在ServletContext保存数据,从而实现Servlet之间的数据共享。 ```java context.setAttribute("key", value); ``` 4. 从ServletContext获取数据:可以使用ServletContext对象的getAttribute()方法来从ServletContext获取数据,例如获取其他Servlet保存的数据。 ```java Object value = context.getAttribute("key"); ``` 需要注意的是,ServletContext对象的作用域为整个Web应用程序,因此需要注意数据的安全性和可靠性。另外,ServletContext对象是在Web应用程序启动创建的,因此可以在Servlet的init()方法获取ServletContext对象并进行初始化操作,例如读取配置文件等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值