Servelet

Servlet Lifecycle

The lifecycle of a servlet is controlled by the container in which the servlet has been deployed.

  1. If an instance of the servlet does not exist, the web container
    1. Loads the servlet class.
    2. Creates an instance of the servlet class.
    3. Initializes the servlet instance by calling the init method.
  2. Invokes the service method, passing request and response objects.

If it needs to remove the servlet, the container finalizes the servlet by calling the servlet’s
destroy method.

 

Handling Servlet Lifecycle Events

You can monitor and react to events in a servlet’s lifecycle by defining listener objects whose
methods get invoked when lifecycle events occur.

 

ObjectEventListener Interface and Event Class
Web contextInitialization and
destruction

javax.servlet.ServletContextListener,

javax.servlet.ServletContextEvent

 Attribute added,
removed, or replaced

javax.servlet.ServletContextAttributeListener,

javax.servlet.ServletContextAttributeEvent

SessionCreation,
invalidation,
activation,
passivation, and
timeout
javax.servlet.http.HttpSessionListener,
javax.servlet.http.HttpSessionActivationListener,
javax.servlet.http.HttpSessionEvent
 Attribute added,
removed, or replaced
javax.servlet.http.HttpSessionAttributeListener,
javax.servlet.http.HttpSessionBindingEvent
RequestA servlet request has
started being
processed by web
components
javax.servlet.ServletRequestListener,
javax.servlet.ServletRequestEvent
 Attribute added,
removed, or replaced

javax.servlet.ServletRequestAttributeListener,

javax.servlet.ServletRequestAttributeEvent

 

Sharing Information

Using Scope Objects

Web context, Session, Request, Page

Controlling Concurrent Access to Shared Resources

In a multithreaded server, shared resources can be accessed concurrently. In addition to scope
object attributes, shared resources include in-memory data, such as instance or class variables,
and external objects, such as files, database connections, and network connections.

 

Concurrent access can arise in several situations:

  • Multiple web components accessing objects stored in the web context.
  • Multiple web components accessing objects stored in a session.
  • Multiple threads within a web component accessing instance variables. To ensure that a servlet instance handles only one request at a time, a servlet can implement the SingleThreadModel interface. If aservlet implements this interface, no two threads will execute concurrently in the servlet’s service method. A web container can implement this guarantee by synchronizing access to a single instance of the servlet or by maintaining a pool of web component instances and
    dispatching each new request to a free instance. This interface does not prevent synchronization problems that result from web components’ accessing shared resources, such as static class variables or external objects.

 Service Method

public abstract class HttpServlet extends GenericServlet
    implements Serializable
{
    ...
    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);
        }
    }
    ...
}

SessionTracking

If your application uses session objects, you must ensure that session tracking is enabled by
having the application rewrite URLs whenever the client turns off cookies. You do this by
calling the response’s encodeURL(URL) method on all URLs returned by a servlet. This method
includes the session ID in the URL only if cookies are disabled; otherwise, the method returns
the URL unchanged.

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值