Servlet是用Java编写的服务器端程序,运行于支持Java的Web应用服务器中,可以响应HTTP请求
void init() throws ServletException
在Servlet第一次被创建时才会被调用,且只调用一次
void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException
Servlet容器在这里处理请求,根据请求的方法调用相应的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";
销毁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()");
}
}
在/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>
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()");
}
}
/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>
日志信息输出如下:
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()
原文:https://www.cnblogs.com/myibu/p/12776181.html