About Servlet
The Java servlet is a server-side component that is not related to the peace platform and runs in a servlet container. The servlet container is responsible for the communication between the servlet and the client and calls the Servlet method, where the servlet container refers to the "request/response" pattern for Tomcat,servlet and client communication.
Servlets can perform the following functions:
--Create and return dynamic HTML pages based on customer requests
--Create a non-HTML page (HTML fragment) that can be embedded in an existing HTML page.
--Communicating with other server resources, such as databases or Java-based applications
Directly below the code
Create a new Dynamic Web project with the same first
Create Class Helloservlet.java
1 PackageCom.hello.testservlet;2 3 Importjava.io.IOException;4 5 ImportJavax.servlet.Servlet;6 ImportJavax.servlet.ServletConfig;7 Importjavax.servlet.ServletException;8 Importjavax.servlet.ServletRequest;9 ImportJavax.servlet.ServletResponse;Ten One Public classHelloServletImplementsServlet { A - @Override - Public voiddestroy () { the //TODO auto-generated Method Stub -System.out.println ("Destory"); - } - + @Override - PublicServletConfig Getservletconfig () { + //TODO auto-generated Method Stub ASystem.out.println ("Getservletconfig"); at return NULL; - } - - @Override - PublicString Getservletinfo () { - //TODO auto-generated Method Stub inSystem.out.println ("Getservletinfo"); - return NULL; to } + - @Override the Public voidInit (ServletConfig arg0)throwsservletexception { * //TODO auto-generated Method Stub $System.out.println ("Init");Panax Notoginseng } - the @Override + Public voidService (ServletRequest arg0, Servletresponse arg1)throwsservletexception, IOException { A //TODO auto-generated Method Stub theSYSTEM.OUT.PRINTLN ("service"); + } - $ PublicHelloServlet () { $ Super(); -System.out.println ("HelloServlet ' s constructor"); - } the - Wuyi the}
Edit Web. xml file
1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Web-appXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns= "Http://java.sun.com/xml/ns/javaee"xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"ID= "webapp_id"version= "3.0">3 <Display-name>HelloServlet</Display-name>4 5 <!--Note that Servlet-name needs to be consistent . -6 <servlet>7 <Servlet-name>Hellos</Servlet-name>8 <!--Full class name -9 <Servlet-class>Com.hello.testservlet.HelloServlet</Servlet-class>Ten </servlet> One <servlet-mapping> A <Servlet-name>Hellos</Servlet-name> - <!--Access Path - - <Url-pattern>/hes</Url-pattern> the </servlet-mapping> - - </Web-app>
Right-click Project Run as->run on service
The browser accesses the http://localhost:8080/helloServlet/hes to see the following output in the console
1 helloservlet's constructor2init3service
Http://localhost:8080/helloServlet This part is the project root
The/hes configured in the Url-pattern property is the access path from the project root SIP
Javaweb Core Programming (iii) Servlethello