1. File > New > Dynamic WEB Project "Javaeedemo" > Create a new Package "Servletdemo" under the Java Resource SRC, add a new class "Myservet" under the package, in addition to Webcontent>web _inf Create a new XML file, this is the servlet configuration file, which will be used later
2, the Myservet class must inherit httpservlet, the sample code is as follows
PackageServletdemo;Importjava.io.IOException;ImportJava.io.PrintWriter;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse; Public classMyservletextendsHttpServlet {@Overrideprotected voiddoget (httpservletrequest req, HttpServletResponse resp)throwsservletexception, IOException {//TODO Auto-generated method stubs//Super.doget (req, resp); //HttpServletRequest Request//HttpServletResponse ResponsePrintWriter pw=Resp.getwriter (); Pw.print ("The first servlet program! "); Pw.close (); } @Overrideprotected voidDoPost (httpservletrequest req, HttpServletResponse resp)throwsservletexception, IOException {//TODO Auto-generated method stubs//super.dopost (req, resp);Doget (req, resp);//typically the POST request is called here by calling the Doget () method above }}
Code
3. Configure the XML file
Learn a servlet (1) The first servlet program