Three major examples of Servlet method implementation

Source: Internet
Author: User

The Servlet method is first developed by implementing the servlet interface.

 
  1. /***//**
  2. * @ (#) Hello. java
  3. *
  4. *
  5. * @ Author shiyi05
  6. * @ Version 1.00 2008/5/9
  7. */
  8. // This is my first servlet. It is developed using the implements Servlet to implement the servlet interface.
  9. Package com. rao;
  10. Import javax. servlet .*;
  11. Import javax. servlet. ServletConfig;
  12. Import javax. servlet. ServletException;
  13. Import javax. servlet. ServletRequest;
  14. Import javax. servlet. ServletResponse;
  15. Import java. io. IOException;
  16. Import javax. servlet .*;
  17. Import java. io .*;
  18.  
  19.  
  20.  
  21. Public class Hello implements Servlet
  22. ...{
  23.  
  24. Public Hello ()
  25. ...{
  26. }
  27. /***//**
  28. * Method init
  29. *
  30. *
  31. * @ Param parm1
  32. *
  33. @ Throws ServletException
  34. *
  35. */
  36. // This function is used to initialize the servlet. This function will only be called once when the user accesses the servlet for the first time)
  37. Public void init (ServletConfig parm1) throws ServletException
  38. ...{
  39. // TODO: Add your code here
  40. System. out. println ("init ");
  41. }
  42.  
  43. /***//**
  44. * Method getServletConfig
  45. *
  46. *
  47. * @ Return
  48. *
  49. */
  50. Public ServletConfig getServletConfig ()
  51. ...{
  52. // TODO: Add your code here
  53. Return null;
  54. }
  55.  
  56. /***//**
  57. * Method service
  58. *
  59. *
  60. * @ Param req is used to obtain client information.
  61. * @ Param res is used to return information to the client.
  62. *
  63. @ Throws ServletException
  64. @ Throws IOException
  65. *
  66. */
  67. // This function is used to process the business logic. Each time a user accesses the servlet, it is called
  68. Public void service (ServletRequest req, ServletResponse res) throws ServletException, IOException
  69. ...{
  70. // TODO: Add your code here
  71. System. out. println ("service ");
  72. PrintWriterPw=Res. GetWriter ();
  73. Pw. println ("Hello world! ");
  74. }
  75.  
  76. /***//**
  77. * Method getServletInfo
  78. *
  79. *
  80. * @ Return
  81. *
  82. */
  83. Public String getServletInfo ()
  84. ...{
  85. // TODO: Add your code here
  86. Return "";
  87. }
  88.  
  89. /***//**
  90. * Method destroy
  91. *
  92. *
  93. */
  94. Public void destroy ()
  95. ...{
  96. // TODO: Add your code here
  97. System. out. println ("destroy ");
  98. }
  99. }


Implement the second Servlet method by inheriting GenericServlet Development

 
  1. /***//**
  2. * @ (#) HelloGen. java
  3. *
  4. *
  5. * @ Author shiyi05
  6. * @ Version 1.00 2008/5/9
  7. */
  8.  
  9.  
  10. // This is the second way to develop servlet. It is developed by inheriting GenericServlet
  11. Package com. rao;
  12. Import javax. servlet. GenericServlet;
  13. Import java. io .*;
  14. Import javax. servlet .*;
  15.  
  16.  
  17. Public class HelloGen extends GenericServlet
  18. ...{
  19.  
  20. Public HelloGen ()
  21. ...{
  22. }
  23. // Rewrite the service () method
  24. Public void service (ServletRequest req, ServletResponse res)
  25. ...{
  26. Try
  27. ...{
  28. PrintWriterPw=Res. GetWriter ();
  29. Pw. println ("Hello World, Generic ");
  30. }
  31. Catch (Exception ex)
  32. ...{
  33. Ex. printStackTrace ();
  34. }
  35. }
  36. }

The third and most common Servlet method is implemented by inheriting the HttpServlet class.

 
  1. /**
  2. * @ (#) HelloHttp. java
  3. * @ Author shiyi05
  4. * @ Version 1.00 2008/5/9
  5. */
  6. // This is the third servlet development method, which is implemented by inheriting the HttpServlet class.
  7. Package com. rao;
  8. Import javax. servlet. http .*;
  9. Import java. io .*;
  10. Public class HelloHttp extends HttpServlet
  11. ...{
  12.  
  13. Public HelloHttp ()
  14. ...{
  15. }
  16. // Process Get requests
  17. Public void doGet (HttpServletRequest req, HttpServletResponse res)
  18. ...{
  19. Try
  20. ...{
  21. PrintWriterPw=Res. GetWriter ();
  22. Pw. println ("Hello World, HttpServlet ");
  23. }
  24. Catch (Exception ex)
  25. ...{
  26. Ex. printStackTrace ();
  27. }
  28. }
  29. Public void doPost (HttpServletRequest req, HttpServletResponse res)
  30. ...{
  31. This. doGet (req, res );
  32. }
  33. }

Finally, deploy the three servlets in the web. xml file.

 
  1. <? XmlVersion="1.0" Encoding=ISO-8859-1"? >
  2.  
  3. <Web-appXmlns=Http://java.sun.com/xml/ns/javaee" 
  4. Xmlns: xsi=Http://www.w3.org/2001/XMLSchema-instance" 
  5. Xsi: schemaLocation=Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
  6. Version="2.5">
  7.  
  8. <Display-name> Welcome to Tomcat </display-name>
  9. <Description>
  10. Welcome to Tomcat
  11. </Description>
  12. <Servlet>
  13. <! -- Servlet name, which is arbitrary -->
  14. <Servlet-name> Hello </servlet-name>
  15. <! -- Specify the servlet Path, package name + class name -->
  16. <Servlet-class> com. rao. Hello </servlet-class>
  17. </Servlet>
  18. <Servlet-mapping>
  19. <! -- Same as above -->
  20. <Servlet-name> Hello </servlet-name>
  21. <! -- The URL used to access the servlet in the browser is arbitrary -->
  22. <Url-pattern>/Hello </url-pattern>
  23. </Servlet-mapping>
  24. <Servlet>
  25. <! -- Servlet name, which is arbitrary -->
  26. <Servlet-name> HelloGen </servlet-name>
  27. <! -- Specify the servlet Path, package name + class name -->
  28. <Servlet-class> com. rao. HelloGen </servlet-class>
  29. </Servlet>
  30. <Servlet-mapping>
  31. <! -- Same as above -->
  32. <Servlet-name> HelloGen </servlet-name>
  33. <! -- The URL used to access the servlet in the browser is arbitrary -->
  34. <Url-pattern>/HelloGen </url-pattern>
  35. </Servlet-mapping>
  36. <Servlet>
  37. <! -- Servlet name, which is arbitrary -->
  38. <Servlet-name> HelloHttp </servlet-name>
  39. <! -- Specify the servlet Path, package name + class name -->
  40. <Servlet-class> com. rao. HelloHttp </servlet-class>
  41. </Servlet>
  42. <Servlet-mapping>
  43. <! -- Same as above -->
  44. <Servlet-name> HelloHttp </servlet-name>
  45. <! -- The URL used to access the servlet in the browser is arbitrary -->
  46. <Url-pattern>/HelloHttp </url-pattern>
  47. </Servlet-mapping>
  48.  
  49. </Web-app>

The Servlet method is developed in three ways !! I hope this will help you implement the Servlet method.

  1. What are servlets and common Servlet APIs?
  2. 8-point discussion on optimizing JSP Servlet applications
  3. Select JSP development tools
  4. Servlet and JSP paths
  5. Analysis of form data in JSP Servlet Technology

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.