Java Servlet 是运行在 Web 服务器或应用服务器上的程序,它是作为来自 Web 浏览器或其他 HTTP 客户端的请求和 HTTP 服务器上的数据库或应用程序之间的中间层。
servlet是单例多线程的。也就是说,在堆内存中只有一份servlet对象。对于每一次http请求,servlet容器会自动调用servlet对象的service()方法去开辟线程,在这个线程中根据已有的url-class关系对这个request进行处理。
servlet需要跑在web服务器上。这里使用apache-tomcat-9.0.12。
servlet流程图:
一、Servlet接口类
1.Servlet接口类及其方法
public interface Servlet
Defines 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.
In addition to the life-cycle methods, this interface provides the getServletConfig method, which the servlet can use to get any startup information, and the getServletInfo method, which allows the servlet to return basic information about itself, such as author, version, and copyright.
| Method Summary | |
|---|---|
void |
destroy() Called by the servlet container to indicate to a servlet that the servlet is being taken out of service. |
ServletConfig |
getServletConfig()
Returns a ServletConfig object, which contains
initialization and startup parameters for this servlet. |
String |
getServletInfo()
Returns information about the servlet, such as author, version, and copyright. |
void |
init(ServletConfig config)
Called by the servlet container to indicate to a servlet that the servlet is being placed into service. |
void |
service(ServletRequest req,
ServletResponse res)
Called by the servlet container to allow the servlet to respond to a request. |
Servlet特性:
1.Servlet是单例多线程的,一个Servlet实例就是一次请求;
2.一个Servlet实例只会执行一次无参构造方法和init方法,并且是在第一次访问时执行;
3.一个Servlet实例只会执行一次destroy方法,并且在应用停止时销毁;
4.一个Servlet实例可以执行多个service,每当有用户请求发生,就会执行一次service方法;
5.Servlet是单例(单例模式)多线程的,所以为了线程安全,不为其设置成员变量,以避免线程间篡改成员变量;
6.默认情况下,Servlet在web容器启动时是不会被实例化的;
2.Servlet生命周期测试
package com.servlets; import java.io.IOException; import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; /** * servlet的生命周期 * @author apple * @see http://tool.oschina.net/apidocs/apidoc?api=javaEE5 * @see javax.servlet.Servlet * @see javax.servlet.ServletConfig */ public class ServletLifeCircleDemo implements Servlet { public ServletLifeCircleDemo() { System.out.println(" 1.执行构造方法......"); } @Override public void init(ServletConfig init) throws ServletException { System.out.println(" 2.初始化......"); } @Override public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { System.out.println(" 3.接受并处理请求......"); // 两个方法 ServletConfig servletConfig = this.getServletConfig(); System.out.println(servletConfig); // 读取配置信息 String servletInfo = this.getServletInfo(); System.out.println(servletInfo); } @Override public ServletConfig getServletConfig() { return null; } @Override public String getServletInfo() { return null; } @Override public void destroy() { System.out.println(" 销毁: destroy"); } }