"Java Servlet Programming" notes (servlet lifecycle)

xiaoxiao2021-03-06  76

The servlet lifecycle is highly flexible. The only rules that theservlet container must strictly abide by the following lifecycle agreed: □ Generate and initialize the servlet □ Handle Customer Request □ Uninstall the servlet and use unused unit collection

Corresponding to the above agre, we have the following method: □ init () method: responsible for initializing the servlet object □ service () method: responsible for responding to customer request; □ DESTROY () method: When servlet object exits the life cycle, responsible Release occupancy resource;

An instance, counter instance, initialization file: //initcounter.javaimport java.io. *; Import javax.servlet.http. *;

public class InitCounter extends HttpServlet {int count; public void init () throws ServletException {String initial = getInitParameter ( "initial"); try {count = Integer.parseInt (initial);} catch (NumberFormatException e) {count = 0;} } Public void doget (httpservletRequest Req, httpservletResponse res) throws servletexception, ioException {res.setContentType ("text / html"); PrintWriter out = res. max;

Count ; "Since Loading this Servlet Instance Has Been Accessed Count " Times ");} public void destroy () {super.destroy ();}}

File: //web.xml

couner initcounter initial < Param-value> 1000 Hello / hello

转载请注明原文地址:https://www.9cbs.com/read-121918.html

New Post(0)