1. 程式人生 > 資訊 >微軟承諾:將積極研究維修權問題

微軟承諾:將積極研究維修權問題

ServletContext

1、簡介

web容器在啟動時會為每個web程式建立一個ServletContext物件,它代表了當前的web應用

2、應用(以下方法現在幾乎不用)

  • 共享資料(setAttribute(),getAttribute())

    在一個Servlet中儲存的資料,可以在另一個Servlet中拿到,例:

    放置類

    public class HelloServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            ServletContext context = this.getServletContext();
            String userName = "張晟偉";
            context.setAttribute("userName",userName);//將userName放進content物件
        }
    }
    

    獲取類

    public class GetServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            ServletContext context = this.getServletContext();
            String userName = (String)context.getAttribute("userName");
            resp.setContentType("text/html");
            resp.setCharacterEncoding("utf-8");
            resp.getWriter().println("名字:"+userName);
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    }
    

    xml

     <servlet>
            <servlet-name>hello</servlet-name>
            <servlet-class>com.zhang.servlet.HelloServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>hello</servlet-name>
            <url-pattern>/hello</url-pattern>
        </servlet-mapping>
        <servlet>
            <servlet-name>getcontext</servlet-name>
            <servlet-class>com.zhang.servlet.GetServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>getcontext</servlet-name>
            <url-pattern>/getc</url-pattern>
        </servlet-mapping>
    

    測試結果:

    原理圖:

  • 獲取初始化引數(getInitParameter())

    java程式碼

    public class ServletDemo_01 extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            ServletContext context = this.getServletContext();
            String url = context.getInitParameter("url");
            resp.getWriter().println(url);
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    }
    

    xml

     <!--
        context-param標籤為配置一些web應用的初始化引數
        -->
        <context-param>
            <param-name>url</param-name>
            <param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
        </context-param>
        <servlet>
            <servlet-name>gp</servlet-name>
            <servlet-class>com.zhang.servlet.ServletDemo_01</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>gp</servlet-name>
            <url-pattern>/gp</url-pattern>
        </servlet-mapping>
    
  • 請求轉發(RequestDispatcher)

    原理圖:

    java程式碼

    public class ServletDemo_04 extends HelloServlet{
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            ServletContext context = this.getServletContext();
    //        RequestDispatcher requestDispatcher = context.getRequestDispatcher("/gp");//請求引數物件,轉發請求路徑
    //        requestDispatcher.forward(req,resp);//呼叫方法實現請求轉發
            context.getRequestDispatcher("/gp").forward(req,resp);//將被註釋的兩行程式碼合併
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    }
    

    xml

    <servlet>
            <servlet-name>respdispatcher</servlet-name>
            <servlet-class>com.zhang.servlet.ServletDemo_04</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>respdispatcher</servlet-name>
            <url-pattern>/rc</url-pattern>
        </servlet-mapping>
    

    實現效果:

    !

  • 讀取資原始檔(Properties)

    新建Properties

    • 在resources下新建properties檔案

      啟動伺服器propertis檔案匯出

    • 在java目錄下新建properties檔案

      注:由於在maven中約定大於配置,所以寫在java目錄下的propertis檔案可能無法被匯出或者生效

      啟動伺服器發現在target檔案相應路徑下並沒有aa.properties檔案

      解決方法

      在pom.xml檔案中配置resources

      <!--在build中配置resources,來防止我們資源匯出失敗問題-->
          <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>
      

      重啟伺服器,問題解決

    • 發現properties檔案都被打包到了同一個路徑下:classes,我們俗稱 這個路徑為 classpath(類路徑)

    讀取Properties

    需要一個流及properties中的內容

    java程式碼

    public class ServletDemo_05 extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            ServletContext context = this.getServletContext();
            InputStream i = context.getResourceAsStream("/WEB-INF/classes/db.properties");
            Properties prop = new Properties();
            prop.load(i);
            String user = prop.getProperty("username");
            String pwd = prop.getProperty("password");
            resp.getWriter().println(user+":"+pwd);
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    }
    

    xml

    <servlet>
        <servlet-name>a_05</servlet-name>
        <servlet-class>com.zhang.servlet.ServletDemo_05</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>a_05</servlet-name>
        <url-pattern>/rp</url-pattern>
    </servlet-mapping>
    

    實現效果