在servlet中默認(rèn)情況下,無論你是get還是post 提交過來 都會經(jīng)過service()方法來處理,然后轉(zhuǎn)向到doGet或是doPost方法,可以看HttpServlet 類的service方法:
原代碼:
- protected void service(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException
- {
- String method = req.getMethod();
- if(method.equals("GET"))
- {
- long lastModified = getLastModified(req);
- if(lastModified == -1L)
- {
- doGet(req, resp);
- } else
- {
- long ifModifiedSince = req.getDateHeader("If-Modified-Since");
- if(ifModifiedSince < (lastModified / 1000L) * 1000L)
- {
- maybeSetLastModified(resp, lastModified);
- doGet(req, resp);
- } else
- {
- resp.setStatus(304);
- }
- }
- } else
- if(method.equals("HEAD"))
- {
- long lastModified = getLastModified(req);
- maybeSetLastModified(resp, lastModified);
- doHead(req, resp);
- } else
- if(method.equals("POST"))
- doPost(req, resp);
- else
- if(method.equals("PUT"))
- doPut(req, resp);
- else
- if(method.equals("DELETE"))
- doDelete(req, resp);
- else
- if(method.equals("OPTIONS"))
- doOptions(req, resp);
- else
- if(method.equals("TRACE"))
- {
- doTrace(req, resp);
- } else
- {
- String errMsg = lStrings.getString("http.method_not_implemented");
- Object errArgs[] = new Object[1];
- errArgs[0] = method;
- errMsg = MessageFormat.format(errMsg, errArgs);
- resp.sendError(501, errMsg);
- }
- }
從上面可以看出 這里的service是用來轉(zhuǎn)向的,但是如果你在自己的servlet類中覆蓋了service方法,比如說你的service是這樣的:
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
res.getOutputStream().print(
"image is <img src='images/downcoin.gif'></img><br>");
}
那么這時service就不是用來轉(zhuǎn)向的,而是用來處理業(yè)務(wù)的,現(xiàn)在不論你的客戶端是用pos還是get來請求此servlet
都會執(zhí)行service方法也只能執(zhí)行servlet方法,不會去執(zhí)行doPost或是doGet方法。
比如說:你的客戶端代碼是:
- <%@page contentType="text/html; charset=utf-8"%>
- <html>
- <head><title>選擇</title></head>
- <body>
- 請選擇你喜歡的水果:<br>
- <form action = "Test" method = "post">
- <input type="checkbox" name="fruit" value ="apple" >蘋果<br>
- <input type="checkbox" name="fruit" value ="orange">桔子<br>
- <input type="checkbox" name="fruit" value ="mango">芒果<br>
- <input type="submit" value="提交">
- </form>
- </body>
- </html>
- 服務(wù)端servlet是:Test類
- import java.io.IOException;
- import javax.servlet.ServletException;
- import javax.servlet.ServletOutputStream;
- import javax.servlet.ServletRequest;
- import javax.servlet.ServletResponse;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- /**
- * 演示service方法
- */
- public class Test extends HttpServlet {
- public void service(ServletRequest req, ServletResponse res)
- throws ServletException, IOException {
- res.getOutputStream().print("This is the service");
- }
- protected void doGet(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {
- doPost(request,response);
- }
- protected void doPost(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {
- ServletOutputStream out=response.getOutputStream();
- String[] args=(String[])request.getParameterValues("fruit");
- for(int i=0;i<args.length;i++){
- out.print(args[i]+"<br>");
- }
- }
- }
那么當(dāng)你提交數(shù)據(jù)后的輸出結(jié)果是:this is the service