01-Servlet(1)

請求類型

  1. GET
    • 在瀏覽器地址欄中輸入某個URL地址或單擊網(wǎng)頁上的一個超鏈接時,瀏覽器發(fā)出的HTTP請求消息的請求方式為GET
    • 如果網(wǎng)頁中的<form>表單元素的 method 屬性被設(shè)置為了“GET”,瀏覽器提交這個FORM表單時生成的HTTP請求消息的請求方式也為GET
    • 使用GET請求方式給WEB服務(wù)器傳遞參數(shù)的格式: http://www.atguigu.com/counter.jsp?name=lc&password=123
    • 使用GET方式傳送的數(shù)據(jù)量一般限制在 1KB 以下
  2. POST
    • POST 請求方式主要用于向 WEB 服務(wù)器端程序提交 FORM 表單中的數(shù)據(jù): form 表單的 method 置為 POST
    • POST 方式將各個表單字段元素及其數(shù)據(jù)作為 HTTP 消息的實(shí)體內(nèi)容發(fā)送給 WEB 服務(wù)器,傳送的數(shù)據(jù)量要比使用GET方式傳送的數(shù)據(jù)量大得多。

獲取參數(shù)

  1. Servlet的service()
    • Servlet 的 service() 方法用于應(yīng)答請求: 因?yàn)槊看握埱蠖紩{(diào)用 service() 方法
public void service(ServletRequest request, ServletResponse response)
            throws ServletException, IOException
  • ServletRequest: 封裝了請求信息. 可以從中獲取到任何的請求信息
  • ServletResponse: 封裝了響應(yīng)信息, 如果想給用戶什么響應(yīng), 具體可以使用該接口的方法實(shí)現(xiàn)
  • 這兩個接口的實(shí)現(xiàn)類都是服務(wù)器給予實(shí)現(xiàn)的, 并在服務(wù)器調(diào)用 service 方法時傳入.


    servlet.png

    登陸界面.png

    web.xml.png
  1. ServletRequest
    封裝了請求信息. 可以從中獲取到任何的請求信息.
    • 獲取請求參數(shù) getParameter()
      • String getParameter(String name): 根據(jù)請求參數(shù)的名字, 返回參數(shù)值.若請求參數(shù)有多個值(例如 checkbox), 該方法只能獲取到第一個提交的值.
      • 獲取請求的參數(shù).png
    • getParameterValues()
      • String[] getParameterValues(String name): 根據(jù)請求參數(shù)的名字, 返回請求參數(shù)對應(yīng)的字符串?dāng)?shù)組
      • getParameterValues.png
    • getParameterNames()
      • Enumeration getParameterNames(): 返回參數(shù)名對應(yīng)的 Enumeration 對象, 類似于 ServletConfig(或 ServletContext) 的 getInitParameterNames() 方法
      • getParameterNames.png
    • getParameterMap()
      • Map getParameterMap(): 返回請求參數(shù)的鍵值對: key: 參數(shù)名, value: 參數(shù)值, String 數(shù)組類型
      • getParameterMap.png
  2. 獲取請求相關(guān)信息
    • getRequestURI
    HttpServletRequest httpServletRequest = (HttpServletRequest) request;
    String requestURI = httpServletRequest.getRequestURI();
    System.out.println(requestURI); //  /day_29/loginServlet
  • getMethod()獲取請求方式:
    String method = httpServletRequest.getMethod();
    System.out.println(method); //GET
  • getQueryString
//若是一個 GET 請求, 獲取請求參數(shù)對應(yīng)的那個字符串, 即 ? 后的那個字符串. 
    String queryString = httpServletRequest.getQueryString();
    System.out.println(queryString); //user=atguigu&password=123456&interesting=game&interesting=party&interesting=shopping
  • getServletPath
 //獲取請求的 Serlvet 的映射路徑 
   String servletPath = httpServletRequest.getServletPath();
   System.out.println(servletPath);  //  /loginServlet
獲取請求相關(guān)信息.png
  1. ServletResponse
    封裝了響應(yīng)信息, 如果想給用戶什么響應(yīng), 具體可以使用該接口的方法實(shí)現(xiàn)
  2. getWriter
    • getWriter(): 返回 PrintWriter 對象. 調(diào)用該對象的 print() 方法, 將把 print() 中的參數(shù)直接打印到客戶的瀏覽器上.
  3. setContentType
    • 設(shè)置響應(yīng)的內(nèi)容類型: response.setContentType("application/msword");
  4. sendRedirect
    • void sendRedirect(String location): 請求的重定向. (此方法為 HttpServletResponse 中定義.)

實(shí)例

  • 在 web.xml 文件中設(shè)置兩個 WEB 應(yīng)用的初始化參數(shù), user, password.
  • 定義一個 login.html, 里邊定義兩個請求字段: user, password. 發(fā)送請求到 loginServlet
  • 在創(chuàng)建一個 LoginServlet, 在其中獲取請求的 user, password. 比對其和 web.xml 文件中定義的請求參數(shù)是否一致
  • 若一致, 響應(yīng) Hello:xxx, 若不一致, 響應(yīng) Sorry: xxx xxx 為 user.
  1. 創(chuàng)建html


    創(chuàng)建html.png
  2. 創(chuàng)建xml


    創(chuàng)建xml.png
  3. loginServlet


    loginServlet.png
  4. GenericServlet
    • 是一個 Serlvet. 是 Servlet 接口和 ServletConfig 接口的實(shí)現(xiàn)類. 但是一個抽象類. 其中的 service 方法為抽象方法
    • 如果新建的 Servlet 程序直接繼承 GenericSerlvet 會使開發(fā)更簡潔.
    • 具體實(shí)現(xiàn)
      • . 在 GenericServlet 中聲明了一個 SerlvetConfig 類型的成員變量, 在 init(ServletConfig) 方法中對其進(jìn)行了初始化
      • 利用 servletConfig 成員變量的方法實(shí)現(xiàn)了 ServletConfig 接口的方法
      • 還定義了一個 init() 方法, 在 init(SerlvetConfig) 方法中對其進(jìn)行調(diào)用, 子類可以直接覆蓋 init() 在其中實(shí)現(xiàn)對 Servlet 的初始化
      • 不建議直接覆蓋 init(ServletConfig), 因?yàn)槿绻浘帉?super.init(config); 而還是用了 SerlvetConfig 接口的方法,則會出現(xiàn)空指針異常
      • 新建的 init(){} 并非 Serlvet 的生命周期方法. 而 init(ServletConfig) 是生命周期相關(guān)的方法.
      • 1.png
      • 2.png
      • 3.png
    • 格式
public abstract class GenericServlet implements Servlet, ServletConfig {

    /** 以下方法為 Servlet 接口的方法 **/
    @Override
    public void destroy() {}

    @Override
    public ServletConfig getServletConfig() {
        return servletConfig;
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    private ServletConfig servletConfig;
    
    @Override
    public void init(ServletConfig arg0) throws ServletException {
        this.servletConfig = arg0;
        init();
    }

    public void init() throws ServletException{}

    @Override
    public abstract void service(ServletRequest arg0, ServletResponse arg1)
            throws ServletException, IOException;

    /** 以下方法為 ServletConfig 接口的方法 **/
    @Override
    public String getInitParameter(String arg0) {
        return servletConfig.getInitParameter(arg0);
    }

    @Override
    public Enumeration getInitParameterNames() {
        return servletConfig.getInitParameterNames();
    }
}
  1. HttpServlet
    是一個 Servlet, 繼承自 GenericServlet. 針對于 HTTP 協(xié)議所定制.
    實(shí)際開發(fā)中, 直接繼承 HttpServlet, 并根據(jù)請求方式復(fù)寫 doXxx() 方法即可.
    好處: 直接由針對性的覆蓋 doXxx() 方法; 直接使用 HttpServletRequest 和 HttpServletResponse, 不再需要強(qiáng)轉(zhuǎn).
    • 在 service() 方法中直接把 ServletReuqest 和 ServletResponse 轉(zhuǎn)為 HttpServletRequest 和 HttpServletResponse.并調(diào)用了重載的 service(HttpServletRequest, HttpServletResponse)
    • 在 service(HttpServletRequest, HttpServletResponse) 獲取了請求方式: request.getMethod(). 根據(jù)請求方式有創(chuàng)建了doXxx() 方法(xxx 為具體的請求方式, 比如 doGet, doPost)
    • 代碼
@Override
 public void service(ServletRequest req, ServletResponse res)
    throws ServletException, IOException {

    HttpServletRequest  request;
    HttpServletResponse response;
    
    try {
        request = (HttpServletRequest) req;
        response = (HttpServletResponse) res;
    } catch (ClassCastException e) {
        throw new ServletException("non-HTTP request or response");
    }
    service(request, response);
}

public void service(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    //1. 獲取請求方式.
    String method = request.getMethod();
    
    //2. 根據(jù)請求方式再調(diào)用對應(yīng)的處理方法
    if("GET".equalsIgnoreCase(method)){
        doGet(request, response);
    }else if("POST".equalsIgnoreCase(method)){
        doPost(request, response);
    }
}

public void doPost(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException{
    // TODO Auto-generated method stub
    
}

public void doGet(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException {
    // TODO Auto-generated method stub
    
}

  • 格式
  • 4.png

    5.png
  • 使用


    6.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容