Package cn.dragon.servlet; The following is the import of the appropriate package Import java.io.IOException; Import Java.io.PrintWriter; Import javax.servlet.ServletException; Import Javax.servlet.http.HttpServlet; Import Javax.servlet.http.HttpServletRequest; Import Javax.servlet.http.HttpServletRe Sponse; /** * This is the first example of a servlet * @author Cn.dragon */ public class Servletdemofirst extends HttpServlet { Used to process a GET request sent by a client public void doget (HttpServletRequest request, httpservletresponse response) Throws Servletexception, IOException { Response.setcontenttype ("text/html;charset=gb2312"); This statement indicates the format of the content sent to the client and the character encoding used. PrintWriter out = Response.getwriter (); OUT.PRINTLN ("Hello! "); To send data to the client using the method of the PrintWriter object Out.close (); } For handling post requests sent by clients public void DoPost (HttpServletRequest request, httpservletresponse response) Throws Servletexception, IOException { Doget (request, response); The purpose of this statement is to call the Doget () method for processing when the client sends a POST request } } |