Java Servlet(十一):一個servlet被10個瀏覽器客戶端訪問時會創建幾個servlet實例?


一般Servlet只初始化一次(只有一個實例)。對於更多的客戶端請求,Server創建新的請求和響應對象,仍然激活此Servlet的service()方法,將這兩個對象作為參數傳遞給該方法。如此重復以上的循環,但無需再調用init()方法。

原因:

出於性能的考慮:特別的對於門戶網站而言,每一個Servlet在每一秒內的並發訪問量都可以是成千上萬的。在一個面向模塊化開發的現在,常常一個點擊操作就被定義為一個Servlet的實現,而如果Servlet的每一次被訪問,都創建一個新的實例的話,服務器的可用資源消耗量將是一個相當重要的問題。
退一步,一般Servlet的訪問是很快的,每一個實例被快速的創建,又被快速的回收,GC的回收速度也跟不上,頻繁的內存操作也將可能帶來次生的問題。

所以,Servlet的“單一實例化”是一個很重要的策略。

此時為了更好理解servlet,這里附上servlet、httpservlet代碼:

Servlet源代碼:

package javax.servlet;  
import java.io.IOException;  
// Referenced classes of package javax.servlet:  
// ServletException, ServletConfig, ServletRequest, ServletResponse  
public interface Servlet  
{  
    public abstract void init(ServletConfig servletconfig)  
        throws ServletException;  
    public abstract ServletConfig getServletConfig();  
    public abstract void service(ServletRequest servletrequest, ServletResponse servletresponse)  
        throws ServletException, IOException;  
    public abstract String getServletInfo();  
    public abstract void destroy();  
}  

HttpServlet源代碼:

import java.io.IOException;  
import java.io.Serializable;  
import java.lang.reflect.Method;  
import java.text.MessageFormat;  
import java.util.Enumeration;  
import java.util.ResourceBundle;  
import javax.servlet.*;  
// Referenced classes of package javax.servlet.http:  
//            NoBodyResponse, HttpServletRequest, HttpServletResponse  
public abstract class HttpServlet extends GenericServlet  
    implements Serializable  
{  
    public HttpServlet()  
    {  
    }  
    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(405, msg);  
        else  
            resp.sendError(400, msg);  
    }  
    protected long getLastModified(HttpServletRequest req)  
    {  
        return -1L;  
    }  
    protected void doHead(HttpServletRequest req, HttpServletResponse resp)  
        throws ServletException, IOException  
    {  
        NoBodyResponse response = new NoBodyResponse(resp);  
        doGet(req, response);  
        response.setContentLength();  
    }  
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)  
        throws ServletException, IOException  
    {  
        String protocol = req.getProtocol();  
        String msg = lStrings.getString("http.method_post_not_supported");  
        if(protocol.endsWith("1.1"))  
            resp.sendError(405, msg);  
        else  
            resp.sendError(400, msg);  
    }  
    protected void doPut(HttpServletRequest req, HttpServletResponse resp)  
        throws ServletException, IOException  
    {  
        String protocol = req.getProtocol();  
        String msg = lStrings.getString("http.method_put_not_supported");  
        if(protocol.endsWith("1.1"))  
            resp.sendError(405, msg);  
        else  
            resp.sendError(400, msg);  
    }  
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp)  
        throws ServletException, IOException  
    {  
        String protocol = req.getProtocol();  
        String msg = lStrings.getString("http.method_delete_not_supported");  
        if(protocol.endsWith("1.1"))  
            resp.sendError(405, msg);  
        else  
            resp.sendError(400, msg);  
    }  
    private Method[] getAllDeclaredMethods(Class c)  
    {  
        if(c.equals(javax/servlet/http/HttpServlet))  
            return null;  
        Method parentMethods[] = getAllDeclaredMethods(c.getSuperclass());  
        Method thisMethods[] = c.getDeclaredMethods();  
        if(parentMethods != null && parentMethods.length > 0)  
        {  
            Method allMethods[] = new Method[parentMethods.length + thisMethods.length];  
            System.arraycopy(parentMethods, 0, allMethods, 0, parentMethods.length);  
            System.arraycopy(thisMethods, 0, allMethods, parentMethods.length, thisMethods.length);  
            thisMethods = allMethods;  
        }  
        return thisMethods;  
    }  
    protected void doOptions(HttpServletRequest req, HttpServletResponse resp)  
        throws ServletException, IOException  
    {  
        Method methods[] = getAllDeclaredMethods(getClass());  
        boolean ALLOW_GET = false;  
        boolean ALLOW_HEAD = false;  
        boolean ALLOW_POST = false;  
        boolean ALLOW_PUT = false;  
        boolean ALLOW_DELETE = false;  
        boolean ALLOW_TRACE = true;  
        boolean ALLOW_OPTIONS = true;  
        for(int i = 0; i < methods.length; i++)  
        {  
            Method m = methods[i];  
            if(m.getName().equals("doGet"))  
            {  
                ALLOW_GET = true;  
                ALLOW_HEAD = true;  
            }  
            if(m.getName().equals("doPost"))  
                ALLOW_POST = true;  
            if(m.getName().equals("doPut"))  
                ALLOW_PUT = true;  
            if(m.getName().equals("doDelete"))  
                ALLOW_DELETE = true;  
        }  
        String allow = null;  
        if(ALLOW_GET && allow == null)  
            allow = "GET";  
        if(ALLOW_HEAD)  
            if(allow == null)  
                allow = "HEAD";  
            else  
                allow = (new StringBuilder()).append(allow).append(", HEAD").toString();  
        if(ALLOW_POST)  
            if(allow == null)  
                allow = "POST";  
            else  
                allow = (new StringBuilder()).append(allow).append(", POST").toString();  
        if(ALLOW_PUT)  
            if(allow == null)  
                allow = "PUT";  
            else  
                allow = (new StringBuilder()).append(allow).append(", PUT").toString();  
        if(ALLOW_DELETE)  
            if(allow == null)  
                allow = "DELETE";  
            else  
                allow = (new StringBuilder()).append(allow).append(", DELETE").toString();  
        if(ALLOW_TRACE)  
            if(allow == null)  
                allow = "TRACE";  
            else  
                allow = (new StringBuilder()).append(allow).append(", TRACE").toString();  
        if(ALLOW_OPTIONS)  
            if(allow == null)  
                allow = "OPTIONS";  
            else  
                allow = (new StringBuilder()).append(allow).append(", OPTIONS").toString();  
        resp.setHeader("Allow", allow);  
    }  
    protected void doTrace(HttpServletRequest req, HttpServletResponse resp)  
        throws ServletException, IOException  
    {  
        String CRLF = "\r\n";  
        String responseString = (new StringBuilder()).append("TRACE ").append(req.getRequestURI()).append(" ").append(req.getProtocol()).toString();  
        for(Enumeration reqHeaderEnum = req.getHeaderNames(); reqHeaderEnum.hasMoreElements();)  
        {  
            String headerName = (String)reqHeaderEnum.nextElement();  
            responseString = (new StringBuilder()).append(responseString).append(CRLF).append(headerName).append(": ").append(req.getHeader(headerName)).toString();  
        }  
        responseString = (new StringBuilder()).append(responseString).append(CRLF).toString();  
        int responseLength = responseString.length();  
        resp.setContentType("message/http");  
        resp.setContentLength(responseLength);  
        ServletOutputStream out = resp.getOutputStream();  
        out.print(responseString);  
        out.close();  
    }  
    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);  
        }  
    }  
    private void maybeSetLastModified(HttpServletResponse resp, long lastModified)  
    {  
        if(resp.containsHeader("Last-Modified"))  
            return;  
        if(lastModified >= 0L)  
            resp.setDateHeader("Last-Modified", lastModified);  
    }  
    public void service(ServletRequest req, ServletResponse res)  
        throws ServletException, IOException  
    {  
        HttpServletRequest request;  
        HttpServletResponse response;  
        try  
        {  
            request = (HttpServletRequest)req;  
            response = (HttpServletResponse)res;  
        }  
        catch(ClassCastException e)  
        {  
            throw new ServletException("non-HTTP request or response");  
        }  
        service(request, response);  
    }  
    private static final String METHOD_DELETE = "DELETE";  
    private static final String METHOD_HEAD = "HEAD";  
    private static final String METHOD_GET = "GET";  
    private static final String METHOD_OPTIONS = "OPTIONS";  
    private static final String METHOD_POST = "POST";  
    private static final String METHOD_PUT = "PUT";  
    private static final String METHOD_TRACE = "TRACE";  
    private static final String HEADER_IFMODSINCE = "If-Modified-Since";  
    private static final String HEADER_LASTMOD = "Last-Modified";  
    private static final String LSTRING_FILE = "javax.servlet.http.LocalStrings";  
    private static ResourceBundle lStrings = ResourceBundle.getBundle("javax.servlet.http.LocalStrings");  
}  

參考:《.init()方法成功完成后,Servlet可以接受請求.默認有多少個Servlet實例被創建》、《Java Servlet(二):servlet配置及生命周期相關(jdk7+tomcat7+eclipse)


免責聲明!

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



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