(JavaWeb)ServletContext對象

ServletContext

  • web容器在啓動的時候,它會爲每個web程序都創建一個對應的ServletContext對象,它代表了當前的web應用;

1.共享數據

Servlet1可以將數據存放在ServletContext中,Servlet2和Servlet3可以在ServletContext中取得Servlet1在ServletContext中存放的數據。
在這裏插入圖片描述
在HelloServlet中存放String數據 伊澤瑞爾。

public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //獲取servletContext對象
        ServletContext servletContext = this.getServletContext();
        String name = "伊澤瑞爾";
        //以鍵值對的形式將數據放入servletContext對象中
        servletContext.setAttribute("name",name);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

在GetNameServlet中取得數據並顯示在網站上

public class GetNameServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //獲取ServletContext對象
        ServletContext servletContext = this.getServletContext();
        //獲取HelloServlet中在獲取ServletContext對象中存放的數據
        String name = (String) servletContext.getAttribute("name");
        //設置響應信息爲中文
        resp.setContentType("text/html");
        resp.setCharacterEncoding("utf-8");
        resp.getWriter().write(name);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

先訪問HelloServlet頁面將數據存放在ServletContext中,再訪問GetNameServlet拿出數據並顯示在網頁。
在這裏插入圖片描述

2.獲取初始化參數

    <!--配置一些web應用初始化參數-->
    <context-param>
        <param-name>url</param-name>
        <param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
    </context-param>
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    ServletContext context = this.getServletContext();
    String url = context.getInitParameter("url");
    resp.getWriter().print(url);
}

3.請求轉發

public class ServletDemo01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //創建servletContext對象
        ServletContext servletContext = this.getServletContext();
        //請求轉發
        servletContext.getRequestDispatcher("/getName").
        	forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

運行,顯示的是GetNameServlet的頁面,這裏null是由於沒有訪問hello的緣故。
在這裏插入圖片描述
顯示的是GetNameServlet的頁面,但是路徑卻還是自己的。
在這裏插入圖片描述
如圖,A向B請求資源,B向C請求資源,B將C返回的資源再返回給A,A自始至終沒有接觸C,所以顯示的還是B的路徑。
在這裏插入圖片描述

4.讀取資源文件

Properties

  • 在resources目錄下新建db.properties文件
username=ez
password=123456
public class ServletDemo02 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //創建servletContext對象
        ServletContext servletContext = this.getServletContext();
        //獲取properties文件的字節輸入流對象
        InputStream is = servletContext.getResourceAsStream("/WEB-INF/classes/db.properties");
        //創建properies對象
        Properties prop = new Properties();
        //加載流
        prop.load(is);
        String user = prop.getProperty("username");
        String pwd = prop.getProperty("password");
        //顯示
        resp.getWriter().write(user+":"+pwd);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

在這裏插入圖片描述

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