(1) Method 1:
// This is the first way to implement servlet, using the time-limit servlet interface method to achieve, use the need to reference servlet-api.jar
Package COM. LC; import javax. servlet. *; import Java. io. *; import javax. servlet. servletconfig; import javax. servlet. servletexception; import javax. servlet. servletrequest; import javax. servlet. servletresponse; import Java. io. ioexception; public class Hello implements servlet {// This function is used to initialize the servlet (similar to the class constructor) // This function will only be called once (when the user first accesses the servlet is called) Public void Init (servletconfig parm1) throws servletexception {system. ou T. println ("init it! ");} Public servletconfig getservletconfig () {return NULL ;} // This function is used to process the business logic. // The programmer should write the business logic code here. // The Code will be called when the user accesses the servlet. // req is used to obtain the client information // res users want the client to return information (which can be considered as a browser) public void Service (servletrequest req, servletresponse res) throws servletexception, ioexception {// get printwritersystem from res. out. println ("service it! "); // Display printwriter PW = res. getwriter (); PW. println (" Hello world! "); // Display in the browser} Public String getservletinfo () {return" ";}// call destroy in three cases: // 1. reload the servlet (webapps) // 2. disable Tomcat // 3. shut down public void destroy () {// destroy the servt instance is actually releasing the memory system. out. println ("Destroy it! ");}}
(2) Method 2:
// The second servlet program is implemented to inherit from genericservlet. This method is very simple package COM. tsinghua; import javax. servlet. genericservlet; import Java. io. *; import javax. servlet. *; public class hellogenericservlet extends genericservlet {// rewrite the service method to public void Service (servletrequest req, servletresponse res) {// return Hello world! Generictry {printwriter PW = res. getwriter (); PW. println ("Hello world! Generic! ");} Catch (exception ex) {ex. printstacktrace ();}}}
(3) method 3:
// The third Servlet implementation program inherits httpservletpackage COM. tsinghua; import javax. servlet. HTTP. *; import Java. io. *; public class hellohttp extends httpservlet {// process get requests // Req: used to obtain client (browser) Information // Res: used to send to client (browser) public void doget (httpservletrequest req, httpservletresponse res) {// business logic try {printwriter PW = res. getwriter (); PW. println ("Hello HTTP! ");} Catch (exception ex) {ex. printstacktrace () ;}/// process post requests // Req: used to obtain client (browser) Information // Res: used to send to client (browser) public void dopost (httpservletrequest req, httpservletresponse res) {This. doget (req, Res );}}
You need to configure the Web. xml file when implementing the above three methods. The Code is as follows:
<? XML version = "1.0" encoding = "ISO-8859-1"?> <Web-app xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version = "3.0" metadata-complete = "true"> <display-Name> welcome to Tomcat </display-Name> <description> welcome to Tomcat </description> <! -- JSP servlet mappings starts --> <servlet> <! -- Give your servlet any name --> <servlet-Name> Hello </servlet-Name> <! -- Specify the Servlet Path (package name + class name) --> <servlet-class> COM. tsinghua. hello </servlet-class> </servlet> <servlet-mapping> <! -- The name of the servlet above is the same --> <servlet-Name> Hello </servlet-Name> <! -- This is the URL entered in the browser to access the servlet --> <URL-pattern>/helloworld </url-pattern> </servlet-mapping> <servlet-name> hellogenericservlet </servlet-Name> <servlet-class> COM. tsinghua. hellogenericservlet </servlet-class> </servlet> <servlet-mapping> <servlet-Name> hellogenericservlet </servlet-Name> <URL-pattern>/hellogeneric </url-Pattern> </servlet-mapping> <servlet-Name> hellohttp </servlet-Name> <se Rvlet-class> COM. tsinghua. hellohttp </servlet-class> </servlet> <servlet-mapping> <servlet-Name> hellohttp </servlet-Name> <URL-pattern>/hellohttp </url-Pattern> </servlet-mapping> <! -- JSP servlet mappings end --> </Web-app>
OK!
Servlet implementation methods