The life cycle of the servlet

Source: Internet
Author: User

1. Create a Servlet object, create a Servlet object from the server reflection mechanism, and for the first time,

Created when you request it. Default

2, call the Servlet object's init () method, initialize the servlet's information, and the Init () method will only be called once after it is created;

3, responding to requests, invoking service () or doget (), DoPost () methods to handle requests, these methods are running in multithreaded state.

4, the Destroy () method is called to destroy the Servlet object when it has not been called for a long time or when the server is shut down.

Test Case:

1  PackageCom.damu.servlet;2 3 ImportCom.damu.dao.UsersDAO;4 Importcom.damu.entity.Users;5 6 Importjavax.servlet.ServletException;7 ImportJavax.servlet.annotation.WebServlet;8 ImportJavax.servlet.http.HttpServlet;9 Importjavax.servlet.http.HttpServletRequest;Ten ImportJavax.servlet.http.HttpServletResponse; One Importjava.io.IOException; A Importjava.util.Date; -  -@WebServlet ("/addusers") the  Public classUsersaddservletextendsHttpServlet { -  -     PrivateUsersdao Usersdao =NewUsersdao (); - @Override +     protected voidDoget (httpservletrequest req, HttpServletResponse resp)throwsservletexception, IOException { -          This. DoPost (req, resp); +     } A  at @Override -     protected voidDoPost (httpservletrequest req, HttpServletResponse resp)throwsservletexception, IOException { -         //get the user data to add -String username = req.getparameter ("username"); -String Userpass = Req.getparameter ("Userpass"); -String nickname = Req.getparameter ("nickname"); inString age = req.getparameter (' Age '); -String gender = Req.getparameter ("Gender"); toString email = req.getparameter ("email"); +String phone = req.getparameter ("Phone"); -  the         //Create a User object based on user data *Users user =NewUsers (username, Userpass, nickname, Integer.parseint (age), gender, email, phone,NewDate (),NewDate (),NewDate (), 0); $ Panax Notoginseng         //to add a user object to the database -user =usersdao.adduser (user); the  +         //view newly added user data AResp.sendredirect ("/detail?id=" +User.getid ()); the     } +}

The life cycle of the servlet

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.