Servlet基本介绍

基本概念

  Servlet又称为Java Servlet是一个基于java技术的web组件,运行在服务器端,用于生成动态的内容。Servlet是平台独立的java类,编写一个Servlet实际上就是按照Servlet规范编写的java类。

  Servlet运行需要一个运行环境,及需要一个Servlet容器,这里我们以tomcat为例,tomcat作为一个web服务器,具有处理HTML页面的功能,另外它还是一个Servlet和jsp容器。

实现方式

  第一种:实现servlet接口,需要实现接口中定义的5个方法

package day_052102;
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;
/*
 这里通过实现Servlet接口来编写一个简单的Servlet
 */
public class ServletDemo implements Servlet
{
    public void init(ServletConfig config) throws ServletException
    {
    }
    public ServletConfig getServletConfig()
    {
        
        return null;
    }
    public String getServletInfo()
    {
        return null;
    }
    public void destroy()
    {
    }
    public void service(ServletRequest req, ServletResponse res)
            throws ServletException, IOException
    {
        PrintWriter out=res.getWriter();
        out.print("hello World!");
        out.close();
    }
}

  第二种:继承抽象类GenericServlet,该类定义了一个通用的Servlet

public abstract class GenericServlet implements Servlet, ServletConfig, java.io.Serializable
public class GenericServletDemo extends GenericServlet
{

    @Override
    public void service(ServletRequest req, ServletResponse res)
            throws ServletException, IOException
    {
        PrintWriter out=res.getWriter();
        out.println("hello World!");
        out.close();
        
    }

}

  第三种:通过继承抽象的HttpServlet类,此类继承于GenericServlet类

HttpServlet中service方法的实现代码:
protected
void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String method = req.getMethod(); if (method.equals(METHOD_GET)) //当请求方式为GET时,调用doGet方法 { long lastModified = getLastModified(req); if (lastModified == -1) { // servlet doesn't support if-modified-since, no reason // to go through further expensive logic doGet(req, resp); } else { long ifModifiedSince; try { ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE); } catch (IllegalArgumentException iae) { // Invalid date header - proceed as if none was set ifModifiedSince = -1; } if (ifModifiedSince < (lastModified / 1000 * 1000)) { // If the servlet mod time is later, call doGet() // Round down to the nearest second for a proper compare // A ifModifiedSince of -1 will always be less maybeSetLastModified(resp, lastModified); doGet(req, resp); } else { resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED); } } }

如果想在自己的类中重写service方法,那么servlet容器就会把请求交给我们自己重写的service方法处理

protected void service(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException 
{
        String method = req.getMethod();
        if (method.equals(METHOD_GET))   //当请求方式为GET时,调用doGet方法
        {   
            long lastModified = getLastModified(req);
            if (lastModified == -1) 
            {
                // servlet doesn't support if-modified-since, no reason
                // to go through further expensive logic
                doGet(req, resp);
            } 
            else
            {
                long ifModifiedSince;
                try 
                {
                    ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE);
                } 
                catch (IllegalArgumentException iae)
                {
                    // Invalid date header - proceed as if none was set
                    ifModifiedSince = -1;
                }
                if (ifModifiedSince < (lastModified / 1000 * 1000))
                {
                    // If the servlet mod time is later, call doGet()
                    // Round down to the nearest second for a proper compare
                    // A ifModifiedSince of -1 will always be less
                    maybeSetLastModified(resp, lastModified);
                    doGet(req, resp);
                }
                else
                {
                    resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
                }
            }
        }

在web‘中的应用

注意事项’

猜你喜欢

转载自www.cnblogs.com/htyj/p/8981613.html