Servlet介绍(二)——Servlet中方法介绍

来源:互联网 发布:mac键盘pc机能用吗 编辑:程序博客网 时间:2024/06/07 13:31

一.Servlet中常见方法介绍

Servlet是一个接口,我们先来看一看这个接口中有哪些方法。
这里写图片描述
还记得不记得Servlet的运行流程,WEB容器启动时候会调用init()方法,宕机时候会调用destroy()方法,在有一个请求到来时候,会调用service()方法。

1.1init方法

在web服务器加载这个servlet的时候,会立刻调用init方法, 我们可以看到init方法的参数是ServletConfig 。那这个ServletConfig是啥呐,从字面翻译就是当前这个servlet的配置,没错,init会从web容器中得到这个初始化参数并且赋值给自己的servletconfig,是的,在servlet的实现类中有这个servletconfig属性。

2.destroy方法

与init方法相反的是,在关闭web服务器时候,容器会调用这个方法,如果你还有一些最后要做的那就在这做好了。

3.service方法

当有请求的时候,会调用service方法处理,可以看service的参数是ServletRequest和ServletResponse,分别包含请求数据和响应数据。
记住这个方法是有参数的。
这个方法会抛出ServletException和IoException

二.HttpServlet中方法详解

HTTPServlet可以说是Servlet最常用的一个实现类,用以处理常见的http协议,我再来挂一下HttpServlet中的方法
这里写图片描述
当然嘞,它有个属性叫ServletConfig

2.1service方法(ServletRequest,ServletResponse)

这个方法就是实现的Servlet接口的service方法,也就是web容器会调用的那个service方法,来我们看看这个方法干了啥

public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {        HttpServletRequest request;        HttpServletResponse response;        try {            request = (HttpServletRequest)req;            response = (HttpServletResponse)res;        } catch (ClassCastException var6) {            throw new ServletException("non-HTTP request or response");        }        this.service(request, response);    }

很简单,它把ServletRequest和ServletRespouse对象分别封装成了HttpServletRequest和HttpServletResponse,这样就可以处理http请求了,然后它又干了啥,调用了它的一个重载的service方法。

2.2Service方法(HttpServletRequest,HttpServletResponse)

这个类的方法是protected,啥意思,就是无关的类不能访问到当前这个类,可以在这里实现一些自己想要的功能,并且不能被本包以为的类随意访问,提高了安全性。
有的同学说,我就想在service方法中去实现一些功能,那我应该去重写哪个service方法呐?
实现哪个service方法都能实现功能,但是我们建议实现protected的那个方法, 因此它封装成了http请求和响应,更便于处理,并且对安全性加以提高。
如果不重写的话,我们来看一看这个service方法它又干了些啥?

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

它这又干啥了,这就会到了http请求上了,http的请求方式有很多种,最常见的有get,post,delete,post等等方式,这个方法就是根据请求方式的不同,将请求派发到不同的方法中,当然我们最常见的还是get和post两种方式

2.3post和get

这两个方法是最常见的两种请求方式,所以一般情况下我们只需要重写这个两个函数就行了,不用去修改service方法。

2.4init方法(带参数)

这个init方法实现Servlet接口的方法, 那么参数是什么呐,前面讲过是ServletConfig,这个类接受web容器传过来的配置信息,还记得每个Servlet的实现类里面都有一个属性叫做ServletConfig,我想你也猜到了这个方法的实现。

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

发没发现,它也会调用一个重载的init()方法

2.5init方法(不带参数)

假如现在没有这个方法,你有一些操作要在初始的时候进行,那你就应该去重写init(带参数)的那个方法,但是总有人忘记了调用父类的init方法这不就出错了吗,我猜测Java设计人员为了避免,所以提供了一个空的init方法来供给调用。
所以如果非得有一些初始化行为,那就实现这个没用参数的init方法吧。

原创粉丝点击