This document focuses on how to create and run a servlet project under JDK7+TOMCAT7 using Eclipse.
Installation specific steps from the online search can be found, here no longer repeat.
Switch to the Java EE in Eclipse,
From the navigation menu, locate the Show view option under Windows and locate the server under Show view.
Create a tomcat server in the server.
and find->new->dynamic Web project from the navigation menu file.
In Project name, enter the project names MyServlet001
Continue to the next step, until the following steps:
Be sure to check the Generate Web. XML deployment Description option, otherwise the Web. xml file will be missing under/webcontent/web-inf/.
The new engineering structure is as follows:
We add the package Com.dx.hello in SRC and create the Java class Helloservlet.class, and let the class implement the Init,service,distory,getservletconfig of the servlet, The Getservletinfo function and adds a constructor for the HelloServlet class, and outputs the name of the function inside each function.
1 Package Com.dx.hello;2 3 import java.io.IOException;4 import Javax.servlet.Servlet;5 import Javax.servlet.ServletConfig;6 import javax.servlet.ServletException;7 import javax.servlet.ServletRequest;8 import Javax.servlet.ServletResponse;9 Ten Public classHelloServlet implements Servlet { One PublicHelloServlet () { ASystem. out. println ("HelloServlet Constructor"); - } - the Public voidinit (servletconfig config) throws servletexception { -System. out. println ("Init"); - } - + Public voiddestroy () { -System. out. println ("Destroy"); + } A at PublicServletConfig Getservletconfig () { -System. out. println ("Getservletconfig"); - return NULL; - } - - PublicString Getservletinfo () { inSystem. out. println ("Getservletinfo"); - return NULL; to } + - Public voidService (ServletRequest request, servletresponse response) throws Servletexception, IOException { theSystem. out. println ("Service"); * } $ Panax Notoginseng}
Modifying a configuration file Web. xml
1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Web-appXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"3 xmlns= "Http://java.sun.com/xml/ns/javaee"4 xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"5 ID= "webapp_id"version= "3.0">6 7 <!--Configuring and Mapping Servlets -8 <servlet>9 <!--Name of servlet registration -Ten <Servlet-name>HelloServlet</Servlet-name> One <!--servlet Full class name - A <Servlet-class>Com.dx.hello.HelloServlet</Servlet-class> - </servlet> - <servlet-mapping> the <!--The registration name for the servlet-name under the servlet node is identical - - <Servlet-name>HelloServlet</Servlet-name> - <!--Map a specific access path, where/represents the root directory of the current web - - <Url-pattern>/hello</Url-pattern> + </servlet-mapping> - + </Web-app>
Start Tomcat, and enter Http://localhost:8080/MyServlet001/hello in the browser. The carriage return will appear in the browser with a blank page.
This is where we look at the Tomcat log information in Eclipse and find the following information:
。。。 Info:command line argument:-DFILE.ENCODING=GBK February 27, 2016 10:10:44 pm Org.apache.catalina.core.AprLifecycleListener Lifecycleeventinfo:the APR based Apache Tomcat Native Library which allows optimal performance in production environments Was wasn't found on the Java.library.path:d:\java\jdk1.6\jre7\bin; C:\Windows\Sun\Java\bin; C:\Windows\system32; C:\windows;d:/program files/java/jdk1.7.0_79/bin/. /jre/bin/server;d:/program files/java/jdk1.7.0_79/bin/. /jre/bin;d:/program files/java/jdk1.7.0_79/bin/. /JRE/LIB/AMD64; E:\SourceCode\lihui\Network\LTEPlanDesktop\LTEPlanDesktop-ZJ\DTGroup.LTEPlanDesktop.WinUI\References\SuperMap\ ;D: \program Files\java\jdk1.7.0_79\bin;d:\program Files\java\jdk1.7.0_79\jre\bin;d:\java\axis2-1.6.3\bin; C:\Windows\system32; C:\Windows; C:\Windows\System32\Wbem; C:\Windows\System32\WindowsPowerShell\v1.0\;D: \program Files (x86) \microsoft SQL server\100\tools\binn\;D: \program Files\Microsoft SQL Server\100\tools\binn\;D: \program Files\Microsoft SQL Server\100\dts\binn\;D: \program Files (x86) \microsoft SQL Server\100\tools\binn\vsshell\common7\ide\; C:\Program files (x86) \microsoft Visual Studio 9.0\common7\ide\privateassemblies\;D: \program Files (x86) \microsoft SQL server\100\dts\binn\;D: \program Files (x86) \scala\bin; C:\Program Files\Microsoft SQL Server\110\tools\binn\; C:\Program Files (x86) \scala\bin; C:\Program Files\microsoft\web Platform Installer\;%userprofile%\.dnx\bin; C:\Program Files\Microsoft Dnx\dnvm\;d: \appserv\php5.5;;D: \ Java\eclipse-jee-mars-1-win32-x86_64\eclipse;;. February 27, 2016 10:10:44 pm org.apache.coyote.AbstractProtocol initinfo:initializing protocolhandler ["http-bio-8080"] 27 , 2016 10:10:44 pm org.apache.coyote.AbstractProtocol initinfo:initializing protocolhandler ["ajp-bio-8009"] 27, 2016 10:10:44 pm Org.apache.catalina.startup.Catalina loadinfo:initialization processed in 1481 Ms February 27, 2016 10:10:44 pm org. Apache.catalina.core.StandardService startinternalinfo:starting service Catalina February 27, 2016 10:10:44 pm ORG.APACHe.catalina.core.StandardEngine startinternalinfo:starting Servlet Engine:apache tomcat/7.0.65 February 27, 2016 10:10:46 pm O Rg.apache.coyote.AbstractProtocol startinfo:starting Protocolhandler ["http-bio-8080"] 27, 2016 10:10:46 pm Org.apache.coyote.AbstractProtocol startinfo:starting Protocolhandler ["ajp-bio-8009"] 27, 2016 10:10:46 pm Org.apache.catalina.startup.Catalina Startinfo:server Startup in 1379 MSHelloServlet Constructorinitservice
Then, when you stop the Tomcat Server service in Eclipse, the Tomcat server log information is sent:
February 27, 2016 10:12:45 pm org.apache.catalina.core.StandardServer awaitinfo:a Valid shutdown command was received via the S Hutdown Port. Stopping the Server instance. 27, 2016 10:12:45 pm Org.apache.coyote.AbstractProtocol pauseinfo:pausing protocolhandle R ["http-bio-8080"] 27, 2016 10:12:45 pm org.apache.coyote.AbstractProtocol pauseinfo:pausing Protocolhandler [" ajp-bio-8009 "] 27, 2016 10:12:45 pm org.apache.catalina.core.StandardService stopinternalinfo:stopping Service CatalinaDestroyFebruary 27, 2016 10:12:45 pm org.apache.coyote.AbstractProtocol stopinfo:stopping protocolhandler ["http-bio-8080"] 27, 201 6 10:12:45 pm Org.apache.coyote.AbstractProtocol stopinfo:stopping protocolhandler ["ajp-bio-8009"] 27, 2016 10:12:45 PM Org.apache.coyote.AbstractProtocol destroyinfo:destroying Protocolhandler ["http-bio-8080"] 27, 2016 10:12:45 pm Org.apache.coyote.AbstractProtocol destroyinfo:destroying Protocolhandler ["ajp-bio-8009"]
Yes, the above log information is marked with red font information, It is the information that is printed in the Helloservlet.java function, the entire process from the initialization of a servlet to the end of the entire life cycle, and Tomcat is a container for managing the servlet lifecycle.
How Servlets work:
Web. XML is a path that must be configured if accessed: Http://localhost:8080/MyServlet001/hello, Tomcat finds the corresponding Servlet-mapping collection based on the configured web. XML information, finds the Servlet-class option under the Servlet node based on the Servlet-name in the collection, and invokes the corresponding function information in the class to receive and work with the client.
From the way the servlet works, we can also send it, and Tomcat is a running container for the servlet, which controls the entire life cycle of the servlet.
In the next section, we document the details of how the servlet is used and how it works.
Creation of Java Servlet project (jdk7+tomcat7+eclipse)