1 创建一个类实现Servlet
package com.duowei.servlet;
import javax.servlet.*;
import javax.swing.*;
import java.io.IOException;
public class TestServlet implements Servlet {
public void init(ServletConfig servletConfig) throws ServletException {
System.out.println("init......");
}
public ServletConfig getServletConfig() {
return null;
}
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
System.out.println("service......");
}
public String getServletInfo() {
return null;
}
public void destroy() {
System.out.println("destroy......");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>helloServlet</servlet-name>
<servlet-class>com.duowei.servlet.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloServlet</servlet-name>
<url-pattern>/helloServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>helloHttpServlet</servlet-name>
<servlet-class>com.duowei.servlet.TestHttpServlet</servlet-class>
</servlet>
</web-app >
启动Tomcat看一下init、service、destroy什么时候执行
我们发现 init 在tomcat 启动时候执行,destroy在tomcat结束运行时执行,service在每次访问servlet时候执行。
试一下,多次访问servlet,只执行service,不会再执行init了,可见Servlet是单例模式
2 创建一个类继承HttpServlet
package com.duowei.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class TestHttpServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req, resp);
System.out.println("TestHttpServlet.................doGet");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
System.out.println("TestHttpServlet.................doPost");
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.service(req, resp);
System.out.println("TestHttpServlet.................service");
}
}
注册servlet
<servlet>
<servlet-name>helloHttpServlet</servlet-name>
<servlet-class>com.duowei.servlet.TestHttpServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloHttpServlet</servlet-name>
<url-pattern>/helloHttpServlet</url-pattern>
</servlet-mapping>
访问以下TestHttpServlet
发现输出
TestHttpServlet…doGet
TestHttpServlet…service
为什么先输出doGet再输出service
- 因为我这里重写了service方法,所以访问TestHttpServlet时候会执行我重写的service方法
- 另外,我在service中调用了super.service(req, resp);那么在访问TestHttpServlet的时候,输出System.out.println(“TestHttpServlet…service”);前会执行父类的service方法
- 我们看一下父类的service方法,也就是HttpServlet会帮我们判断是GET请求方式的时候,调用doGet方法,所以先打印了TestHttpServlet…doGet;后打印了TestHttpServlet…service
上述所说是为了让我们了解
- 浏览器通过URL访问Tomcat;
- 创建request请求对象,response响应对象;
- Tomcat根据URL找到HttpServlet的类路径,创建HttpServlet实例;
- HttpServlet实例调用service方法,并判断GET和POST去执行doGet或者doPost,以参数形式传入request,response;
- 返回response对象