The Servlet method is first developed by implementing the servlet interface.
- /***//**
- * @ (#) Hello. java
- *
- *
- * @ Author shiyi05
- * @ Version 1.00 2008/5/9
- */
- // This is my first servlet. It is developed using the implements Servlet to implement the servlet interface.
- Package com. rao;
- Import javax. servlet .*;
- Import javax. servlet. ServletConfig;
- Import javax. servlet. ServletException;
- Import javax. servlet. ServletRequest;
- Import javax. servlet. ServletResponse;
- Import java. io. IOException;
- Import javax. servlet .*;
- Import java. io .*;
-
-
-
- Public class Hello implements Servlet
- ...{
-
- Public Hello ()
- ...{
- }
- /***//**
- * Method init
- *
- *
- * @ Param parm1
- *
- @ Throws ServletException
- *
- */
- // 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)
- Public void init (ServletConfig parm1) throws ServletException
- ...{
- // TODO: Add your code here
- System. out. println ("init ");
- }
-
- /***//**
- * Method getServletConfig
- *
- *
- * @ Return
- *
- */
- Public ServletConfig getServletConfig ()
- ...{
- // TODO: Add your code here
- Return null;
- }
-
- /***//**
- * Method service
- *
- *
- * @ Param req is used to obtain client information.
- * @ Param res is used to return information to the client.
- *
- @ Throws ServletException
- @ Throws IOException
- *
- */
- // This function is used to process the business logic. Each time a user accesses the servlet, it is called
- Public void service (ServletRequest req, ServletResponse res) throws ServletException, IOException
- ...{
- // TODO: Add your code here
- System. out. println ("service ");
- PrintWriterPw=Res. GetWriter ();
- Pw. println ("Hello world! ");
- }
-
- /***//**
- * Method getServletInfo
- *
- *
- * @ Return
- *
- */
- Public String getServletInfo ()
- ...{
- // TODO: Add your code here
- Return "";
- }
-
- /***//**
- * Method destroy
- *
- *
- */
- Public void destroy ()
- ...{
- // TODO: Add your code here
- System. out. println ("destroy ");
- }
- }
Implement the second Servlet method by inheriting GenericServlet Development
- /***//**
- * @ (#) HelloGen. java
- *
- *
- * @ Author shiyi05
- * @ Version 1.00 2008/5/9
- */
-
-
- // This is the second way to develop servlet. It is developed by inheriting GenericServlet
- Package com. rao;
- Import javax. servlet. GenericServlet;
- Import java. io .*;
- Import javax. servlet .*;
-
-
- Public class HelloGen extends GenericServlet
- ...{
-
- Public HelloGen ()
- ...{
- }
- // Rewrite the service () method
- Public void service (ServletRequest req, ServletResponse res)
- ...{
- Try
- ...{
- PrintWriterPw=Res. GetWriter ();
- Pw. println ("Hello World, Generic ");
- }
- Catch (Exception ex)
- ...{
- Ex. printStackTrace ();
- }
- }
- }
The third and most common Servlet method is implemented by inheriting the HttpServlet class.
- /**
- * @ (#) HelloHttp. java
- * @ Author shiyi05
- * @ Version 1.00 2008/5/9
- */
- // This is the third servlet development method, which is implemented by inheriting the HttpServlet class.
- Package com. rao;
- Import javax. servlet. http .*;
- Import java. io .*;
- Public class HelloHttp extends HttpServlet
- ...{
-
- Public HelloHttp ()
- ...{
- }
- // Process Get requests
- Public void doGet (HttpServletRequest req, HttpServletResponse res)
- ...{
- Try
- ...{
- PrintWriterPw=Res. GetWriter ();
- Pw. println ("Hello World, HttpServlet ");
- }
- Catch (Exception ex)
- ...{
- Ex. printStackTrace ();
- }
- }
- Public void doPost (HttpServletRequest req, HttpServletResponse res)
- ...{
- This. doGet (req, res );
- }
- }
Finally, deploy the three servlets in the web. xml file.
- <? XmlVersion="1.0" Encoding=ISO-8859-1"? >
-
- <Web-appXmlns=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_2_5.xsd"
- Version="2.5">
-
- <Display-name> Welcome to Tomcat </display-name>
- <Description>
- Welcome to Tomcat
- </Description>
- <Servlet>
- <! -- Servlet name, which is arbitrary -->
- <Servlet-name> Hello </servlet-name>
- <! -- Specify the servlet Path, package name + class name -->
- <Servlet-class> com. rao. Hello </servlet-class>
- </Servlet>
- <Servlet-mapping>
- <! -- Same as above -->
- <Servlet-name> Hello </servlet-name>
- <! -- The URL used to access the servlet in the browser is arbitrary -->
- <Url-pattern>/Hello </url-pattern>
- </Servlet-mapping>
- <Servlet>
- <! -- Servlet name, which is arbitrary -->
- <Servlet-name> HelloGen </servlet-name>
- <! -- Specify the servlet Path, package name + class name -->
- <Servlet-class> com. rao. HelloGen </servlet-class>
- </Servlet>
- <Servlet-mapping>
- <! -- Same as above -->
- <Servlet-name> HelloGen </servlet-name>
- <! -- The URL used to access the servlet in the browser is arbitrary -->
- <Url-pattern>/HelloGen </url-pattern>
- </Servlet-mapping>
- <Servlet>
- <! -- Servlet name, which is arbitrary -->
- <Servlet-name> HelloHttp </servlet-name>
- <! -- Specify the servlet Path, package name + class name -->
- <Servlet-class> com. rao. HelloHttp </servlet-class>
- </Servlet>
- <Servlet-mapping>
- <! -- Same as above -->
- <Servlet-name> HelloHttp </servlet-name>
- <! -- The URL used to access the servlet in the browser is arbitrary -->
- <Url-pattern>/HelloHttp </url-pattern>
- </Servlet-mapping>
-
- </Web-app>
The Servlet method is developed in three ways !! I hope this will help you implement the Servlet method.
- What are servlets and common Servlet APIs?
- 8-point discussion on optimizing JSP Servlet applications
- Select JSP development tools
- Servlet and JSP paths
- Analysis of form data in JSP Servlet Technology