servlet三种方式实现servlet接口
servlet三种方式实现servlet接口
简单介绍
Servlet接口实现类
1、Servlet接口SUN公司定义了两个默认实现类,分别为:GenericServlet、HttpServlet。
2、HttpServlet指能够处理HTTP请求的servlet,它在原有Servlet接口上添加了一些与HTTP协议处理方法,它比Servlet接口的功能更为强大。因此开发人员在编写Servlet时,通常应继承这个类,而避免直接去实现Servlet接口。
3、HttpServlet在实现Servlet接口时,覆写了service方法,该方法体内的代码会自动判断用户的请求方式,如为GET请求,则调用HttpServlet的doGet方法,如为Post请求,则调用doPost方法。因此,开发人员在编写Servlet时,通常只需要覆写doGet或doPost方法,而不要去覆写service方法。
接口分离原则:
将一个包含多功能的单一接口根据函数功能分离称多个独立的接口,同时每个接口都有自己特有的、有意义的、具有实际哦那功能的修饰器。
多重接口修饰:
每个修饰器设计时都需要首先实现他要修饰的接口,可以配合应用 适配器和修饰器这两个模式来构建多重修饰器,同时也能保证代码量最少。
客户端构建:
为客户端提供接口实例的方式一定程度上取决于接口实现的数目。如果每个接口都有自己特有的实现,那就需要构造所有实现的实例并提供给客户端。或者如果所有的接口的实现都包含在单个类中,那么只需要构建该类的实例就能够满足客户端的所有依赖。
1、多接口、多实例:
控制器类:将所有方法整合到控制器类中(构造函数传入参数为各个接口)。控制器类中的每个方法都需要一个不同的接口来执行他的功能。
2、单接口、单实例:
在单个类中继承并实现所有接口。这样将每个接口的实现方法都整合到一个类中。(一般用于叶子实现类)
Servlet的作用:
1.接收请求数据
2.处理请求
3.完成响应
Servlet接口中的五大方法:
其中有三个是生命周期方法:
1.void init(ServletConfig).当Servlet对象被服务器创建时调用,仅调用一次。
2.void service(ServletRequest, ServletResponse).可被服务器调用多次。
3.void destroy().服务器关闭连接之前调用,仅一次。
另外的两个:
4.ServletConfig getServletConfig()
5.String getServletInfo()
Servlet代码源
package javax.servlet;import java.io.IOException;public interface Servlet {void init(ServletConfig var1) throws ServletException;ServletConfig getServletConfig();void service(ServletRequest var1, ServletResponse var2) throws ServletException, IOException;String getServletInfo();void destroy(); }
View Code
HttpServlet代码源
package javax.servlet.http;import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.text.MessageFormat; import java.util.Enumeration; import java.util.ResourceBundle; import javax.servlet.DispatcherType; import javax.servlet.GenericServlet; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse;public abstract class HttpServlet extends GenericServlet {private static final long serialVersionUID = 1L;private static final String METHOD_DELETE = "DELETE";private static final String METHOD_HEAD = "HEAD";private static final String METHOD_GET = "GET";private static final String METHOD_OPTIONS = "OPTIONS";private static final String METHOD_POST = "POST";private static final String METHOD_PUT = "PUT";private static final String METHOD_TRACE = "TRACE";private static final String HEADER_IFMODSINCE = "If-Modified-Since";private static final String HEADER_LASTMOD = "Last-Modified";private static final String LSTRING_FILE = "javax.servlet.http.LocalStrings";private static final ResourceBundle lStrings = ResourceBundle.getBundle("javax.servlet.http.LocalStrings");public HttpServlet() {}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 long getLastModified(HttpServletRequest req) {return -1L;}protected void doHead(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {if (DispatcherType.INCLUDE.equals(req.getDispatcherType())) {this.doGet(req, resp);} else {NoBodyResponse response = new NoBodyResponse(resp);this.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);}}private static Method[] getAllDeclaredMethods(Class<?> c) {if (c.equals(HttpServlet.class)) {return null;} else {Method[] parentMethods = getAllDeclaredMethods(c.getSuperclass());Method[] thisMethods = c.getDeclaredMethods();if (parentMethods != null && parentMethods.length > 0) {Method[] allMethods = new Method[parentMethods.length + thisMethods.length];System.arraycopy(parentMethods, 0, allMethods, 0, parentMethods.length);System.arraycopy(thisMethods, 0, allMethods, parentMethods.length, thisMethods.length);thisMethods = allMethods;}return thisMethods;}}protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {Method[] methods = getAllDeclaredMethods(this.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;Class clazz = null;try {clazz = Class.forName("org.apache.catalina.connector.RequestFacade");Method getAllowTrace = clazz.getMethod("getAllowTrace", (Class[])null);ALLOW_TRACE = (Boolean)getAllowTrace.invoke(req, (Object[])null);} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | ClassNotFoundException var14) {;}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 = allow + ", HEAD";}}if (ALLOW_POST) {if (allow == null) {allow = "POST";} else {allow = allow + ", POST";}}if (ALLOW_PUT) {if (allow == null) {allow = "PUT";} else {allow = allow + ", PUT";}}if (ALLOW_DELETE) {if (allow == null) {allow = "DELETE";} else {allow = allow + ", DELETE";}}if (ALLOW_TRACE) {if (allow == null) {allow = "TRACE";} else {allow = allow + ", TRACE";}}if (ALLOW_OPTIONS) {if (allow == null) {allow = "OPTIONS";} else {allow = allow + ", OPTIONS";}}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());Enumeration reqHeaderEnum = req.getHeaderNames();while(reqHeaderEnum.hasMoreElements()) {String headerName = (String)reqHeaderEnum.nextElement();buffer.append(CRLF).append(headerName).append(": ").append(req.getHeader(headerName));}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();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);}}private void maybeSetLastModified(HttpServletResponse resp, long lastModified) {if (!resp.containsHeader("Last-Modified")) {if (lastModified >= 0L) {resp.setDateHeader("Last-Modified", lastModified);}}}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);} }
View Code
GenericServlet代码源
package javax.servlet;import java.io.IOException; import java.io.Serializable; import java.util.Enumeration;public abstract class GenericServlet implements Servlet, ServletConfig, Serializable {private static final long serialVersionUID = 1L;private transient ServletConfig config;public GenericServlet() {}public void destroy() {}public String getInitParameter(String name) {return this.getServletConfig().getInitParameter(name);}public Enumeration<String> getInitParameterNames() {return this.getServletConfig().getInitParameterNames();}public ServletConfig getServletConfig() {return this.config;}public ServletContext getServletContext() {return this.getServletConfig().getServletContext();}public String getServletInfo() {return "";}public void init(ServletConfig config) throws ServletException {this.config = config;this.init();}public void init() throws ServletException {}public void log(String msg) {this.getServletContext().log(this.getServletName() + ": " + msg);}public void log(String message, Throwable t) {this.getServletContext().log(this.getServletName() + ": " + message, t);}public abstract void service(ServletRequest var1, ServletResponse var2) throws ServletException, IOException;public String getServletName() {return this.config.getServletName();} }
View Code
实现接口代码:
package com.wbg;import javax.servlet.*; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException;@WebServlet("/MyServlet") public class MyServlet implements Servlet {@Overridepublic void init(ServletConfig servletConfig) throws ServletException {System.out.println("初始化servlet");}@Overridepublic ServletConfig getServletConfig() {System.out.println("getServletConfig()");return null;}@Overridepublic void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {System.out.println("正在销毁");}@Overridepublic String getServletInfo() {System.out.println("getServletInfo()");return null;}@Overridepublic void destroy() {System.out.println("正在销毁");} }
浏览器访问servlet时,需要将servlet与路径绑定,需要在web.xml中对servlet进行配置:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"version="4.0"><servlet> <servlet-name>testservlet</servlet-name>
<servlet-class>com.wbg.testservlet</servlet-class></servlet> </web-app>
实现接口的意义:
1、接口和实现分离给软件系统提供了无限的想像空间,才使得软件世界如此丰富多彩。
2、接口和实现分离使得多态变为可能
3、接口和实现分离使得解耦变为可能
4、接口和实现分离使得组合更有意义
5、接口和实现分离使得设计模式变为可能,才能衍生出23种设计模式。
简单的说接口和实现分离给软件系统提供了无限的想象空间、多态变为可能、解耦变为可能、组合更有意义、设计模式变为可能,才能衍生出23钟设计模式(创建型、结构型、行为型)
servlet三种方式实现servlet接口相关推荐
- 华三防火墙旁路部署三种方式之子接口旁路
为什么需要旁路部署:原有组网复杂 好处:物理上的旁路,逻辑上的串联 1.防火墙子接口形式旁路部署 配置原理: loo0是为了方便测试使用,实际组网此处是属于公网. 需要实现的目的:让进来的流量先经过防 ...
- 创建线程的第三种方式:实现Callable接口(含部分源码解析)
创建线程的第三种方式--实现Callable接口 package com.lqy.Multithreading; import java.util.concurrent.Callable; impor ...
- JUC-多线程(5.获得线程的第三种方式)学习笔记
文章目录 获得线程的第三种方式 : Callable接口 1. 前言 1. 获得多线程的方法几种? 2. 以下两种获得线程的方式的异同 2. 使用 1. 重写 call 方法 2.创建线程 3.获取返 ...
- Struts2访问Servlet的三种方式
Struts2访问Servlet的三种方式 基本介绍 在Struts2中,Action并没有直接和Servlet API耦合.虽然Struts2 中的Action访问Servlet API麻烦一些,但 ...
- 【小家Spring】SpringBoot中使用Servlet、Filter、Listener三大组件的三种方式以及原理剖析
每篇一句 要么就安逸的穷,要么就拼命的干 前提概要 web开发使用Controller基本能解决大部分的需求,但是有时候我们也需要使用Servlet,因为相对于拦截和监听来说,有时候原生的还是比较好用 ...
- Android:按键响应方式第一种onClick属性,第二种方法接口类,第三种方式匿名内部类,第四种方式Activity
一.在xml 中设置按键的onClick 绑定的函数 新建一个工程 然后在相关的页面添加一个Button 运行一下 实际上,现在这个按键一,我们去运行的时候,他是没有任何的反馈的,我们按完它,他没有任 ...
- Java中实现接口的三种方式您造吗?
本文介绍了Java中实现接口的三种方式:常规实现方式.匿名内部类和 Lambda表达式实现方式.希望已经了解的同学可以重新温习一下,不了解的同学则从中受益! Java中接口最常规的实现方式 同学们都会 ...
- C#调用COM接口的三种方式
1.准备工作 使用OLEView.exe查看本机安装的COM组件CLSID和接口Guid COM组件的提供者没有提供CLSID等信息或信息提供不全时,可以使用OleView.exe来查看其类和接口 ...
- 调用第三方接口的三种方式
调用第三方接口到三种方式 链接 Hutool 依赖 <dependency><groupId>cn.hutool</groupId><artifactId&g ...
- 【Spring MVC学习】WebApplicationContext初始化的三种方式
ApplicationContext是Spring的核心,Context我们通常解释为上下文环境,我想用"容器"来表述它更容易理解一些,ApplicationContext则是&q ...
最新文章
- RabbitMQ 入门系列(5)— RabbitMQ 使用场景优缺点
- Vagrant安装指南
- 刻意练习:LeetCode实战 -- Task08.删除排序链表中的重复元素
- jQuery选择器实现隔行变色和使用javaScript实现隔行变色
- POJ 2152 Fire(树形DP)
- python怎么填充画布_如何在kivy中用图像填充画布?
- Django - 内置admin
- 检查虚ip跟实ip之间网络问题_虚电路有哪些特点 虚电路原理介绍【详解】
- android渐变布局,关于Android的径向渐变高级编程的实现
- 拓端tecdat|python在Keras中使用LSTM解决序列问题
- Linux系统中如何查看日志信息
- excel如何把顺序倒过来_Excel中表格进行颠倒顺序的操作方法
- python视频格式转换命令_音视频格式转换
- WS以及NW小世界网络的生成(MATLAB)
- 每日一题 - 剪绳子
- 利用 Map-Reduce 从文件中找到出现频率最高的 10 个 URL(2021 VLDB Summer School Lab0)
- L. Lifeguards(排序)
- 获取处于选中状态的checkbox的id值
- 智安网络丨浅谈信息系统终止时如何确保信息资产的安全
- 玩转华为数据中心交换机系列 | 配置MAC地址漂移检测示例
热门文章
- Keras基础项目实践
- C++ std::set insert 失败 原因和解决方案 operator
- 共享锁 排他锁 是什么区别
- java执行复杂linux,解决java使用Runtime.exec执行linux复杂命令不成功问题
- mysql改变授权_mysql8使用grant授权修改
- 搭建Maven环境及其基本知识
- cenos各个版本下载地址
- HTML 变形(transform)、转换(transition)和动画(animation)
- MyBatis工作流程及配置
- 聊聊对账系统的设计方案