日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HttpServlet概述及应用

發(fā)布時(shí)間:2023/12/15 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HttpServlet概述及应用 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Servlet采用了接口的設(shè)計(jì)思想,由Tomcat提供

文章目錄

    • 實(shí)現(xiàn)Servlet方法詳細(xì)解讀

HttpServlet是?個(gè)與HTTP協(xié)議相關(guān)的Servlet,專門用來處理HTTP協(xié)議的請(qǐng)求響應(yīng)。

  • 在HttpServlet類的service方法內(nèi)部,根據(jù)HTTP協(xié)議請(qǐng)求方式不同,執(zhí)行不同的doXXX的方法(get請(qǐng)求執(zhí)行doGet方法,如果是post請(qǐng)求就會(huì)執(zhí)行doPost方法)
  • 繼承了HttpServlet之后不需要重寫service方法,只需要重寫doGet和doPost方法即可。

實(shí)現(xiàn)Servlet方法詳細(xì)解讀

import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException;public class MyServlet extends HttpServlet {@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.getWriter().print("<font color = 'red'>MyServlet doPost</font>");}@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.getWriter().print("<font color = 'red'>MyServlet doGet</font>");} } <?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"><servlet><servlet-name>MyServlet</servlet-name><servlet-class>com.byc.servlet.MyServlet</servlet-class></servlet><servlet-mapping><servlet-name>MyServlet</servlet-name><url-pattern>/my</url-pattern> <!--匹配路徑--></servlet-mapping> </web-app>


瀏覽器客戶端只響應(yīng)了doGet的輸出:
servlet接口下的方法service()方法
在tomcat運(yùn)行的時(shí)候就會(huì)自動(dòng)完成(HttpServletRequest req, HttpServletResponse resp)中req、resp對(duì)象的創(chuàng)建

String method = req.getMethod();//獲取定義的請(qǐng)求方式是post請(qǐng)求還是get請(qǐng)求 /** post請(qǐng)求只有在表單中存在,get請(qǐng)求則是顯示在地址欄中 */

在HttpServlet中定義doGet方法

/*** Called by the server (via the <code>service</code> method) to* allow a servlet to handle a GET request.** <p>Overriding this method to support a GET request also* automatically supports an HTTP HEAD request. A HEAD* request is a GET request that returns no body in the* response, only the request header fields.** <p>When overriding this method, read the request data,* write the response headers, get the response's writer or* output stream object, and finally, write the response data.* It's best to include content type and encoding. When using* a <code>PrintWriter</code> object to return the response,* set the content type before accessing the* <code>PrintWriter</code> object.** <p>The servlet container must write the headers before* committing the response, because in HTTP the headers must be sent* before the response body.** <p>Where possible, set the Content-Length header (with the* {@link javax.servlet.ServletResponse#setContentLength} method),* to allow the servlet container to use a persistent connection* to return its response to the client, improving performance.* The content length is automatically set if the entire response fits* inside the response buffer.** <p>When using HTTP 1.1 chunked encoding (which means that the response* has a Transfer-Encoding header), do not set the Content-Length header.** <p>The GET method should be safe, that is, without* any side effects for which users are held responsible.* For example, most form queries have no side effects.* If a client request is intended to change stored data,* the request should use some other HTTP method.** <p>The GET method should also be idempotent, meaning* that it can be safely repeated. Sometimes making a* method safe also makes it idempotent. For example,* repeating queries is both safe and idempotent, but* buying a product online or modifying data is neither* safe nor idempotent.** <p>If the request is incorrectly formatted, <code>doGet</code>* returns an HTTP "Bad Request" message.** @param req an {@link HttpServletRequest} object that* contains the request the client has made* of the servlet** @param resp an {@link HttpServletResponse} object that* contains the response the servlet sends* to the client** @exception IOException if an input or output error is* detected when the servlet handles* the GET request** @exception ServletException if the request for the GET* could not be handled** @see javax.servlet.ServletResponse#setContentType*/protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException{String protocol = req.getProtocol();String msg = lStrings.getString("http.method_get_not_supported");if (protocol.endsWith("1.1")) {resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);} else {resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);}}

構(gòu)建的實(shí)體類繼承HttpServlet,其中doGet方法作為父類,再在實(shí)體類中重寫doGet方法,在客戶端瀏覽器中調(diào)用子類的doGet方法,由于子類service方法沒有重寫,所以就直接調(diào)用父類的service方法

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的HttpServlet概述及应用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。