Servlet

Servlet 全称为server applet是运行在 Web 服务器或应用服务器上的程序,它是作为来自 Web 浏览器或其他 HTTP 客户端的请求和 HTTP 服务器上的数据库或应用程序之间的中间层。
Servlet 执行以下主要任务:
1、读取客户端(浏览器)发送的显式的数据。这包括网页上的 HTML 表单,或者也可以是来自 applet 或自定义的 HTTP 客户端程序的表单。
2、读取客户端(浏览器)发送的隐式的 HTTP 请求数据。这包括 cookies、媒体类型和浏览器能理解的压缩格式等等。
3、处理数据并生成结果。这个过程可能需要访问数据库,调用 Web 服务,或者直接计算得出对应的响应。
4、发送显式的数据(即文档)到客户端(浏览器)。该文档的格式可以是多种多样的,包括文本文件(HTML 或 XML)、二进制文件(GIF 图像)、Excel 等。
5、发送隐式的 HTTP 响应到客户端(浏览器)。这包括告诉浏览器或其他客户端被返回的文档类型(例如 HTML),设置 cookies 和缓存参数,以及其他类似的任务。
Servlet包
Java Servlet 是运行在带有支持 Java Servlet 规范的解释器的 web 服务器(Servlet类无主方法,由web服务器执行)上的 Java 类。
Servlet 三种方式创建。
1、实现 Servlet 接口
Servlet接口有四个抽象方法

public interface Servlet
{

    public abstract void init(ServletConfig servletconfig)
        throws ServletException;//初始化方法

    public abstract ServletConfig getServletConfig();//Servlet获取配置

    public abstract void service(ServletRequest servletrequest, ServletResponse servletresponse)
        throws ServletException, IOException;//服务方法

    public abstract String getServletInfo();//获取Servlet信息

    public abstract void destroy();//销毁Servlet
}


2、继承 GenericServlet 类
GenericServlet类实现了Servlet接口,并且为除了Service方法以外的所有抽象方法做了空实现,因此只需实现Service方法即可
部分源码如下:

public abstract class GenericServlet
    implements Servlet, ServletConfig, Serializable
{
   public void destroy()
    {
    }
    public String getServletInfo()
    {
        return "";
    }

    public void init(ServletConfig config)
        throws ServletException
    {
        this.config = config;
        init();
    }

    public void init()
        throws ServletException
    {
    }
    
    public abstract void service(ServletRequest servletrequest, ServletResponse servletresponse)
        throws ServletException, IOException;
}

3、继承 HttpServlet
部分源码如下:

public abstract class HttpServlet extends GenericServlet
{


    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException
    {
        String protocol = req.getProtocol();
        String msg = lStrings.getString("http.method_get_not_supported");
        if(protocol.endsWith("1.1"))
            resp.sendError(405, msg);
        else
            resp.sendError(400, msg);
    }

    protected void doHead(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException
    {
        if(DispatcherType.INCLUDE.equals(req.getDispatcherType()))
        {
            doGet(req, resp);
        } else
        {
            NoBodyResponse response = new NoBodyResponse(resp);
            doGet(req, response);
            response.setContentLength();
        }
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException
    {
        String protocol = req.getProtocol();
        String msg = lStrings.getString("http.method_post_not_supported");
        if(protocol.endsWith("1.1"))
            resp.sendError(405, msg);
        else
            resp.sendError(400, msg);
    }

    protected void doPut(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException
    {
        String protocol = req.getProtocol();
        String msg = lStrings.getString("http.method_put_not_supported");
        if(protocol.endsWith("1.1"))
            resp.sendError(405, msg);
        else
            resp.sendError(400, msg);
    }

    protected void doDelete(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException
    {
        String protocol = req.getProtocol();
        String msg = lStrings.getString("http.method_delete_not_supported");
        if(protocol.endsWith("1.1"))
            resp.sendError(405, msg);
        else
            resp.sendError(400, msg);
    }

    protected void doOptions(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException
    {
        Method methods[] = getAllDeclaredMethods(getClass());
        boolean ALLOW_GET = false;
        boolean ALLOW_HEAD = false;
        boolean ALLOW_POST = false;
        boolean ALLOW_PUT = false;
        boolean ALLOW_DELETE = false;
        boolean ALLOW_TRACE = true;
        boolean ALLOW_OPTIONS = true;
        for(int i = 0; i < methods.length; i++)
        {
            Method m = methods[i];
            if(m.getName().equals("doGet"))
            {
                ALLOW_GET = true;
                ALLOW_HEAD = true;
            }
            if(m.getName().equals("doPost"))
                ALLOW_POST = true;
            if(m.getName().equals("doPut"))
                ALLOW_PUT = true;
            if(m.getName().equals("doDelete"))
                ALLOW_DELETE = true;
        }

        String allow = null;
        if(ALLOW_GET)
            allow = "GET";
        if(ALLOW_HEAD)
            if(allow == null)
                allow = "HEAD";
            else
                allow = (new StringBuilder()).append(allow).append(", HEAD").toString();
        if(ALLOW_POST)
            if(allow == null)
                allow = "POST";
            else
                allow = (new StringBuilder()).append(allow).append(", POST").toString();
        if(ALLOW_PUT)
            if(allow == null)
                allow = "PUT";
            else
                allow = (new StringBuilder()).append(allow).append(", PUT").toString();
        if(ALLOW_DELETE)
            if(allow == null)
                allow = "DELETE";
            else
                allow = (new StringBuilder()).append(allow).append(", DELETE").toString();
        if(ALLOW_TRACE)
            if(allow == null)
                allow = "TRACE";
            else
                allow = (new StringBuilder()).append(allow).append(", TRACE").toString();
        if(ALLOW_OPTIONS)
            if(allow == null)
                allow = "OPTIONS";
            else
                allow = (new StringBuilder()).append(allow).append(", OPTIONS").toString();
        resp.setHeader("Allow", allow);
    }

    protected void doTrace(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException
    {
        String CRLF = "\r\n";
        StringBuilder buffer = (new StringBuilder("TRACE ")).append(req.getRequestURI()).append(" ").append(req.getProtocol());
        String headerName;
        for(Enumeration reqHeaderEnum = req.getHeaderNames(); reqHeaderEnum.hasMoreElements(); buffer.append(CRLF).append(headerName).append(": ").append(req.getHeader(headerName)))
            headerName = (String)reqHeaderEnum.nextElement();

        buffer.append(CRLF);
        int responseLength = buffer.length();
        resp.setContentType("message/http");
        resp.setContentLength(responseLength);
        ServletOutputStream out = resp.getOutputStream();
        out.print(buffer.toString());
        out.close();
    }

    protected void service(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException
    {
        String method = req.getMethod();
        if(method.equals("GET"))
        {
            long lastModified = getLastModified(req);
            if(lastModified == -1L)
            {
                doGet(req, resp);
            } else
            {
                long ifModifiedSince;
                try
                {
                    ifModifiedSince = req.getDateHeader("If-Modified-Since");
                }
                catch(IllegalArgumentException iae)
                {
                    ifModifiedSince = -1L;
                }
                if(ifModifiedSince < (lastModified / 1000L) * 1000L)
                {
                    maybeSetLastModified(resp, lastModified);
                    doGet(req, resp);
                } else
                {
                    resp.setStatus(304);
                }
            }
        } else
        if(method.equals("HEAD"))
        {
            long lastModified = getLastModified(req);
            maybeSetLastModified(resp, lastModified);
            doHead(req, resp);
        } else
        if(method.equals("POST"))
            doPost(req, resp);
        else
        if(method.equals("PUT"))
            doPut(req, resp);
        else
        if(method.equals("DELETE"))
            doDelete(req, resp);
        else
        if(method.equals("OPTIONS"))
            doOptions(req, resp);
        else
        if(method.equals("TRACE"))
        {
            doTrace(req, resp);
        } else
        {
            String errMsg = lStrings.getString("http.method_not_implemented");
            Object errArgs[] = new Object[1];
            errArgs[0] = method;
            errMsg = MessageFormat.format(errMsg, errArgs);
            resp.sendError(501, errMsg);
        }
    }
    
    public void service(ServletRequest req, ServletResponse res)
        throws ServletException, IOException
    {
        HttpServletRequest request;
        HttpServletResponse response;
        try
        {
            request = (HttpServletRequest)req;
            response = (HttpServletResponse)res;
        }
        catch(ClassCastException e)
        {
            throw new ServletException("non-HTTP request or response");
        }
        service(request, response);
    }
}

Servlet生命周期

  • 1、实例化(使用构造方法创建对象)
  • 2、初始化 执行init方法,当Servlet第一次被创建对象时执行该方法,该方法在整个生命周期中只执行一次。对象默认创建于用户第一次调用对应于该 Servlet 的 URL 时,也可以指定 Servlet 在服务器第一次启动时被加载,可在web.xml中配置。init() 方法简单地创建或加载一些数据,这些数据将被用于 Servlet 的整个生命周期。
  • 3、服务 执行service方法,客户端响应的方法,该方法会被执行多次,每次请求该servlet都会执行该方法。service() 方法是执行实际任务的主要方法。Servlet 容器(即 Web 服务器)调用 service() 方法来处理来自客户端(浏览器)的请求,并把格式化的响应写回给客户端。每次服务器接收到一个 Servlet 请求时,服务器会产生一个新的线程并调用服务。service() 方法检查 HTTP 请求类型(GET、POST、PUT、DELETE 等),并在适当的时候调用 doGet、doPost、doPut,doDelete 等方法。
  • 4、销毁 执行destroy方法,当Servlet被销毁时执行该方法(当停止tomcat时也就销毁的servlet)。destroy() 方法可以让Servlet 关闭数据库连接、停止后台线程、把 Cookie 列表或点击计数器写入到磁盘,并执行其他类似的清理活动。在调用 destroy() 方法之后,servlet 对象被标记为垃圾回收
    其他方法
    doGet() 方法
    GET 请求来自于一个 URL 的正常请求,或者来自于一个未指定 METHOD 的 HTML 表单,它由 doGet() 方法处理。
    doPost() 方法
    POST 请求来自于一个特别指定了 METHOD 为 POST 的 HTML 表单,它由 doPost() 方法处理。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值