HttpServlet概述及应用
生活随笔
收集整理的這篇文章主要介紹了
HttpServlet概述及应用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Servlet采用了接口的設計思想,由Tomcat提供
文章目錄
- 實現Servlet方法詳細解讀
HttpServlet是?個與HTTP協議相關的Servlet,專門用來處理HTTP協議的請求響應。
- 在HttpServlet類的service方法內部,根據HTTP協議請求方式不同,執行不同的doXXX的方法(get請求執行doGet方法,如果是post請求就會執行doPost方法)
- 繼承了HttpServlet之后不需要重寫service方法,只需要重寫doGet和doPost方法即可。
實現Servlet方法詳細解讀
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>
瀏覽器客戶端只響應了doGet的輸出:
servlet接口下的方法service()方法
在tomcat運行的時候就會自動完成(HttpServletRequest req, HttpServletResponse resp)中req、resp對象的創建
在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);}}構建的實體類繼承HttpServlet,其中doGet方法作為父類,再在實體類中重寫doGet方法,在客戶端瀏覽器中調用子類的doGet方法,由于子類service方法沒有重寫,所以就直接調用父類的service方法
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的HttpServlet概述及应用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Servlet使用基本步骤及功能实现
- 下一篇: 如何显示日期和时间(电脑右下角显示日期和