ServletContext——被称为context域对象

ServletContext——被称为context域对象

1.servletContext域:1,是一个容器 2。作用范围是应用程序范围
服务器启动时,他会为每个WEB应用程序创建一个ServletContext对象,它代表当前web应用。停掉服务器时,ServletContext也就没了。一个web应用中所有Servlet对象共享一个ServletContext,所以多个Servlet通过ServletContext对象实现数据共享
public class ServletDemo6 extends HttpServlet {

//servletContext示例
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//得到servletContext
ServletContext context=this.getServletConfig().getServletContext();——两种方法
ServletContext context2=this.getServletContext();——两种方法,这个比较好写,用这个
String data=”aaaaa”;
context.setAttribute(“data”, data);
}
}

使用例子:比如可以用到聊天室
先写一个Servlet
public class ServletDemo7 extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String data=”ddd”;

this.getServletContext.setAttribute(“data”,data);
}

}

再写另一个Servlet
public class ServletDemo8 extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String value = (String) this.getServletContext().getAttribute("data");
    response.getOutputStream().write(value.getBytes());
}

}

2.还可以:这样给整个web应用添一个参数

data
zzzz


data1
zzzz

调用:获得web应用初始化参数
String value=this.getServletContext.getInitParameter(“data”);
还可以获取所有,这里就不写了

3.还可以实现Servlet转发
转发:你找我借钱,我没有,我帮你找别人。客户机一次请求
重定向:你找我借钱,我没有,我让你自己去找别人。客户机两次请求
例子:
public class ServletDemo10 extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
String data="aaaaaaaaaa";
RequestDispatcher rd=this.getServletContext().getRequestDispatcher("/1.jsp");——得到转发对象,即转到哪里去
rd.forward(request, response);  ——实现转发
}

}

4.ServletContext读取web应用中的资源文件
public class ServletDemo11 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//test5();
File file=new File(“”);
response.getOutputStream().write(file.getAbsolutePath().getBytes());

}

方法一:这个方法比较好:这个方法可以得到当前读取到资源的名称

private void test5() throws IOException {
    //得到绝对路径
    String path=this.getServletContext().getRealPath("/WEB-INF/classes/db.properties");

//再通过传统方法读文件
String filename=path.substring(path.lastIndexOf(“\”));——这个方法可以得到当前读取到资源的名称
FileInputStream in2=new FileInputStream(path);
Properties props=new Properties();
props.load(in2);
System.out.println(props.getProperty(“url”));

}

方法二:
private void test4() throws IOException {
//in2的相对路径是tomcat的bin目录,所以这种方法要在该目录下建立文件夹classes,并把文件放在这里,所以不要用这个方法
FileInputStream in2=new FileInputStream(“classes/db.properties”);
Properties props=new Properties();
props.load(in2);
System.out.println(props.getProperty(“url”));
}

方法三:这个方法比较好:比较简便,直接得到流,但这个方法不能得到当前读取到资源的名称

private void test3() throws IOException {
    InputStream in=this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");——得到资源文件位置
    Properties props=new Properties();——一下代码记住!!!对所有properties都这样写
    props.load(in);——加载进来
    System.out.println(props.getProperty("url"));

System.out.println(props.getProperty(“username”));

}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章