servlet中service() doGet() doPost() 方法


HttpServlet 里的三個方法:service(HttpServletRequest req, HttpServletResponse resp) ,doGet(HttpServletRequest req, HttpServletResponse resp), doPost(HttpServletRequest req, HttpServletResponse res)的區別和聯系:

    在servlet中默認情況下,無論你是get還是post 提交過來 都會經過service()方法來處理,然后轉向到doGet 
  1. 注意,sun只是定義了servlet接口,而實現servlet接口的就是類似於tomcat的服務器,所以我是在tomcat的安裝目錄下找到實現的類。
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是用來轉向的,但是如果你在自己的servlet類中覆蓋了service方法,比如說你的service是這樣的: 
  1. Java代碼 
  2.    1. public void service(ServletRequest req, ServletResponse res)    
  3. 2.                    throws ServletException, IOException {    
  4. 3.          res.getOutputStream().print(    
  5. 4.          "image is <img src='images/downcoin.gif'></img><br>");    
  6. 5.      }   
  7.  
  8.  
  9. Java代碼 
  10.    1. <%@page contentType="text/html; charset=utf-8"%>    
  11. 2. <html>    
  12. 3. <head><title>選擇</title></head>    
  13. 4. <body>    
  14. 5. 請選擇你喜歡的水果:<br>    
  15. 6. <form action = "Test" method = "post">    
  16. 7. <input type="checkbox" name="fruit" value ="apple" >蘋果<br>    
  17. 8. <input type="checkbox" name="fruit" value ="orange">桔子<br>    
  18. 9. <input type="checkbox" name="fruit" value ="mango">芒果<br>    
  19. 10. <input type="submit" value="提交">    
  20. 11. </form>    
  21. 12. </body>    
  22. 13. </html>    
  23. 14.    
  24. 15. 服務端servlet是:Test類    
  25. 16.    
  26. 17. import java.io.IOException;    
  27. 18.    
  28. 19. import javax.servlet.ServletException;    
  29. 20. import javax.servlet.ServletOutputStream;    
  30. 21. import javax.servlet.ServletRequest;    
  31. 22. import javax.servlet.ServletResponse;    
  32. 23. import javax.servlet.http.HttpServlet;    
  33. 24. import javax.servlet.http.HttpServletRequest;    
  34. 25. import javax.servlet.http.HttpServletResponse;    
  35. 26.    
  36. 27. /**
  37.   29. */   
  38. 30. public class Test extends HttpServlet {    
  39. 31.    
  40. 32. public void service(ServletRequest req, ServletResponse res)    
  41. 33.    throws ServletException, IOException {    
  42. 34.             res.getOutputStream().print("This is the service");    
  43. 35.    
  44. 36. }    
  45. 37.    
  46. 38. protected void doGet(HttpServletRequest request,    
  47. 39.     HttpServletResponse response) throws ServletException, IOException {    
  48. 40.    doPost(request,response);    
  49. 41.    
  50. 42. }    
  51. 43. protected void doPost(HttpServletRequest request,    
  52. 44.     HttpServletResponse response) throws ServletException, IOException {    
  53. 45.    ServletOutputStream out=response.getOutputStream();    
  54. 46.    String[] args=(String[])request.getParameterValues("fruit");    
  55. 47.   for(int i=0;i<args.length;i++){    
  56. 48.     out.print(args[i]+"<br>");    
  57. 49.    }    
  58. 50.       
  59. 51. }    
  60. 52. }   
  61. 所以,我們在寫servlet的時候,一般都是重寫doGet或doPost方法,不會管service方法。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM