`
hillmover
  • 浏览: 34454 次
  • 性别: Icon_minigender_2
  • 来自: 上海
社区版块
存档分类
最新评论

Servelet

    博客分类:
  • Java
阅读更多

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.

 

Object Event Listener Interface and Event Class
Web context Initialization and
destruction

javax.servlet.ServletContextListener,

javax.servlet.ServletContextEvent

  Attribute added,
removed, or replaced

javax.servlet.ServletContextAttributeListener,

javax.servlet.ServletContextAttributeEvent

Session Creation,
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
Request A 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.

 

 

分享到:
评论

相关推荐

    jsp+servelet+javabean

    【标题】"jsp+servelet+javabean"是一个经典的Web开发技术组合,它们在Java Web应用程序中扮演着核心角色。JSP(JavaServer Pages)是用于创建动态网页的服务器端技术,Servlet是Java编写的服务器端程序,而JavaBean...

    基于jdbc+jsp+servelet实现的图书管理系统

    基于jdbc+jsp+servelet实现的图书管理系统基于jdbc+jsp+servelet实现的图书管理系统基于jdbc+jsp+servelet实现的图书管理系统基于jdbc+jsp+servelet实现的图书管理系统基于jdbc+jsp+servelet实现的图书管理系统基于...

    servelet

    根据给定的信息,我们可以深入探讨有关Servelet的基本概念、配置及其实现方法。Servelet是一种用Java编写的服务器端应用程序,主要用于处理客户端请求并生成动态Web页面或内容。以下是基于提供的标题、描述、标签和...

    Java Servelet API入门基础教程

    Java Servlet API是Java Web开发中的核心组件之一,它为创建动态网页内容提供了强大的支持。本教程将引导初学者深入理解Servlet的基本概念、工作原理以及如何使用Servlet API进行开发。 Servlet是一种基于Java的...

    ajax+servelet无刷新显示用户名

    在传统的网页交互中,用户提交表单时,整个页面需要重新加载以展示服务器返回的信息,这不仅耗时,而且打断了用户的浏览流程。而Ajax(Asynchronous JavaScript and XML)技术的出现,改变了这一状况,它允许网页在...

    基于jsp+servelet+javaweb的网上书店,java项目 eclipse和idea都能打开运行 优质精品项目

    基于jsp+servelet+javaweb的网上书店,java项目。 eclipse和idea都能打开运行。 推荐环境配置:eclipse/idea jdk1.8 maven mysql 前端技术:jsp,Ajax,Json 后端技术:servlet,javaweb 本系统共分为两个角色:管理...

    https servelet war package

    servelet war package receive https ,print remote ip ,print remote port ,print local ip and port

    servelet 和mysql的jar包.zip

    总的来说,"servelet 和mysql的jar包.zip"提供了一套基础的Java Web开发环境,能够帮助开发者快速搭建与MySQL数据库交互的Servlet应用。为了使用这些资源,开发者需要了解Servlet编程和JDBC的基本概念,同时确保配置...

    servelet的API文档

    servelet API 文档,这个文档是我自己精心制作的chm格式的文件,希望能给大家带来帮助

    宾馆客房管理系统jsp+javabean+servelet

    【宾馆客房管理系统jsp+javabean+servelet】是一个基于Web的软件开发项目,主要针对宾馆行业的客房管理需求。这个系统采用经典的Java Web技术栈,包括JSP(JavaServer Pages)、JavaBean和Servlet,旨在提供高效、...

    Jsp+Dao+Jdbc+Servelet 用户完整登录

    以上就是"Jsp+Dao+Jdbc+Servelet 用户完整登录"的知识点详解。这样的架构确保了前端与后端的分离,提高了代码的可维护性和可扩展性。同时,通过DAO和JDBC,我们可以灵活地切换不同的数据库,以适应不同的项目需求。

    SWFUpload+COS 上传组件+servelet 实现大附件上传

    标题中的“SWFUpload+COS 上传组件+servelet 实现大附件上传”是指使用SWFUpload这款JavaScript库,结合腾讯云对象存储服务(COS)以及Servlet技术,来实现大文件的分片上传功能。这是一个常见的Web应用上传方案,...

    JAVA servelet API

    Java Servlet API是Java Web开发中的核心组件,它提供了一组接口和类,使得开发者能够创建服务器端的应用程序,特别是处理HTTP请求和响应。... 1. **Servlet接口** Servlet接口是所有Servlet的基础,定义了Servlet的...

    jsp+javabean+servelet做的一个很好的博客系统

    【标题】:“jsp+javabean+servelet做的一个很好的博客系统” 【描述】:这个项目采用经典的Java Web技术栈——jsp、javabean和servlet来构建了一个功能完善的博客平台,非常适合用作课程设计或者初学者的学习实例...

    swfupload+COS 上传组件+servelet实现大附件上传

    [swfupload]+[COS 上传组件]+[servelet] 实现大附件上传

    Jsp&Servelet 学习笔记(4)_JSP教程_www_knowsky_com.htm

    Jsp&Servelet 学习笔记(4)_JSP教程_www_knowsky_com.htm

    Servelet API

    Servelet API

    tp5_servelet

    【标题】"tp5_servelet" 指的是基于ThinkPHP5框架的Servlet应用,这是一个在Java平台上运行的Web应用程序。在Java世界里,Servlet是用于处理HTTP请求的核心组件,它扩展了Web服务器的功能,使得Java开发者可以构建...

    servelet开发

    servelet开发基础的课件,详细介绍了servelet的开发需要的基本知识

    java servelet 演示例子

    Java Servlet 是一种用于扩展Web服务器功能的技术,主要应用于构建动态Web应用程序。...在本"Java Servlet 演示例子"中,我们将深入探讨Servlet的基础概念、工作原理以及如何通过实例进行实践。 ...Servlet生命周期包括...

Global site tag (gtag.js) - Google Analytics