servlet的三种开发方法


  servlet的三种开发方法:
  *************************1(import java.servlet.*;******************************
  
          public class Hello  implements Servlet {
            /*销毁Servlet实例(释放内存),调用它的三种情况:1,reload 该Servlet(webApp)
             * 2,关闭tomcat,3,关机
            */
            public void destroy() {
                
                System.out.println("destroy it");
                
            }

            public ServletConfig getServletConfig() {
                // TODO Auto-generated method stub
                return  null;
            }

            public String getServletInfo() {
                // TODO Auto-generated method stub
                return null;
            }
            /*
             该函数用户初始化Servlet,类似于java中类的构造函数,只会被调用一次(当用户第一次访问该Servlet
             的时候被调用
             */
            public void init(ServletConfig arg0) throws ServletException {
                // TODO Auto-generated method stub
                System.out.println("init");
            }
            /*这个函数用户处理业务逻辑,程序员把业务逻辑代码写在这里
             * 当用户访问一次该Servlet的时候都会被调用
             req用于获得客户端(浏览器)的信息,而res则相反,向客户端(浏览器)返回信息
             */
            public void service(ServletRequest req, ServletResponse res)
                    throws ServletException, IOException {
                System.out.println(" service it");
                //从res中得到Printwriter
                PrintWriter pw=res.getWriter();
                pw.println("hello,world");    
                
            }

        }
        ********************************************2(import java.servlet.GenericServlet.*)****************
        只需重写GenericServlet的service方法

        public  class test_GenericServlet extends GenericServlet{

            @Override
            public void service(ServletRequest req, ServletResponse res)
                    throws ServletException, IOException {
                try{
                    PrintWriter pw=res.getWriter();
                    pw.println("hello,world,这是利用service");
                }
                catch(Exception ex){
                    ex.printStackTrace();
                }
                
                
            }

            
        }
        *************3,HttpServlet***************
        import javac.service.http.*;
            [import javax.servlet.http.HttpServlet;
             import javax.servlet.http.HttpServletRequest;
             import javax.servlet.http.HttpServletResponse;]

        public class test_httpServlet extends HttpServlet {

            /*处理get请求,resquest:用户获得客户端(浏览器)的请求
             * response:用户向客户端(浏览器)返回信息*/
            public void doGet(HttpServletRequest request, HttpServletResponse response)
                    throws ServletException, IOException {
                //业务逻辑,也可以和doPost的使用调过来
                
            }

            
            public void doPost(HttpServletRequest request, HttpServletResponse response)
                    throws ServletException, IOException {

                this.doGet(request, response);
            }

        }

 

posted @ 2016-03-18 22:26  戒。  阅读(131)  评论(0编辑  收藏  举报