This article Source: GitHub · Click here || GitEE · Click here
A, Servlet Introduction
Server-side program written in Java, platform-independent and protocol features, the main function is to interactively browse and generate data, generate dynamic Web content. Use Servlet, can collect user input from a Web form, showing records from a database or other source, you can also create dynamic web pages.
Second, implementation
1, inherited HttpServlet
- About API
Inherited from GenericServlet comply with HTTP protocol implementation, design patterns at an angle of view, HttpServlet abstract template role as the template method: as a service () method. Basic methods: as a doPost (), doGet () methods. service () method process, part of the decision logic is omitted. This method is called seven do one or several methods, completion response to client requests. These methods do need to provide a concrete subclass of HttpServlet, the API package template method is a typical pattern.
- Code Cases
public class ServletOneImpl extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
response.getWriter().print("执行:doGet");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
response.getWriter().print("执行:doPost");
}
}
2, inherited GenericServlet
- About API
ServletConfig Servlet interface and the interface implementation class. An abstract class which service method is an abstract method.
- Code Cases
public class ServletTwoImpl extends GenericServlet {
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse)
throws ServletException, IOException {
HttpServletResponse response = (HttpServletResponse)servletResponse ;
response.setContentType("text/html;charset=utf-8");
response.getWriter().print("执行:service");
}
}
3, implement the interface Servlet
- About API
Servlet is an interface, which comprises init, getServletConfig, service, getServletInfo, destroy several core method.
- Code Cases
public class ServletThreeImpl implements Servlet {
@Override
public void init(ServletConfig servletConfig) throws ServletException {
servletConfig.getServletName();
System.out.println("init 被调用...");
}
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse)
throws ServletException, IOException {
System.out.println("ThreadId:"+Thread.currentThread().getId());
System.out.println("service 被调用...");
HttpServletResponse response = (HttpServletResponse)servletResponse ;
response.getWriter().print("Servlet.Life");
}
@Override
public void destroy() {
System.out.println("destroy 被调用...");
}
@Override
public ServletConfig getServletConfig() {
System.out.println("getServletConfig 被调用...");
return null;
}
@Override
public String getServletInfo() {
System.out.println("getServletInfo 被调用...");
return null;
}
}
Third, the life cycle
- Loading and instantiation
Servlet container or when the client sends a request to start, if the Servlet container looks Servlet examples exist, if present, directly read the response request instance; if not, create a Servlet examples (of singleton design pattern). load-on-startup can be configured to create timing.
- Initialization: init ()
Instantiated, Servlet container calls the init method once and initializes the current Servlet.
- Services: service ()
After initialization, Servlet in a ready state in response to the request. Upon receiving the client request, call service () method of handling client requests, the HttpServlet's service () method will call different methods depending on the request templates.
- Destruction: destroy ()
When the Servlet container is closed, Servlet examples also destroyed at any time. You can print to see the implementation of the method by Tomcat log when you shut down the service.
Fourth, the running configuration
1, web.xml configuration
<servlet>
<servlet-name>servletOneImpl</servlet-name>
<servlet-class>com.node01.servlet.impl.ServletOneImpl</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servletOneImpl</servlet-name>
<url-pattern>/servletOneImpl</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>servletTwoImpl</servlet-name>
<servlet-class>com.node01.servlet.impl.ServletTwoImpl</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servletTwoImpl</servlet-name>
<url-pattern>/servletTwoImpl</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>servletThreeImpl</servlet-name>
<servlet-class>com.node01.servlet.impl.ServletThreeImpl</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servletThreeImpl</servlet-name>
<url-pattern>/servletThreeImpl</url-pattern>
</servlet-mapping>
Request: http://localhost:6003/servletOneImpl
test.
- servlet-name: Servlet registered name.
- servlet-class: Servlet class full path name.
- serlvet-mapping: the same Servlet may be mapped to the plurality of URL.
- url-pattern: to map the path Servlet access.
2, thread pool running
Log observed above third printing Servlet implementations: Thread.currentThread().getId());
.
ThreadId:32
ThreadId:33
ThreadId:32
ThreadId:31
ThreadId:32
Here difficult to find, Servlet thread pool to the modalities of implementation.
Fifth, the source code address
GitHub·地址
https://github.com/cicadasmile/java-base-parent
GitEE·地址
https://gitee.com/cicadasmile/java-base-parent