1.什么是servlet ?
servlet是java运行在服务器端的应用程序,本质是java 类库
2.servlet的作用是什么?
接收客户端的请求,处理请求,给出响应
3.如何创建servlet
1)继承HttpServlet 或者实现Servlet接口
2)
web.xml 配置Servlet映射:
<servlet>
<servlet-name>BookServlet</servlet-name>
<servlet-class>com.servlet.deno.BookServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>BookServlet</servlet-name>
<url-pattern>/servlet/BookServlet</url-pattern>
</servlet-mapping>
或者使用注解
@WebServlet(name="bookServlet",urlPatterns="/demo/book")
4.Servlet的生命周期
以继承HttpServlet接口为例
1)实例化Servlet对象, 第一次请求的时候实例化,只会实例化一次(说明servlet是单例的,线程不安全的,成员变量的使用需要谨慎)
2)执行初始化init ,第一次请求的时候,只执行一次
3)当请求来临的时候,Service方法根据请求类型不同,分发请求doGet,doPost,doPut...
4) doGet,doPost处理请求,给出响应
5)销毁Servlet,容器关闭的时候,执行destory
package com.servlet.deno; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet(name="bookServlet",urlPatterns="/demo/book") public class BookServlet extends HttpServlet { /** * Constructor of the object. */ public BookServlet() { System.out.println("-----------servlet instance----------------"); } @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("----------service 分发请求---------"); super.service(req, resp); } /** * Destruction of the servlet. <br> */ public void destroy() { System.out.println("******************************************"); System.out.println("-----------------destory----------------"); } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("-------------doGet----------------"); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("----------------doPost------------------"); doGet(request, response); } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init() throws ServletException { System.out.println("---------------servlet init---------------"); } }
控制台输出:
INFO: Deployment of web application directory D:\tomcat\apache-tomcat-7.0.72\webapps\Servlet has finished in 1,291 ms
二月 21, 2020 3:02:38 下午 org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8088"]
二月 21, 2020 3:02:38 下午 org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8089"]
二月 21, 2020 3:02:38 下午 org.apache.catalina.startup.Catalina start
INFO: Server startup in 2435 ms
-----------servlet instance----------------
---------------servlet init---------------
----------service 分发请求---------
-------------doGet----------------
二月 21, 2020 3:02:47 下午 org.apache.catalina.core.StandardServer await
INFO: A valid shutdown command was received via the shutdown port. Stopping the Server instance.
二月 21, 2020 3:02:47 下午 org.apache.coyote.AbstractProtocol pause
INFO: Pausing ProtocolHandler ["http-bio-8088"]
二月 21, 2020 3:02:47 下午 org.apache.coyote.AbstractProtocol pause
INFO: Pausing ProtocolHandler ["ajp-bio-8089"]
二月 21, 2020 3:02:47 下午 org.apache.catalina.core.StandardService stopInternal
INFO: Stopping service Catalina
******************************************
-----------------destory----------------