【Java基础知识】Servlet学习之Servlet API - javax.servlet软件包(四)

发布 : Java培训      来源:Java培训干货资料

2021-01-29 09:52:44

5、HttpServlet类

public abstract class HttpServlet extends GenericServlet

由实现Servlet接口的抽象类GenericServlet派生的抽象子类,用于创建支持HTTP协议的Servlet程序。

针对HTTP1.1协议中定义的7种请求方法GET、POST、HEAD、PUT、DELETE、TRACE、OPTIONS,HttpServlet分别提供了7个处理方法:

doGet(),doPost(),doHead(),doPut(),doDelete(),doTrace(),doOptions() 响应客户请求。

(1)服务方法

protected void service(HttpServletRequest request,

HttpServletResponse response) throws ServletException,

IOException

public void service(ServletRequest request,

ServletResponse response) throws ServletException,

IOException

当Servlet容器接收到客户请求时,调用service方法响应客户请求,service方法在Servlet实例化、初始化之后被调用,并且可以被多次调用。

若在Servlet中重写了service方法,则由该方法处理客户请求;若没有重写service方法,Servlet容器会根据HTTP协议的请求方式,调用不同的请求处理方法响应客户请求。

protected void doGet(HttpServletRequest request,

HttpServletResponse response) throws ServletException,

IOException

处理HTTP的GET请求。

protected void doPost(HttpServletRequest request,

HttpServletResponse response) throws ServletException,

IOException

处理HTTP的POST请求。

protected void doDelete(HttpServletRequest request,

HttpServletResponse response) throws ServletException,

IOException

处理HTTP的DELETE请求。

protected void doHead(HttpServletRequest request,

HttpServletResponse response) throws ServletException,

IOException

处理HTTP的HEAD请求。

protected void doOptions(HttpServletRequest request,

HttpServletResponse response) throws ServletException,

IOException

处理HTTP的OPTIONS请求。

protected void doPut(HttpServletRequest request,

HttpServletResponse response) throws ServletException,

IOException

处理HTTP的PUT请求。

protected void doTrace(HttpServletRequest request,

HttpServletResponse response) throws ServletException,

IOException

处理HTTP的TRACE请求。

(2)获取Servlet相关信息方法

protected long getLastModified(HttpServletRequest request)

获取HttpServletRequest对象的最后修改时间,返回的数值是自1970-1-1日以来的毫秒数。

默认返回一个负数,表示不知道HttpServletRequest对象的最后修改时间。

THE END  

声明:本站稿件版权均属中公教育优就业所有,未经许可不得擅自转载。

领取零基础自学IT资源

涉及方向有Java、Web前端、UI设计、软件测试、python等科目,内容包含学习路线、视频、源码等

点击申请领取资料

点击查看资料详情 

收起 


 相关推荐

问题解答专区
返回顶部