ServletContext 學習


ServletContext

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

1、共享數據

​ 在這個Servlet中保存了數據,就可以在另外一個servlet取到

首先將數據存入HelloServlet中,然后可以在另外一個Servlet類中取出

image

@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //this.getInitParameter()   //初始化參數
        //this.getServletConfig()     Servlet配置
        //this.getServletContext()    Servlet 上下文

        ServletContext context = this.getServletContext();

        String name = "逍遙子";//數據
        context.setAttribute("name",name);//將一個數據保存在了 ServletContext 中
        System.out.println("數據已經存放!!!!");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
@WebServlet("/getc")
public class ReadServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //數據
        ServletContext context = this.getServletContext();
        String name = (String) context.getAttribute("name");
        //響應,設置編碼
        resp.setContentType("text/html");
        resp.setCharacterEncoding("utf-8");
        resp.getWriter().print("名字"+name);
    }

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

2、獲取初始化參數

可以獲得在xml中初始化保存的參數

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
        <!-- 配置一些web應用的初始化參數 -->
        <context-param>
            <param-name>url</param-name>
            <param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
        </context-param>
    
</web-app>
@WebServlet("/gp")
public class ServletDeom03 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //數據
        ServletContext context = this.getServletContext();
        String url = context.getInitParameter("url");
        resp.getWriter().print(url);
    }

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

3、請求轉發

轉發並不會改變請求的路徑,重定向才會改變請求的路徑

轉發是間接獲取到資源,重定向是直接拿到資源

@WebServlet("/sd4")
public class Servletdemo04 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //數據
        System.out.println("進入了demo04頁面");
        ServletContext context = this.getServletContext();
        //RequestDispatcher requestDispatcher = context.getRequestDispatcher("/gp");//轉發路徑
        //requestDispatcher.forward(req,resp);//調用forward實現請求轉發
        context.getRequestDispatcher("/gp").forward(req,resp);//請求轉發
    }

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

4、讀取資源文件

Properties

  • 在java目錄下新建properties
  • 在resource目錄下新建properties

發現:都被打包到了同一路徑下:classes,我們俗稱這個路徑為classpath

注:在java目錄下創建的properties文件,若想導出成功,還需配置xml文件

<!--  在build中配置resource,來防止我們資源導出失敗的問題-->
  <build>
    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <includes>
          <include>**/*.properties</include>
          <include>**/*.xml</include>
        </includes>
        <filtering>true</filtering>
      </resource>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.properties</include>
          <include>**/*.xml</include>
        </includes>
        <filtering>true</filtering>
      </resource>
    </resources>
  </build>

讀文件思路:需要一個文件流;

@WebServlet("/sd5")
public class ServletDemo05 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        /**
         * 1、導入properties流,根據項目的相對地址
         * 2、創建properties對象
         * 3、將剛剛的流加載到properties對象中
         * 4、成功引入properties文件
         */
        InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
        Properties prop = new Properties();
        prop.load(is);
        String username = prop.getProperty("username");
        String password = prop.getProperty("password");
        resp.getWriter().print("username = "+username);
        resp.getWriter().print("password = "+password);
    }

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM