[Servlet] how to compile a Servlet
Creating a Servlet through Eclipse or MyEclipse is simple, but we do not know what the relevant code means. Therefore, we need to study the generated code below.
First, let's look at the source code of the created Servlet File and find the following content:
Is inherited from the HttpServlet class. Including init (), doGet (), doPost (), and destroy () methods.
Based on the above content, we can manually create a Servlet Java file, as shown in the following code:
Public class SecondServlet extends HttpServlet {@ Override public void init () throws ServletException {System. out. println ("this is the init () method... ") ;}@ Override protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System. out. println ("this is the doGet () method... ") ;}@ Override protected void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System. out. println ("this is the doPost () method... ") ;}@ Override public void destroy () {System. out. println ("this is the destroy () method... ");}}
Next, let's take a look at the content added to the web. xml configuration file:
SecondServlet
App. java. servlet. SecondServlet
SecondServlet
/Servlet/SecondServlet
Index. jsp
Publish the current Web application to the Tomcat server and start the Tomcat server to run the test on the Servlet content currently created.
Use Eclipse or MyEclipse to publish the current Web project to the Tomcat server and start the Tomcat server.
Open a browser and enter http: // localhZ success? In the address bar? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> fuse + zvHG97bLtcRTZXJ2bGV0xNrI3aGjDQo8cD48aW1nIGFsdD0 =" here write picture description "src =" http://www.bkjia.com/uploads/allimg/150606/04341V446-1.jpg "title ="/>
View the information of the console of Eclipse or MyEclipse. If the preceding content is displayed, the Servlet is successfully created.
Reprinted description:Please specify the author and original article links. Thank you!