实现servlet的3种方法_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 实现servlet的3种方法

实现servlet的3种方法

 2014/9/3 18:42:51  xiaoyixiao  程序员俱乐部  我要评论(0)
  • 摘要:Servlet有三种实现方式:1.实现Servlet接口2.继承GenericServlet类3.继承HttpServlet类实现servlet接口代码packagecom.yuchao.servlet;importjava.io.IOException;importjava.io.PrintWriter;importjavax.servlet.Servlet;importjavax.servlet.ServletConfig;importjavax.servlet
  • 标签:方法 实现 Servlet
Servlet有三种实现方式:
1.实现Servlet接口
2.继承GenericServlet类
3.继承HttpServlet类

实现servlet接口代码
class="java" name="code">
package com.yuchao.servlet;  
  
import java.io.IOException;  
import java.io.PrintWriter;  
  
import javax.servlet.Servlet;  
import javax.servlet.ServletConfig;  
import javax.servlet.ServletException;  
import javax.servlet.ServletRequest;  
import javax.servlet.ServletResponse;  
  
public class HelloWorldServlet implements Servlet{  
  
    //释放内存  
    //1. reload  
    //2. 关闭TomCat  
    //3. 关机  
    public void destroy() {  
        // TODO Auto-generated method stub  
        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时,被调用)  
    public void init(ServletConfig arg0) throws ServletException {  
        // TODO Auto-generated method stub  
        System.out.println("init it");  
          
    }  
  
    //用于处理业务逻辑,应该把业务逻辑代码写在该方法中  
    //会被多次调用,当用户每访问一次时就会被调用一次  
    //request用于获取客户端的信息  
    //response用户向客户端返回信息  
    public void service(ServletRequest request, ServletResponse response)  
            throws ServletException, IOException {  
        // TODO Auto-generated method stub  
        System.out.println("service it");  
        PrintWriter printWriter=response.getWriter();  
        printWriter.println("<h1>"+"hello,world"+"</h1>");        
    }  
  
}  



继承genericservlet类代码
package com.yuchao.servlet;  
  
import java.io.IOException;  
import java.io.PrintWriter;  
  
import javax.servlet.GenericServlet;  
import javax.servlet.ServletException;  
import javax.servlet.ServletRequest;  
import javax.servlet.ServletResponse;  
  
@SuppressWarnings("serial")  
public class HelloWorldGenericServlet extends GenericServlet {  
  
    @Override  
    public void service(ServletRequest request, ServletResponse response)  
            throws ServletException, IOException {  
        // TODO Auto-generated method stub  
        try{  
            PrintWriter printWriter=response.getWriter();  
            printWriter.println("<h1>hello,generic world.!!!</h1>");  
        }catch(Exception e){  
            e.printStackTrace();  
        }  
    }  
  
} 



继承httpservlet类代码

package com.yuchao.servlet;  
  
import java.io.IOException;  
import java.io.PrintWriter;  
  
import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
  
@SuppressWarnings("serial")  
public class HelloWorldHttpServlet extends HttpServlet {  
  
    @Override  
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)  
            throws ServletException, IOException {  
        // TODO Auto-generated method stub  
        doPost(req,resp);             
    }  
  
    @Override  
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)  
            throws ServletException, IOException {  
        // TODO Auto-generated method stub        
        PrintWriter printWriter=resp.getWriter();  
        printWriter.println("<h1>Hello World!</h1>");     
    }  
  
} 



使用Servlet需要对其在web.xml中进行配置,例如:
配置信息代码

<!-- HelloWorldServlet映射  -->  
  <servlet>  
    <servlet-name>hello</servlet-name>  
    <servlet-class>com.yuchao.servlet.HelloWorldServlet</servlet-class>  
  </servlet>  
    
  <servlet-mapping>  
    <servlet-name>hello</servlet-name>  
    <url-pattern>/hello</url-pattern>  
  </servlet-mapping> 

上一篇: log4j配置文件详解 下一篇: 没有下一篇了!
发表评论
用户名: 匿名