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(); }
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); } }
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(); } }
实现接口代码:
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 { @Override public void init(ServletConfig servletConfig) throws ServletException { System.out.println("初始化servlet"); } @Override public ServletConfig getServletConfig() { System.out.println("getServletConfig()"); return null; } @Override public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { System.out.println("正在销毁"); } @Override public String getServletInfo() { System.out.println("getServletInfo()"); return null; } @Override public 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种设计模式。