您的位置:首页 > 其它

关于servlet生命周期中的方法

2013-07-31 13:57 381 查看
关于servlet生命周期中的方法

public class TestServlet extends HttpServlet {

    @Override

    protected void doGet(

            HttpServletRequest req,

            HttpServletResponse resp)

            throws ServletException, IOException {

        // TODO Auto-generated method stub

        //super.doGet(req, resp);

        System.out.println("进入了doGet方法");

        resp.setContentType("text/html;charset=gbk");

        PrintWriter out = resp.getWriter();

        out.write("<h1><font color=red>欢迎进入doGet方法!</font></h1>");

        out.flush();

        out.close();        

    }

    @Override

    protected void doPost(HttpServletRequest req, HttpServletResponse resp)

            throws ServletException, IOException {

        // TODO Auto-generated method stub

        //super.doPost(req, resp);

        System.out.println("进入了doPost方法");

        resp.setContentType("text/html;charset=gbk");

        PrintWriter out = resp.getWriter();

        out.write("<h1><font color=blue>欢迎进入doPost方法!</font></h1>");

        out.flush();

        out.close();

    }

    @Override

    public void destroy() {

        // TODO Auto-generated method stub

        //super.destroy();

        System.out.println("进入了destory方法");

    }

    @Override

    public void init(ServletConfig config) throws ServletException {

        // TODO Auto-generated method stub

        //super.init(config);

        System.out.println("进入了init方法");

        String val1 = config.getInitParameter("name1");

        String val2 = config.getInitParameter("name2");

        System.out.println("初始化的参数值是:name1="+val1+"  name2="+val2);

    }

}

在页面提交表单后 init方法和doPost方法都会执行,destroy方法需要你在tomcat中关闭时,才执行的方法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  servlet