Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Servlet method implementation

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shares with you the content of a sample analysis of the Servlet method implementation. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

The Servlet method implements * development by implementing the servlet interface.

/ * * / / * @ (#) Hello.java * @ author shiyi05 * @ version 1.00 2008-5-9 * / / this is my * servlet, using implements Servlet to implement the servlet interface to develop 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 *) 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 the information of the client * @ param res is used to return information to the client * @ throws ServletException @ throws IOException * * / / this function is used to process the business logic every time the user accesses the servlet Will be called public void service (ServletRequest req, ServletResponse res) throws ServletException, IOException. {/ / TODO: Add your code here System.out.println ("service") PrintWriter pw=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");}}

The Servlet method implements the second development by inheriting GenericServlet

/ * * / / * * @ (#) HelloGen.java * @ author shiyi05 * @ version 1.00 2008-5-9 * / / this is the second way to develop servlet by inheriting GenericServlet to develop package com.rao; import javax.servlet.GenericServlet; import java.io.*; import javax.servlet.* Public class HelloGen extends GenericServlet. {public HelloGen (). {} / / override the service () method public void service (ServletRequest req,ServletResponse res). {try... {PrintWriter pw=res.getWriter (); pw.println ("Hello World,Generic") } catch (Exception ex)... {ex.printStackTrace ();}

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

/ * * @ (#) HelloHttp.java * @ author shiyi05 * @ version 1.00 2008-5-9 * / / this is the third way to develop servlet, which implements package com.rao; import javax.servlet.http.*; import java.io.* by inheriting the HttpServlet class Public class HelloHttp extends HttpServlet... {public HelloHttp ()... {} / / processing Get requests public void doGet (HttpServletRequest req,HttpServletResponse res)... {try... {PrintWriter pw=res.getWriter (); pw.println ("Hello World,HttpServlet") } catch (Exception ex)... {ex.printStackTrace ();}} public void doPost (HttpServletRequest req,HttpServletResponse res)... {this.doGet (req,res);}}

* deploy these three servlet in web.xml file

< 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_2_5.xsd" version=" 2.5 " > < display-name > Welcome to Tomcat < / display-name > < description > Welcome to Tomcat < / description > < servlet >

Share To

Development

© 2024 shulou.com SLNews company. All rights reserved.

12
Report