实现servlet的三种方式

1、实现Servlet接口。

2、继承GenericServlet。

3、继承HttpServlet。

第三种方式是现在比较常用的。

第一种:

 

[java] view plain copy
  1. package com.tsinghua;  
  2.   
  3.   
  4. import javax.servlet.*;  
  5.   
  6. import java.io.*;  
  7.   
  8.   
  9. public class Hello implements Servlet{  
  10.   
  11.     /** 
  12.      * 销毁servlet实例,释放内存 
  13.      * 3种情况: 
  14.      * 1.reload 该servlet(所在的webapps) 
  15.      * 2.关闭tomcat 
  16.      * 3.关机 
  17.      */  
  18.     @Override  
  19.     public void destroy() {  
  20.         // TODO Auto-generated method stub  
  21.         System.out.println("destroy it");  
  22.           
  23.           
  24.     }  
  25.   
  26.     @Override  
  27.     public ServletConfig getServletConfig() {  
  28.         // TODO Auto-generated method stub  
  29.         return null;  
  30.     }  
  31.   
  32.     @Override  
  33.     public String getServletInfo() {  
  34.         // TODO Auto-generated method stub  
  35.         return "";  
  36.     }  
  37.   
  38.     /** 
  39.      * 初始化该servlet,类似于构造函数。 
  40.      * 该函数只会被调用一次。当用户第一次访问该servlet时被调用。 
  41.      */  
  42.     @Override  
  43.     public void init(ServletConfig arg0) throws ServletException {  
  44.         // TODO Auto-generated method stub  
  45.         System.out.println("init it");  
  46.     }  
  47.   
  48.       
  49.     /** 
  50.      * 这个函数用于处理业务逻辑 
  51.      * 程序员应该把业务逻辑代码写在这里 
  52.      * 这个函数会被调用多次,当用户每访问该servlet时都会被调用。 
  53.      * req:用来获得客户端的信息 
  54.      * res:用来对客户端返回信息 
  55.      */  
  56.     @Override  
  57.     public void service(ServletRequest req, ServletResponse res)  
  58.             throws ServletException, IOException {  
  59.         // TODO Auto-generated method stub  
  60.           
  61.         System.out.println("service it");  
  62.         //从res中得到PrintWriter  
  63.         PrintWriter pw=res.getWriter();  
  64.         pw.println("hello,world");  
  65.           
  66.           
  67.           
  68.     }  
  69.       
  70. }  


第二种:

 

 

[java] view plain copy
  1. package com.tsinghua;  
  2.  
  3.   
  4. import javax.servlet.GenericServlet;  
  5. import javax.servlet.ServletException;  
  6. import javax.servlet.ServletRequest;  
  7. import javax.servlet.*;  
  8.   
  9. import java.io.*;  
  10.   
  11.   
  12. public class HelloGen extends GenericServlet{  
  13.   
  14.     /** 
  15.      * 重写service方法即可。 
  16.      */  
  17.     @Override  
  18.     public void service(ServletRequest req, ServletResponse res)  
  19.             throws ServletException, IOException {  
  20.         // TODO Auto-generated method stub  
  21.         //返回HelloWorld!generic  
  22.         PrintWriter pw=res.getWriter();  
  23.         pw.println("HelloWorld!generic");  
  24.           
  25.           
  26.           
  27.     }  
  28.   
  29.       
  30.       
  31. }  


第三种:

 

[java] view plain copy
  1. package com.tsinghua;  
  2.   
  3.   
  4. import javax.servlet.http.HttpServlet;  
  5. import javax.servlet.http.HttpServletRequest;  
  6. import javax.servlet.http.HttpServletResponse;  
  7.   
  8. import java.io.*;  
  9.   
  10.   
  11. public class HelloHttp  extends HttpServlet{  
  12.       
  13.     /** 
  14.      * 处理get请求 
  15.      * req:客户端的请求信息 
  16.      * resp:客户端的返回信息 
  17.      */  
  18.     public void doGet(HttpServletRequest req, HttpServletResponse resp)  
  19.     {  
  20.         //业务逻辑在这里写。  
  21.         try {  
  22.             PrintWriter pw=resp.getWriter();  
  23.             pw.println("Hello,Http");  
  24.         } catch (IOException e) {  
  25.             // TODO Auto-generated catch block  
  26.             e.printStackTrace();  
  27.         }  
  28.           
  29.     }  
  30.       
  31.     /** 
  32.      * 处理post请求 
  33.      * req:客户端的请求信息 
  34.      * resp:客户端的返回信息 
  35.      */  
  36.     public void doPost(HttpServletRequest req, HttpServletResponse resp)  
  37.     {  
  38.         //post和get请求一样  
  39.         this.doGet(req, resp);  
  40.     }  
  41.   
  42. }  



 

servlet的配置:

 

[html] view plain copy
  1. <?xml version="1.0" ?>  
  2. <!--  
  3.  Licensed to the Apache Software Foundation (ASF) under one or more  
  4.   contributor license agreements.  See the NOTICE file distributed with  
  5.   this work for additional information regarding copyright ownership.  
  6.   The ASF licenses this file to You under the Apache License, Version 2.0  
  7.   (the "License"); you may not use this file except in compliance with  
  8.   the License.  You may obtain a copy of the License at  
  9.   
  10.       http://www.apache.org/licenses/LICENSE-2.0  
  11.   
  12.   Unless required by applicable law or agreed to in writing, software  
  13.   distributed under the License is distributed on an "AS IS" BASIS,  
  14.   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  15.   See the License for the specific language governing permissions and  
  16.   limitations under the License.  
  17. -->  
  18.   
  19. <web-app xmlns="http://java.sun.com/xml/ns/javaee"  
  20.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  21.    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  22.    version="2.5">  
  23.   
  24.   <display-name>Welcome to Tomcat</display-name>  
  25.   <description>  
  26.      Welcome to Tomcat  
  27.   </description>  
  28.     
  29.     
  30.   <!-- servlet的配置 -->  
  31.   <servlet>  
  32.     <servlet-name>hello</servlet-name><!-- servlet的名字,任意的 -->  
  33.     <servlet-class>com.tsinghua.Hello</servlet-class><!-- 指明servlet的包名+类名 -->  
  34.   </servlet>  
  35.   <!-- 映射关系配置 -->  
  36.   <servlet-mapping>  
  37.     <servlet-name>hello</servlet-name><!-- servlet的名字,和上面一样 -->  
  38.     <url-pattern>/hello</url-pattern><!-- 这是在浏览器中输入的访问该servlet的url -->  
  39.   </servlet-mapping>  
  40.     
  41.   <!-- servlet的配置 -->  
  42.   <servlet>  
  43.     <servlet-name>helloGen</servlet-name><!-- servlet的名字,任意的 -->  
  44.     <servlet-class>com.tsinghua.HelloGen</servlet-class><!-- 指明servlet的包名+类名 -->  
  45.   </servlet>  
  46.   <!-- 映射关系配置 -->  
  47.   <servlet-mapping>  
  48.     <servlet-name>helloGen</servlet-name><!-- servlet的名字,和上面一样 -->  
  49.     <url-pattern>/helloGen</url-pattern><!-- 这是在浏览器中输入的访问该servlet的url -->  
  50.   </servlet-mapping>  
  51.     
  52.   <!-- servlet的配置 -->  
  53.   <servlet>  
  54.     <servlet-name>helloHttp</servlet-name><!-- servlet的名字,任意的 -->  
  55.     <servlet-class>com.tsinghua.HelloHttp</servlet-class><!-- 指明servlet的包名+类名 -->  
  56.   </servlet>  
  57.   <!-- 映射关系配置 -->  
  58.   <servlet-mapping>  
  59.     <servlet-name>helloHttp</servlet-name><!-- servlet的名字,和上面一样 -->  
  60.     <url-pattern>/helloHttp</url-pattern><!-- 这是在浏览器中输入的访问该servlet的url -->  
  61.   </servlet-mapping>  
  62.     
  63.     
  64.     
  65.   
  66. </web-app>  
原文地址:https://www.cnblogs.com/xiaobaizhang/p/8117673.html