您的位置:首页 > 其它

三个写servlet方法的实例

2008-05-09 04:45 309 查看
第一个通过实现servlet接口的方式来开发

 




/** *//**


 * @(#)Hello.java


 *


 *


 * @author   shiyi05


 * @version 1.00 2008/5/9


 */






//这是我的第一个servlet,使用 implements Servlet实现servlet接口的方式来开发


package com.rao;


import javax.servlet.*;


import javax.servlet.ServletConfig;


import javax.servlet.ServletException;


import javax.servlet.ServletRequest;


import javax.servlet.ServletResponse;


import java.io.IOException;


import javax.servlet.*;


import java.io.*;








public class Hello implements Servlet




...{




    public Hello() 




    ...{


    }


    


    




        /** *//**


     * Method init


     *


     *


     * @param parm1


     *


     @throws ServletException


     *


     */


    //该函数用于初始化该servlet,该函数只会被调用一次(当用户第一次访问该servlet时)


    public void init(ServletConfig parm1) throws ServletException




                    ...{


        // TODO: Add your code here


        System.out.println("init");


    }






    /** *//**


     * Method getServletConfig


     *


     *


     * @return


     *


     */


    public ServletConfig getServletConfig()




    ...{


        // TODO: Add your code here 


        


        return null;


    }






    /** *//**


     * Method service


     *


     *


     * @param req 用于获得客户端的信息


     * @param res 用于向客户端返回信息


     *


     @throws ServletException


     @throws IOException


     *


     */


    //该函数用于处理业务逻辑,当用户每访问该servlet时,都会被调用    


    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException




                    ...{


        // TODO: Add your code here


        System.out.println("service");


        PrintWriter pw=res.getWriter();


        pw.println("Hello world!");


    }






    /** *//**


     * Method getServletInfo


     *


     *


     * @return


     *


     */


    public String getServletInfo() 




    ...{


        // TODO: Add your code here


        return "";


    }






    /** *//**


     * Method destroy


     *


     *


     */


    public void destroy() 




    ...{


        // TODO: Add your code here


        System.out.println("destroy");


    }


    


}

 

第二个通过继承GenericServlet开发

 




/** *//**


 * @(#)HelloGen.java


 *


 *


 * @author   shiyi05


 * @version 1.00 2008/5/9


 */






//这是第二种开发servlet的方法,是通过继承GenericServlet开发


package com.rao;


import javax.servlet.GenericServlet;


import java.io.*;


import javax.servlet.*;






public class HelloGen extends GenericServlet




...{




    public HelloGen()




    ...{


    }


    


    //重写service()方法


    public void service(ServletRequest req,ServletResponse res)




    ...{


           try




           ...{


                       PrintWriter pw=res.getWriter();


                       pw.println("Hello World,Generic");


           }


           catch(Exception ex)




           ...{


                   ex.printStackTrace();


           }    


           


           


    }


    


    


}

 

第三种,也是常见的一种,通过继承HttpServlet类实现




/** *//**


 * @(#)HelloHttp.java


 *


 *


 * @author   shiyi05


 * @version 1.00 2008/5/9


 */






//这是第三种开发servlet的方法,是通过继承HttpServlet类实现


package com.rao;


import javax.servlet.http.*;


import java.io.*;


public class HelloHttp extends HttpServlet




 ...{




    public HelloHttp() 




    ...{


    }


    


    //处理Get请求


    public void doGet(HttpServletRequest req,HttpServletResponse res)




    ...{


           try




           ...{


                PrintWriter pw=res.getWriter();


                               pw.println("Hello World,HttpServlet");


           }


           catch(Exception ex)




           ...{


                   ex.printStackTrace();


           }


        


    }


    


    public void doPost(HttpServletRequest req,HttpServletResponse res)




    ...{


        this.doGet(req,res);


        


    }


    


    


}

 最后把这三个servlet在web.xml文件中部署

 


<?xml version="1.0" encoding="ISO-8859-1"?>


<!--


 Licensed to the Apache Software Foundation (ASF) under one or more


  contributor license agreements.  See the NOTICE file distributed with


  this work for additional information regarding copyright ownership.


  The ASF licenses this file to You under the Apache License, Version 2.0


  (the "License"); you may not use this file except in compliance with


  the License.  You may obtain a copy of the License at




      http://www.apache.org/licenses/LICENSE-2.0




  Unless required by applicable law or agreed to in writing, software


  distributed under the License is distributed on an "AS IS" BASIS,


  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.


  See the License for the specific language governing permissions and


  limitations under the License.


-->




<web-app xmlns="http://java.sun.com/xml/ns/javaee"


   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"


   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"


   version="2.5">




  <display-name>Welcome to Tomcat</display-name>


  <description>


     Welcome to Tomcat


  </description>


  


      <servlet>


          <!--servlet取名,是任意的-->


        <servlet-name>Hello</servlet-name>


        <!--指明servlet路径,包名+类名-->


        <servlet-class>com.rao.Hello</servlet-class>


    </servlet>


        <servlet-mapping>


        <!--跟上面一样-->


        <servlet-name>Hello</servlet-name>


        <!--浏览器中访问该servlet的URL,是任意的-->


        <url-pattern>/Hello</url-pattern>


    </servlet-mapping>


    


      


      <servlet>


          <!--servlet取名,是任意的-->


        <servlet-name>HelloGen</servlet-name>


        <!--指明servlet路径,包名+类名-->


        <servlet-class>com.rao.HelloGen</servlet-class>


    </servlet>


        <servlet-mapping>


        <!--跟上面一样-->


        <servlet-name>HelloGen</servlet-name>


        <!--浏览器中访问该servlet的URL,是任意的-->


        <url-pattern>/HelloGen</url-pattern>


    </servlet-mapping>


    


          <servlet>


          <!--servlet取名,是任意的-->


        <servlet-name>HelloHttp</servlet-name>


        <!--指明servlet路径,包名+类名-->


        <servlet-class>com.rao.HelloHttp</servlet-class>


    </servlet>


        <servlet-mapping>


        <!--跟上面一样-->


        <servlet-name>HelloHttp</servlet-name>


        <!--浏览器中访问该servlet的URL,是任意的-->


        <url-pattern>/HelloHttp</url-pattern>


    </servlet-mapping>




</web-app>



 

这样,使用三种方法都开发完成!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息