天天看點

ServletContext——被稱為context域對象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”));

}
           

}

繼續閱讀