## 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()
~~~
- JavaCook
- Java专题零:类的继承
- Java专题一:数据类型
- Java专题二:相等与比较
- Java专题三:集合
- Java专题四:异常
- Java专题五:遍历与迭代
- Java专题六:运算符
- Java专题七:正则表达式
- Java专题八:泛型
- Java专题九:反射
- Java专题九(1):反射
- Java专题九(2):动态代理
- Java专题十:日期与时间
- Java专题十一:IO与NIO
- Java专题十一(1):IO
- Java专题十一(2):NIO
- Java专题十二:网络
- Java专题十三:并发编程
- Java专题十三(1):线程与线程池
- Java专题十三(2):线程安全与同步
- Java专题十三(3):内存模型、volatile、ThreadLocal
- Java专题十四:JDBC
- Java专题十五:日志
- Java专题十六:定时任务
- Java专题十七:JavaMail
- Java专题十八:注解
- Java专题十九:浅拷贝与深拷贝
- Java专题二十:设计模式
- Java专题二十一:序列化与反序列化
- 附加专题一:MySQL
- MySQL专题零:简介
- MySQL专题一:安装与连接
- MySQL专题二:DDL与DML语法
- MySQL专题三:工作原理
- MySQL专题四:InnoDB存储引擎
- MySQL专题五:sql优化
- MySQL专题六:数据类型
- 附加专题二:Mybatis
- Mybatis专题零:简介
- Mybatis专题一:配置文件
- Mybatis专题二:映射文件
- Mybatis专题三:动态SQL
- Mybatis专题四:源码解析
- 附加专题三:Web编程
- Web专题零:HTTP协议
- Web专题一:Servlet
- Web专题二:Cookie与Session
- 附加专题四:Redis
- Redis专题一:数据类型
- Redis专题二:事务
- Redis专题三:key的过期
- Redis专题四:消息队列
- Redis专题五:持久化