Three methods for compiling Servlet

Source: Internet
Author: User

Who said that the Servlet compilation must inherit from the HttpServlet class and must implement doGet) or doPost). Isn't it possible to implement the Servlet interface?

In fact, there are three main methods to compile Servlet: one is to implement the Servlet interface, the other is to inherit the abstract class GenericServlet, and the third is to inherit the HttpServlet class. The following three methods are used to implement Servlet:

1) It is difficult to compile a Servlet from the Servlet interface, because all the methods in the Servlet interface must be implemented. The Servlet interface mainly defines five methods:

(1) void init (ServletConfig config) throws ServletException
(2) ServletConfig getServletConfig ()
(3) void service (ServletRequest req, ervletResponse res) throws ServletException, IOException
(4) String getServletInfo ()
(5) void destroy ()

The following describes how to compile a Servlet using the Servlet interface:

 
 
  1. Import javax. servlet .*;
  2. Import java. io .*;
  3. Public class TestServlet implements Servlet {
  4. Public void init (ServletConfig config) throws ServletException {
  5. System. out. println ("init ");
  6. }
  7. Public ServletConfig getServletConfig (){
  8. Reture null;
  9. }
  10. Public void service (ServletRequest req, ServletResponse res)
  11. Throws ServletException, IOException {
  12. // The content to be done after the request is implemented here
  13. PrintWriterOut=Response. GetWriter ();
  14. Out. println ("Hello World! ");
  15.  
  16. }
  17. Public String getServletInfo (){
  18. Return null;
  19. }
  20. Public void destroy (){
  21. System. out. println ("destory ");
  22. }
  23. }

(2) inherit the abstract class GenericServlet to compile Serlvet, which is easier than implementing Servlet interfaces. The GenericServlet class only has one abstract method, namely serviceServletRequest req and ServletResponse res ), you only need to implement this method. The following is an example:

 
 
  1. import javax.servlet.*;  
  2. import java.io.IOException;  
  3.  
  4. public TestGenericServlet extends GenericServlet{  
  5.     public abstract void service(ServletRequest req,ServletResponse res)  
  6.                       throws ServletException,IOException{  
  7.      PrintWriter out = response.getWriter();  
  8.      out.println("Hello World!");                       
  9.      }  

3) it should be the easiest and most common to inherit from the HttpServlet class to write Servlet. We generally need to write Servlet to directly inherit this class, rewrite doGet) or doPost) method. The following is an example:

 
 
  1. import javax.servlet.http.*;  
  2. import javax.servlet.*;  
  3. import java.io.*;  
  4.  
  5. public TestHttpServlet extends HttpServlet{  
  6.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  7.         throws IOException, ServletException {  
  8.   response.setContentType("text/html;charset=gb2312");  
  9.   PrintWriter out = response.getWriter();  
  10.  
  11.   out.println("<html>");  
  12.   out.println("<head>");  
  13.   out.println("<title>HelloWorld</title>");  
  14.   out.println("</head>");  
  15.   out.println("<body bgcolor=\"white\">");  
  16.   out.println("<hr>");  
  17.   out.println("HelloWorld");  
  18.   out.println("</body></html>");  
  19.  }  
  20.  
  21.  public void doPost(HttpServletRequest request, HttpServletResponse response)  
  22.    throws IOException, ServletException {  
  23.   doGet(request, response);  
  24.  }  
  25.  
  1. Scala Servlet in Scala Language
  2. Servlet API Discussion
  3. Introduction to Servlet containers and Context
  4. Servlet Source file to Class Process
  5. Details about Listener listening to Http sessions

Related Article

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.