Servlet implementation methods

Source: Internet
Author: User

Servlet implementation methods

(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!) on the console !); // 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:

 

 

 
   
  
   
Welcome to Tomcat
    
  
   
Welcome to Tomcat
    
    
      
       
   
    
Hello
       
       
   
    
Com. tsinghua. Hello
     
     
      
       
   
    
Hello
       
       
   
    
/Helloworld
     
     
      
   
    
HelloGenericServlet
       
   
    
Com. tsinghua. HelloGenericServlet
     
     
      
   
    
HelloGenericServlet
       
   
    
/HelloGeneric
     
     
      
   
    
HelloHttp
       
   
    
Com. tsinghua. HelloHttp
     
     
      
   
    
HelloHttp
       
   
    
/HelloHttp
     
   
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.