The concept of "one" servlet
"Two" a simple servlet demo
Part I: Configuration of Web. xml
<!--create a native selevt to re-learn the servlet -<servlet> <Servlet-name>Sxfservlet</Servlet-name> <Servlet-class>Com.yeepay.nobank.sxfservlet.ImgStudyServlet</Servlet-class> <Init-param> <!--init parameter - <Param-name>MyName</Param-name> <Param-value>My name is Shangxiaofei</Param-value> </Init-param> <!--the timing of the Init method invocation of the servlet, when set to negative or not set, is called when the servlet is first used - <Load-on-startup>1</Load-on-startup></servlet><servlet-mapping> <Servlet-name>Sxfservlet</Servlet-name> <Url-pattern>/sxf/*</Url-pattern></servlet-mapping>
View Code
Part II: Source Code of Imgstudyservlet
PackageCom.yeepay.nobank.sxfservlet;Importjava.io.IOException;ImportJavax.servlet.ServletConfig;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;/*** Servlet's *@authorShangxiaofei **/ Public classImgstudyservletextendshttpservlet{/*** called only once throughout the servlet lifecycle for loading configuration information and doing some initialization actions*/@Override Public voidInit (servletconfig config)throwsservletexception {System.out.println ("Imgstudyservlet.init () call the first Init method to start! "); Super. init (config); String mynamestrstring=config.getinitparameter ("MyName"); System.out.println ("Imgstudyservlet.init ()" +mynamestrstring); System.out.println ("Imgstudyservlet.init () call the first Init method end! "); } /*** When the client is a GET request, call the method*/@Overrideprotected voiddoget (httpservletrequest req, HttpServletResponse resp)throwsservletexception, IOException {String param1=req.getparameter ("param"); System.out.println ("Imgstudyservlet.doget ([param]===>)" +param1); } /*** When the client is a POST request, call the method*/@Overrideprotected voidDoPost (httpservletrequest req, HttpServletResponse resp)throwsservletexception, IOException {String param1=req.getparameter ("param"); System.out.println ("Imgstudyservlet.dopost ([param]===>)" +param1); } /*** When the servlet is destroyed, call the method and do some cleanup work*/@Override Public voiddestroy () {System.out.println ("Imgstudyservlet.destroy" ([Destroy ...]) ".... ...)"); } }
View Code
Part III: Talk about Web project packaging, start the Tomcat container. Tomcat's boot log print startup message contains
Part III: Sending requests to Web projects Http://localhost:8080/nobankcard-web/sxf/s?param=tiantianxiangshanghaohaoxuexi,tomcat print logs
Part IV: When Tomcat is turned off, tomcat prints the log
SPRINGMVC Source code reading servlet Part < A >servlet part of the explanation