- 浏览: 34710 次
- 性别:
- 来自: 上海
文章分类
最新评论
Servlet Lifecycle
The lifecycle of a servlet is controlled by the container in which the servlet has been deployed.
- If an instance of the servlet does not exist, the web container
- Loads the servlet class.
- Creates an instance of the servlet class.
- Initializes the servlet instance by calling the init method.
- 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.
发表评论
-
Spring 3
2010-10-14 18:17 757Spring内置了2个基础DI容器: BeanFactory、 ... -
EJB
2010-10-13 15:20 561To improve performance, you mig ... -
Web设计相关
2010-10-13 12:51 606所有架构级的设计决策应该根据被设计系统的功能、行为和社会学等方 ... -
web services
2010-10-12 16:20 725Java API for XML Web Services ( ... -
Java Extension Mechanism
2010-07-02 12:09 1464Java 1.2引入了扩展机制(the extension m ... -
Java系统的堆栈跟踪
2010-06-19 01:53 1128如何生成堆栈的Dump? 有下面3种方法,可以得到Java系 ... -
Axis2 notes
2008-07-17 06:48 9061. set env var: JAVA_HOME, AXIS ...
相关推荐
【标题】"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实现的图书管理系统基于...
根据给定的信息,我们可以深入探讨有关Servelet的基本概念、配置及其实现方法。Servelet是一种用Java编写的服务器端应用程序,主要用于处理客户端请求并生成动态Web页面或内容。以下是基于提供的标题、描述、标签和...
Java Servlet API是Java Web开发中的核心组件之一,它为创建动态网页内容提供了强大的支持。本教程将引导初学者深入理解Servlet的基本概念、工作原理以及如何使用Servlet API进行开发。 Servlet是一种基于Java的...
在传统的网页交互中,用户提交表单时,整个页面需要重新加载以展示服务器返回的信息,这不仅耗时,而且打断了用户的浏览流程。而Ajax(Asynchronous JavaScript and XML)技术的出现,改变了这一状况,它允许网页在...
基于jsp+servelet+javaweb的网上书店,java项目。 eclipse和idea都能打开运行。 推荐环境配置:eclipse/idea jdk1.8 maven mysql 前端技术:jsp,Ajax,Json 后端技术:servlet,javaweb 本系统共分为两个角色:管理...
基于Maven+Servelet+JSP+Bootstrap期末大作业JavaWeb学生管理系统源码+文档+高分项目+全部资料.zip 【备注】 1、该项目是个人高分项目源码,已获导师指导认可通过,答辩评审分达到95分 2、该资源内项目代码都经过...
servelet war package receive https ,print remote ip ,print remote port ,print local ip and port
总的来说,"servelet 和mysql的jar包.zip"提供了一套基础的Java Web开发环境,能够帮助开发者快速搭建与MySQL数据库交互的Servlet应用。为了使用这些资源,开发者需要了解Servlet编程和JDBC的基本概念,同时确保配置...
servelet API 文档,这个文档是我自己精心制作的chm格式的文件,希望能给大家带来帮助
【宾馆客房管理系统jsp+javabean+servelet】是一个基于Web的软件开发项目,主要针对宾馆行业的客房管理需求。这个系统采用经典的Java Web技术栈,包括JSP(JavaServer Pages)、JavaBean和Servlet,旨在提供高效、...
以上就是"Jsp+Dao+Jdbc+Servelet 用户完整登录"的知识点详解。这样的架构确保了前端与后端的分离,提高了代码的可维护性和可扩展性。同时,通过DAO和JDBC,我们可以灵活地切换不同的数据库,以适应不同的项目需求。
标题中的“SWFUpload+COS 上传组件+servelet 实现大附件上传”是指使用SWFUpload这款JavaScript库,结合腾讯云对象存储服务(COS)以及Servlet技术,来实现大文件的分片上传功能。这是一个常见的Web应用上传方案,...
Java Servlet API是Java Web开发中的核心组件,它提供了一组接口和类,使得开发者能够创建服务器端的应用程序,特别是处理HTTP请求和响应。... 1. **Servlet接口** Servlet接口是所有Servlet的基础,定义了Servlet的...
【标题】:“jsp+javabean+servelet做的一个很好的博客系统” 【描述】:这个项目采用经典的Java Web技术栈——jsp、javabean和servlet来构建了一个功能完善的博客平台,非常适合用作课程设计或者初学者的学习实例...
[swfupload]+[COS 上传组件]+[servelet] 实现大附件上传
Jsp&Servelet 学习笔记(4)_JSP教程_www_knowsky_com.htm
Servelet API
【标题】"tp5_servelet" 指的是基于ThinkPHP5框架的Servlet应用,这是一个在Java平台上运行的Web应用程序。在Java世界里,Servlet是用于处理HTTP请求的核心组件,它扩展了Web服务器的功能,使得Java开发者可以构建...
servelet开发基础的课件,详细介绍了servelet的开发需要的基本知识