<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      Servlet簡介和ServletContext

      0x01: 什么是Servlet?

      • 是sun公司開發動態web的技術
      • 實現了servlet接口的Java程序

      0x02: Servlet的實現類有哪些?

      Servlet接口默認有兩個實現類

      • HttpServlet
      • GenericServlet
        HttpServlet 繼承自 GenericServlet, 一般我們自己寫類只需要繼承HttpServlet,重寫方法就可以了
      public class HelloServlet extends HttpServlet {
          
          //由于get或者post只是請求實現的不同的方式,可以相互調用,業務邏輯都一樣;
          @Override
          protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
              //ServletOutputStream outputStream = resp.getOutputStream();
              PrintWriter writer = resp.getWriter(); //響應流
              writer.print("Hello,Serlvet");
          }
      
          @Override
          protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
              doGet(req, resp);
      
      

      0x03:Servlet的原理

      瀏覽器發送http請求給web容器,web容器產生Request和Response對象,這兩個對象調用servlet接口的Service方法,Service方法再將返回的Responce信息返回給Responce對象,web容器從Responce對象讀取將其響應給客戶端。(注意,如果web容器是首次被訪問,需要先把我們的java類變成class字節碼文件)

      什么是ServletContext

      Web容器在啟動時,為每個web程序創建一個對應的ServletContext,它代表當前web應用

      ServletContext的一些用法和特性

      在同一個web應用中,不同的servlet可以共享數據

      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 username = "黎星澄"; //數據
              context.setAttribute("username",username); //將一個數據保存在了ServletContext中,名字為:username 。值 username
      
          }
      
      }
      
      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().print("名字"+username);
      
          }
      
          @Override
          protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
              doGet(req, resp);
          }
      }
      
      

      獲取初始化參數

          <!--在web.xml配置一些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"); //得到name為url的參數
          resp.getWriter().print(url);
      }
      
      

      請求轉發

      @Override
      protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
          ServletContext context = this.getServletContext();
          System.out.println("進入了ServletDemo04");
          //RequestDispatcher requestDispatcher = context.getRequestDispatcher("/xxx");           //轉發的請求路徑
          //requestDispatcher.forward(req,resp);      //調用forward實現請求轉發;
          context.getRequestDispatcher("/gp").forward(req,resp);
      }
      //請求轉發和重定向的區別?
      請求轉發:像當于在服務器內部將請求轉給請求的資源,然后由服務器相應給客戶端
      重定向:服務器將請求資源作為響應信息響應給客戶端,客戶端根據這個信息對服務器發起請求
      

      讀取資源文件
      在java目錄下新建properties
      在resources目錄下新建properties
      都被打包到同一路徑:classes

      username=admin
      password=123456
      
      public class Servlet_Properties extends HttpServlet {
          @Override
          protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      
              InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/com/hu/servlet/test.properties"); //輸入流
      
              Properties prop = new Properties();                                                                               //創建一個Properties對象
              prop.load(is);                                                                                                    //將讀取到的加載到prop里
              String user = prop.getProperty("username");
              String pwd = prop.getProperty("password");
      
              resp.getWriter().print(user+":"+pwd);
      
          }
      
          @Override
          protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
              doGet(req, resp);
          }
      }
      
      
      posted @ 2021-01-01 14:12  折學Lex  閱讀(91)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 国产在线视频www色| 成人亚洲一区二区三区在线| 精品久久久中文字幕一区| 国产精品亚洲mnbav网站| 日本一区二区三区东京热| 精品国产一区二区三区av片| 亚洲无?码A片在线观看| 精品一区二区三区在线视频观看| 欧洲中文字幕一区二区| 亚洲熟女一区二区av| 欧美另类图区清纯亚洲| 亚洲午夜爱爱香蕉片| 精品亚洲香蕉久久综合网| 五月婷婷开心中文字幕| 国产精品成人免费视频网站京东| 国产av一区二区三区精品| a4yy私人毛片| 亚洲码亚洲码天堂码三区| 亚洲高清WWW色好看美女| 国产精品久久久久久爽爽爽| 日韩精品一区二区三区激情| 午夜三级成人在线观看| 国产成人无码综合亚洲日韩| 国内精品视这里只有精品| 亚洲一区成人在线视频| 免费午夜无码片在线观看影院| 起碰免费公开97在线视频| 亚洲国产午夜精品福利| 亚洲男人第一无码av网站| 亚洲男人av天堂久久资源| 中文字幕丰满乱子无码视频| 亚洲精品成人网久久久久久| 国产精品自拍午夜福利| 日本韩国日韩少妇熟女少妇| 韩国V欧美V亚洲V日本V| 中国熟女仑乱hd| 亚洲av中文一区二区| 国产蜜臀在线一区二区三区| 激情综合五月丁香亚洲| 国产精品高清中文字幕| 337p西西人体大胆瓣开下部|