[Servlet] how to compile a Servlet

Source: Internet
Author: User

[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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.