🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## Web专题一:Servlet [TOC] > Servlet是用Java编写的服务器端程序,运行于支持Java的Web应用服务器中,可以响应HTTP请求 ### 1.1. Servlet生命周期 ![](https://img.kancloud.cn/d2/14/d21495d7d990b611d3392165760834df_423x672.png) #### 1.1.1. init()方法 ~~~ void init() throws ServletException ~~~ 在Servlet第一次被创建时才会被调用,且只调用一次 #### 1.1.2. service()方法 ~~~ void service(ServletRequest req, ServletResponse res) throws ServletException, IOException ~~~ Servlet容器在这里处理请求,根据请求的方法调用相应的doGet等方法,且每个请求都会产生一个新的线程 #### 1.1.3. doGet()方法 ~~~ void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException ~~~ 处理请求方法为GET的请求,更多request方法如下: ~~~ private static final String METHOD_DELETE = "DELETE"; private static final String METHOD_HEAD = "HEAD"; private static final String METHOD_GET = "GET"; private static final String METHOD_OPTIONS = "OPTIONS"; private static final String METHOD_POST = "POST"; private static final String METHOD_PUT = "PUT"; private static final String METHOD_TRACE = "TRACE"; ~~~ #### 1.1.4. doDestory()方法 销毁Servlet,只被调用一次 ### 1.2. Servlet完整示例 #### 1.2.1. 创建Servlet类 定义`Servlet`方法有3种: - 实现`Servlet`接口 - 继承`GenericServlet`类 - 继承`HttpServlet`类 ~~~ package org.ibu; import javax.servlet.http.*; import javax.servlet.*; import java.io.IOException; import java.util.logging.Logger; public class SearchServlet extends HttpServlet { final Logger logger = Logger.getLogger(SearchServlet.class.getName()); @Override public void init() throws ServletException { logger.info("Servlet::init()"); } @Override public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { logger.info("Servlet::service()"); super.service(req, res); } @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { logger.info("Servlet::doGet()"); } @Override public void destroy() { logger.info("Servlet::destroy()"); } } ~~~ #### 1.2.2. 配置Servlet 在`/WEB-INF/web.xml`添加Servlet配置,将URL映射到Servlet类,将`/SearchServlet`和`/search`映射到SearchServlet类上 ~~~ <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <servlet> <servlet-name>SearchServlet</servlet-name> <servlet-class>org.ibu.SearchServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>SearchServlet</servlet-name> <url-pattern>/SearchServlet</url-pattern> <url-pattern>/search</url-pattern> </servlet-mapping> </web-app> ~~~ ### 1.3. Servlet完整示例(注解方式) #### 1.3.1. 创建Servlet类 ~~~ package org.ibu; import javax.servlet.annotation.WebServlet; import javax.servlet.http.*; import javax.servlet.*; import java.io.IOException; import java.util.logging.Logger; @WebServlet(name = "SearchServlet", urlPatterns = {"/SearchServlet", "/search"}) public class SearchServlet extends HttpServlet { final Logger logger = Logger.getLogger(SearchServlet.class.getName()); @Override public void init() throws ServletException { logger.info("Servlet::init()"); } @Override public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { logger.info("Servlet::service()"); super.service(req, res); } @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { logger.info("Servlet::doGet()"); } @Override public void destroy() { logger.info("Servlet::destroy()"); } } ~~~ #### 1.3.2. 配置Servlet `/WEB-INF/web.xml`无需配置Servlet ~~~ <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> </web-app> ~~~ #### 1.3.3. 生命周期示例 1. startup启动web服务器 2. 浏览器访问[http://localhost:8080/SearchServlet](http://localhost:8080/SearchServlet) 3. 浏览器访问[http://localhost:8080/search](http://localhost:8080/search) 4. shutdown停止Web服务器 日志信息输出如下: ~~~ 19-Apr-2020 11:19:49.521 信息 [http-nio-8080-exec-3] org.ibu.SearchServlet.init Servlet::init() 19-Apr-2020 11:19:49.522 信息 [http-nio-8080-exec-3] org.ibu.SearchServlet.service Servlet::service() 19-Apr-2020 11:19:49.523 信息 [http-nio-8080-exec-3] org.ibu.SearchServlet.doGet Servlet::doGet() 19-Apr-2020 11:20:14.163 信息 [http-nio-8080-exec-8] org.ibu.SearchServlet.service Servlet::service() 19-Apr-2020 11:20:14.163 信息 [http-nio-8080-exec-8] org.ibu.SearchServlet.doGet Servlet::doGet() ... 19-Apr-2020 11:20:20.506 信息 [localhost-startStop-2] org.ibu.SearchServlet.destroy Servlet::destroy() ~~~