Servle技术
什么是Servlet
- Servlet是javaee规范之一。规范就是接口
- Servlet就是javaweb三大组件之一。三大组件分别是:servlet程序,filter过滤器,listener监听器。
- Servlet是运行在服务器上的一个java小程序,他可以接受客户端发送过来的请求,并相应数据给客户端。
手动实现Servlet程序
- 编写一个类来实现Servlet 接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| public class HelloServlet implements Servlet { @Override public void init(ServletConfig servletConfig) throws ServletException { } @Override public ServletConfig getServletConfig() { return null; } @Override public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { System.out.println("hello"); } @Override public String getServletInfo() { return null; } @Override public void destroy() { } }
|
- 配置Servlet到
web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <web-app> <display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>hello</servlet-name> <servlet-class>com.lishx.demo01.HelloServlet</servlet-class> </servlet>
<servlet-mapping> <servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern> </servlet-mapping> </web-app>
|
- 启动Tomcat
常见的错误1:配置访问地址出错,未加/
java.lang.IllegalArgumentException: servlet映射中的<url pattern>[hello]无效
常见的错误2:servlet-name
配置出错
java.lang.IllegalArgumentException: Servlet映射指定未知的Servlet名称[hello1]
常见的错误3:servlet-class
配置出错
java.lang.IllegalArgumentException: servlet映射中的<url pattern>[hello]无效
url地址到Servlet程序的访问路径
TODO
Servlet的生命周期
- 执行Servlet构造器方法
- 执行init初始化方法
- 执行service方法
- 执行destroy销毁方法
service方法每次访问都会调用,其他方法只在创建和销毁时才会调用
GET和POST请求的分发处理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| @Override public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest; String method = httpServletRequest.getMethod(); if("GET".equals(method)){ doGET(); }else if("POST".equals(method)){ doPOST(); } System.out.println("hello"); }
private void doPOST() { System.out.println("doPOST"); }
private void doGET() { System.out.println("doGET"); }
|
通过继承HttpServlet实现Servlet程序
一般在项目开发中,都是使用HttpServlet
类的方式去实现Servlet程序。
- 编写一个类去继承
HttpServlet
- 根据业务需要重写doGet 或者doPost 方法
1 2 3 4 5 6 7 8 9 10 11 12 13
| public class HelloServlet2 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doGet(req, resp); }
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doPost(req, resp); } }
|
- 到web.xml中配置Servlet程序的访问地址
1 2 3 4 5 6 7 8 9
| <servlet> <servlet-name>hello2</servlet-name> <servlet-class>com.lishx.demo01.HelloServlet2</servlet-class> </servlet>
<servlet-mapping> <servlet-name>hello2</servlet-name> <url-pattern>/hello2</url-pattern> </servlet-mapping>
|
使用IDEA创建Servlet程序

ServletConfig类
ServletConfig类从类名上来看是Servlet的配置信息类
Servlet程序和ServletConfig对象都是有tomcat负责创建,我们负责使用。
Servlet程序默认是第一次访问的时候创建,ServletConfig是每个Servlet程序创建时,就创建一个对应的ServletConfig对象。
ServletConfig类的三大作用
- 可以获取Servlet程序的别名 servlet-name的值
- 获取初始化参数init-param
- 获取ServletContext对象
获取方式
1 2 3 4 5 6
| @Override public void init(ServletConfig servletConfig) throws ServletException{ System.out.println("servlet-name = " + servletConfig.getInitParameter("username")); System.out.println("初始化参数 = " + servletConfig.getInitParameter("username")); System.out.println("获取ServletContext对象 = " + servletConfig.getServletContext()); }
|
配置内容
1 2 3 4 5 6 7 8 9
| <servlet> <servlet-name>hello</servlet-name> <servlet-class>com.lishx.demo01.HelloServlet</servlet-class> <init-param> <param-name>username</param-name> <param-value>root</param-value> </init-param> </servlet>
|
获取ServletConfig
的其他方式
1 2 3 4 5
| @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doGet(req, resp); ServletConfig servletConfig = getServletConfig(); }
|
请注意 如果重写 init 方法的话, 必须要 super.init(config)
调用父类的方法
ServletContext类
什么是ServletContext?
- servletContext是一个接口,他表示Servlet上下文对象
- 一个web工程,只有一个ServletContext对象实例
- ServletContext是一个域对象
- ServletContext是在web工程部署启动的时候创建,在web工程停止的时候销毁
什么是域对象?
域对象是可以向Map一样存取数据的对象
这里的域指的是存取数据的操作范围:整个web工程
|
存数据 |
取数据 |
删除数据 |
Map |
put() |
get() |
remove() |
域对象 |
putAttribute() |
getAttribute() |
removeAttribute() |
ServletContext的四个常见作用
- 获取web.xml中配置的上下文参数
- 获取当前的工程路径
- 获取工程部署后在服务器硬盘上的绝对路径
- 像map一样存取数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| @WebServlet(name = "ServletContext") public class ServletContext extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
javax.servlet.ServletContext servletContext = getServletConfig().getServletContext();
String name = servletContext.getInitParameter("name"); System.out.println(name);
String contextPath = servletContext.getContextPath();
String realPath = servletContext.getRealPath("/"); }
|