Java Web笔记 – Servlet技术介绍 生命周期 核心API 类方法调用顺序
来源:互联网 发布:html5手机商城源码 编辑:程序博客网 时间:2024/06/12 01:44
Servlet是和平台无关的服务器端组件,它运行再Servlet容器中。Servlet容器负责Servlet和客户端通信和调用Servlet方式。Servlet和客户端通过“请求/响应”模式。
创建可嵌入现有HTML页面中的部分HTML代码片段
与其他服务器资源通信
javax.servlet.http包:定义了采用HTTP协议通信的HttpServlet类。
Servlet框架的核心是Javax.servlet.Servlet接口,所有的Servlet都必须实现这一个接口中定义的五个方法。
service方法:响应客户请求
destroy方法:Servlet对象退出生命周期时,释放占用的资源
默认情况下,当Web客户第一次请求访问某个Servlet时,才创建这个Servlet的实例。如果设置了<servlet>元素的<load-on-startup>子元素,Servlet容器在启动Web应用时,将按照指定的顺序创建并初始化这个Servlet:
<servlet> <servlet-name>LoginServlet</servlet-name> <servlet-class>com.itzhai.login.LoginServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet>
可以在这种servlet中编写全局的初始化信息。
Servlet被装载后,Servlet容器创建一个Servlet实例并调用Servlet的init()方法进行初始化。在Servlet整个生命周期中,init方法只被调用一次。
对于到达Servlet容器的客户请求,Servlet容器创建特定于这个请求的ServletRequest和ServletResponse对象,然后调用service方法进行处理。service方法从ServletRequest对象中获取客户请求信息,处理请求,并通过ServletResponse对象返回根客户端响应结果。
当Web应用被终止或者Servlet容器结束运行时,或Servlet容器重新装载Servlet的新实例时,Servlet容器会先调用Servlet的destroy方法释放Servlet所占用的资源。
ServletRequest接口中封装了客户端请求信息,客户端正在使用的协议,还提供了直接以二进制读取客户端数据流的ServletInputStream。
ServletRequest子类提供了更多的和特点协议相关的数据,如HttpServletRequest。
ServletRequest接口的主要方法:
getContentType
getInputStream
getParameter
getRemoteAddr
getRemoteHost
getRemotePort
ServletResponse接口为Servlet提供了返回相应结果的方法。允许Servlet设置返回数据的长度和MIME类型,并提供输出流ServletOutputStream。
ServletResponse子类提供了更多的和特点协议相关的数据,如HttpServletResponse。
ServletResponse接口的主要方法:
getWriter
getCharacterEncoding
getContentType
setContentType
public interface ServletDefines methods that all servlets must implement.
A servlet is a small Java program that runs within a Web server. Servlets receive and respond to requests from Web clients, usually across HTTP, the HyperText Transfer Protocol.
To implement this interface, you can write a generic servlet that extends javax.servlet.GenericServlet or an HTTP servlet that extends javax.servlet.http.HttpServlet.
This interface defines methods to initialize a servlet, to service requests, and to remove a servlet from the server. These are known as life-cycle methods and are called in the following sequence:
1. The servlet is constructed, then initialized with the init method.2. Any calls from clients to the service method are handled.3. The servlet is taken out of service, then destroyed with the destroy method, then garbage collected and finalized.
servicevoid service(ServletRequest req, ServletResponse res) throws ServletException, IOException
Called by the servlet container to allow the servlet to respond to a request.
This method is only called after the servlet's init() method has completed successfully.
public abstract class GenericServletextends Objectimplements Servlet, ServletConfig, Serializable
Defines a generic, protocol-independent servlet. To write an HTTP servlet for use on the Web, extend HttpServlet instead.
GenericServlet implements the Servlet and ServletConfig interfaces. GenericServlet may be directly extended by a servlet, although it's more common to extend a protocol-specific subclass such as HttpServlet.
public abstract class HttpServletextends GenericServletimplements Serializable
Provides an abstract class to be subclassed to create an HTTP servlet suitable for a Web site. A subclass of HttpServlet must override at least one method, usually one of these:
• doGet, if the servlet supports HTTP GET requests• doPost, for HTTP POST requests• doPut, for HTTP PUT requests• doDelete, for HTTP DELETE requests• init and destroy, to manage resources that are held for the life of the servlet• getServletInfo, which the servlet uses to provide information about itself
There's almost no reason to override the service method. service handles standard HTTP requests by dispatching them to the handler methods for each HTTP request type (the doXXX methods listed above).
Likewise, there's almost no reason to override the doOptions and doTrace methods.
首先调用Service方法,再根据请求信息,调用具体的getGet或doPost等方法。
由于Servlet是运行在多线程的服务器,所有需要特别处理并发共享的资源。
每个Servlet都需要实现Servlet接口。GenericServlet是一个通用的,不特定于协议的Servlet,其实现了Servlet接口,而HttpServlet继承与GenericServlet,实现了域HTTP协议相关的操作,所以我们定义的Servlet只需要直接继承HttpServlet即可。
- Java Web笔记 – Servlet技术介绍 生命周期 核心API 类方法调用顺序
- java web学习笔记(servlet生命周期)
- java web笔记之Servlet生命周期
- Java Web笔记:Servlet的生命周期
- 初识Servlet生命周期及核心API
- Java Servlet API类库中的基本方法介绍
- Java Servlet API类库中的基本方法介绍
- Java Servlet API类库中的基本方法介绍
- 30天学通JAVA WEB Servlet调用图及生命周期
- Java笔记--Servlet生命周期
- Java web学习笔记之servlet技术
- 《Servlet学习笔记》Servlet生命周期和调用
- 《Java Servlet编程》笔记(servlet生命周期)
- Java Web -- Servlet(4) 开发Servlet的三种方法、配置Servlet详解、Servlet的生命周期(1)
- Java Web -- Servlet(5) 开发Servlet的三种方法、配置Servlet详解、Servlet的生命周期(2)
- Java Servlet之Servlet生命周期方法
- Java Web笔记 – Servlet使用笔记
- Web---演示servlet技术(servlet生命周期),解决中文乱码问题
- git rebase master First, rewinding head to replay your work on top of it... error: The following unt
- 把十进制转成二进制的一个简单易行的方法(不需要用栈) .
- 50 Most Frequently Used UNIX / Linux Commands (With Examples)
- 《C语言深度剖析》笔记之C语言基础测试题
- Hibernate3.3.2注解使用流程
- Java Web笔记 – Servlet技术介绍 生命周期 核心API 类方法调用顺序
- word贴心自动智能功能
- ScrollView反弹效果实现[bug修正版]
- dll动态加载方式
- How to Use C's volatile Keyword C 语言volatile
- mysql-jdbc 处理全0格式的时间 (Timestamp)
- Jmockit示例三部曲之二让我验证下你是否走对了
- 马士兵Java自学之路
- 线程同步