关闭

servlet中service、doGet、doPost等方法关系

222人阅读 评论(0) 收藏 举报
分类:
<p>在servlet中默认情况下,无论你是get还是post 提交过来都会经过service()方法来处理,然后转向到doGet</p>
<p>或是doPost方法,可以看HttpServlet 类的service方法:</p>
<div class="dp-highlighter">
<div class="bar">
<div class="tools">Java代码</div>
</div>
<div class="save_code tracking-ad" data-mod="popu_249" style="display: none;"><a href="javascript:;" target="_blank"><img src="http://static.blog.csdn.net/images/save_snippets_01.png"></a></div></div>
<div class="dp-highlighter bg_java"><div class="bar"><div class="tools"><b>[java]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 592px; top: 593px; width: 29px; height: 14px; z-index: 99;"><embed id="ZeroClipboardMovie_1" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="29" height="14" name="ZeroClipboardMovie_1" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=1&width=29&height=14" wmode="transparent"></div></span><span data-mod="popu_169"> <a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;">?</a><span class="tracking-ad" data-mod="popu_167"><a href="https://code.csdn.net/snippets/167649" target="_blank" title="在CODE上查看代码片" style="text-indent:0;"><img src="https://code.csdn.net/assets/CODE_ico.png" width="12" height="12" alt="在CODE上查看代码片" style="position:relative;top:1px;left:2px;"></a></span><span class="tracking-ad" data-mod="popu_170"><a href="https://code.csdn.net/snippets/167649/fork" target="_blank" title="派生到我的代码片" style="text-indent:0;"><img src="https://code.csdn.net/assets/ico_fork.svg" width="12" height="12" alt="派生到我的代码片" style="position:relative;top:2px;left:2px;"></a></span></div></div><ol start="1" class="dp-j"><li class="alt"><span><span class="keyword">protected</span><span> </span><span class="keyword">void</span><span> service(HttpServletRequest req, HttpServletResponse resp)  </span></span></li><li class=""><span>       <span class="keyword">throws</span><span> ServletException, IOException  </span></span></li><li class="alt"><span>   {  </span></li><li class=""><span>       String method = req.getMethod();  </span></li><li class="alt"><span>       <span class="keyword">if</span><span>(method.equals(</span><span class="string">"GET"</span><span>))  </span></span></li><li class=""><span>       {  </span></li><li class="alt"><span>           <span class="keyword">long</span><span> lastModified = getLastModified(req);  </span></span></li><li class=""><span>           <span class="keyword">if</span><span>(lastModified == -1L)  </span></span></li><li class="alt"><span>           {  </span></li><li class=""><span>               doGet(req, resp);  </span></li><li class="alt"><span>           } <span class="keyword">else</span><span>  </span></span></li><li class=""><span>           {  </span></li><li class="alt"><span>               <span class="keyword">long</span><span> ifModifiedSince = req.getDateHeader(</span><span class="string">"If-Modified-Since"</span><span>);  </span></span></li><li class=""><span>               <span class="keyword">if</span><span>(ifModifiedSince < (lastModified / 1000L) * 1000L)  </span></span></li><li class="alt"><span>               {  </span></li><li class=""><span>                   maybeSetLastModified(resp, lastModified);  </span></li><li class="alt"><span>                   doGet(req, resp);  </span></li><li class=""><span>               } <span class="keyword">else</span><span>  </span></span></li><li class="alt"><span>               {  </span></li><li class=""><span>                   resp.setStatus(<span class="number">304</span><span>);  </span></span></li><li class="alt"><span>               }  </span></li><li class=""><span>           }  </span></li><li class="alt"><span>       } <span class="keyword">else</span><span>  </span></span></li><li class=""><span>       <span class="keyword">if</span><span>(method.equals(</span><span class="string">"HEAD"</span><span>))  </span></span></li><li class="alt"><span>       {  </span></li><li class=""><span>           <span class="keyword">long</span><span> lastModified = getLastModified(req);  </span></span></li><li class="alt"><span>           maybeSetLastModified(resp, lastModified);  </span></li><li class=""><span>           doHead(req, resp);  </span></li><li class="alt"><span>       } <span class="keyword">else</span><span>  </span></span></li><li class=""><span>       <span class="keyword">if</span><span>(method.equals(</span><span class="string">"POST"</span><span>))  </span></span></li><li class="alt"><span>           doPost(req, resp);  </span></li><li class=""><span>       <span class="keyword">else</span><span>  </span></span></li><li class="alt"><span>       <span class="keyword">if</span><span>(method.equals(</span><span class="string">"PUT"</span><span>))  </span></span></li><li class=""><span>           doPut(req, resp);  </span></li><li class="alt"><span>       <span class="keyword">else</span><span>  </span></span></li><li class=""><span>       <span class="keyword">if</span><span>(method.equals(</span><span class="string">"DELETE"</span><span>))  </span></span></li><li class="alt"><span>           doDelete(req, resp);  </span></li><li class=""><span>       <span class="keyword">else</span><span>  </span></span></li><li class="alt"><span>       <span class="keyword">if</span><span>(method.equals(</span><span class="string">"OPTIONS"</span><span>))  </span></span></li><li class=""><span>           doOptions(req, resp);  </span></li><li class="alt"><span>       <span class="keyword">else</span><span>  </span></span></li><li class=""><span>       <span class="keyword">if</span><span>(method.equals(</span><span class="string">"TRACE"</span><span>))  </span></span></li><li class="alt"><span>       {  </span></li><li class=""><span>           doTrace(req, resp);  </span></li><li class="alt"><span>       } <span class="keyword">else</span><span>  </span></span></li><li class=""><span>       {  </span></li><li class="alt"><span>           String errMsg = lStrings.getString(<span class="string">"http.method_not_implemented"</span><span>);  </span></span></li><li class=""><span>           Object errArgs[] = <span class="keyword">new</span><span> Object[</span><span class="number">1</span><span>];  </span></span></li><li class="alt"><span>           errArgs[<span class="number">0</span><span>] = method;  </span></span></li><li class=""><span>           errMsg = MessageFormat.format(errMsg, errArgs);  </span></li><li class="alt"><span>           resp.sendError(<span class="number">501</span><span>, errMsg);  </span></span></li><li class=""><span>       }  </span></li><li class="alt"><span>   }  </span></li></ol><div class="save_code tracking-ad" data-mod="popu_249" style="display: none;"><a href="javascript:;" target="_blank"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div><pre code_snippet_id="167649" snippet_file_name="blog_20140123_1_9003153" style="display: none;" class="java" title="servlet中service doGet doPost 的关系" pre_index="0" source_url="http://jzinfo.iteye.com/blog/457574" codeable_type="Blog" codeable_id="457574" name="code"> 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);
        }
    }
</pre>
<p> </p>
<p> 从上面可以看出 这里的service是用来转向的,但是如果你在自己的servlet类中覆盖了service方法,比如说你的service是这样的:</p>
<p> </p>
<div class="dp-highlighter">
<div class="bar">
<div class="tools">Java代码 <div class="dp-highlighter bg_java"><div class="bar"><div class="tools"><b>[java]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 650px; top: 1781px; width: 29px; height: 14px; z-index: 99;"><embed id="ZeroClipboardMovie_2" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="29" height="14" name="ZeroClipboardMovie_2" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=2&width=29&height=14" wmode="transparent"></div></span><span data-mod="popu_169"> <a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;">?</a><span class="tracking-ad" data-mod="popu_167"><a href="https://code.csdn.net/snippets/167649" target="_blank" title="在CODE上查看代码片" style="text-indent:0;"><img src="https://code.csdn.net/assets/CODE_ico.png" width="12" height="12" alt="在CODE上查看代码片" style="position:relative;top:1px;left:2px;"></a></span><span class="tracking-ad" data-mod="popu_170"><a href="https://code.csdn.net/snippets/167649/fork" target="_blank" title="派生到我的代码片" style="text-indent:0;"><img src="https://code.csdn.net/assets/ico_fork.svg" width="12" height="12" alt="派生到我的代码片" style="position:relative;top:2px;left:2px;"></a></span></div></div><ol start="1" class="dp-j"><li class="alt"><span><span class="keyword">public</span><span> </span><span class="keyword">void</span><span> service(ServletRequest req, ServletResponse res)  </span></span></li><li class=""><span>                   <span class="keyword">throws</span><span> ServletException, IOException {  </span></span></li><li class="alt"><span>        res.getOutputStream().print(  </span></li><li class=""><span>         <span class="string">"image is <img src='images/downcoin.gif'></img><br>"</span><span>);  </span></span></li><li class="alt"><span>    }  </span></li></ol><div class="save_code tracking-ad" data-mod="popu_249" style="display: none;"><a href="javascript:;" target="_blank"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div><pre code_snippet_id="167649" snippet_file_name="blog_20140123_2_1543646" style="display: none;" class="java" title="servlet中service doGet doPost 的关系" pre_index="1" source_url="http://jzinfo.iteye.com/blog/457574" codeable_type="Blog" codeable_id="457574" name="code">public void service(ServletRequest req, ServletResponse res)
                   throws ServletException, IOException {
        res.getOutputStream().print(
         "image is <img src='images/downcoin.gif'></img><br>");
    }</pre></div>
</div>
<div class="save_code tracking-ad" data-mod="popu_249" style="display: none;"><a href="javascript:;" target="_blank"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div>
<p> </p>
<p><span style="color:#000000">那么这时service就不是用来转向的,而是用来处理业务的,现在不论你的客户端是用pos还是get来请求此servlet</span></p>
<p>都会执行service方法也只能执行servlet方法,不会去执行doPost或是doGet方法。</p>
<p> </p>


   

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:41869次
    • 积分:1419
    • 等级:
    • 排名:千里之外
    • 原创:99篇
    • 转载:28篇
    • 译文:1篇
    • 评论:4条
    文章分类
    最新评论