首页 > 编程语言 >Servlet源码

Servlet源码

时间:2023-06-05 15:36:11浏览次数:37  
标签:String resp req private 源码 void Servlet public

顶级接口:Servlet:
 package javax.servlet;


 import java.io.IOException;




 public interface Servlet {


    
     public void init(ServletConfig config) throws ServletException;
     
     public ServletConfig getServletConfig();
     
     public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException;

     public String getServletInfo();

     public void destroy();
 }
GenericServlet抽象类:

package javax.servlet;


 import java.io.IOException;
 import java.util.Enumeration;


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


     private static final long serialVersionUID = 1L;


     private transient ServletConfig config;
     
     @Override
     public void init(ServletConfig config) throws ServletException {
         this.config = config;
         this.init();
     }
     
     public void init() throws ServletException {
         // NOOP by default
     }
     
     @Override
     public abstract void service(ServletRequest req, ServletResponse res) throws ServletException, IOException;
     
     @Override
     public ServletConfig getServletConfig() {
         return config;
     }
     
     @Override
     public String getServletInfo() {
         return "";
     } 
    
     @Override
     public void destroy() {
         // NOOP by default
     }
     
     
     public GenericServlet() {
         // NOOP
     }
     @Override
     public String getInitParameter(String name) {
         return getServletConfig().getInitParameter(name);
     }
     @Override
     public Enumeration<String> getInitParameterNames() {
         return getServletConfig().getInitParameterNames();
     }
     public void log(String msg) {
         getServletContext().log(getServletName() + ": " + msg);
     }
     public void log(String message, Throwable t) {
         getServletContext().log(getServletName() + ": " + message, t);
     }
     @Override
     public String getServletName() {
         return config.getServletName();
     }
     @Override
     public ServletContext getServletContext() {
         return getServletConfig().getServletContext();
     }
 }
HttpServlet:
 package javax.servlet.http;


 import java.io.IOException;
 import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
 import java.io.UnsupportedEncodingException;
 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 ResourceBundle lStrings =
         ResourceBundle.getBundle(LSTRING_FILE);
     
     public HttpServlet() {
         // NOOP
     }
     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(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);
         } else {
             resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);
         }
     }
     protected long getLastModified(HttpServletRequest req) {
         return -1;
     }
     protected void doHead(HttpServletRequest req, HttpServletResponse resp)
         throws ServletException, IOException {


         if (DispatcherType.INCLUDE.equals(req.getDispatcherType())) {
             doGet(req, resp);
         } else {
             NoBodyResponse response = new NoBodyResponse(resp);
             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(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);
         } else {
             resp.sendError(HttpServletResponse.SC_BAD_REQUEST, 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(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);
         } else {
             resp.sendError(HttpServletResponse.SC_BAD_REQUEST, 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(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);
         } else {
             resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);
         }
     }
     private static Method[] getAllDeclaredMethods(Class<?> c) {


         if (c.equals(javax.servlet.http.HttpServlet.class)) {
             return null;
         }
         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;
         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=METHOD_GET;
         if (ALLOW_HEAD)
             if (allow==null) allow=METHOD_HEAD;
             else allow += ", " + METHOD_HEAD;
         if (ALLOW_POST)
             if (allow==null) allow=METHOD_POST;
             else allow += ", " + METHOD_POST;
         if (ALLOW_PUT)
             if (allow==null) allow=METHOD_PUT;
             else allow += ", " + METHOD_PUT;
         if (ALLOW_DELETE)
             if (allow==null) allow=METHOD_DELETE;
             else allow += ", " + METHOD_DELETE;
         if (ALLOW_TRACE)
             if (allow==null) allow=METHOD_TRACE;
             else allow += ", " + METHOD_TRACE;
         if (ALLOW_OPTIONS)
             if (allow==null) allow=METHOD_OPTIONS;
             else allow += ", " + METHOD_OPTIONS;


         resp.setHeader("Allow", allow);
     }
     protected void doTrace(HttpServletRequest req, HttpServletResponse resp)
         throws ServletException, IOException
     {
         int responseLength;


         String CRLF = "\r\n";
         StringBuilder buffer = new StringBuilder("TRACE ").append(req.getRequestURI())
             .append(" ").append(req.getProtocol());


         Enumeration<String> reqHeaderEnum = req.getHeaderNames();


         while( reqHeaderEnum.hasMoreElements() ) {
             String headerName = reqHeaderEnum.nextElement();
             buffer.append(CRLF).append(headerName).append(": ")
                 .append(req.getHeader(headerName));
         }
         buffer.append(CRLF);


         responseLength = buffer.length();


         resp.setContentType("message/http");
         resp.setContentLength(responseLength);
         ServletOutputStream out = resp.getOutputStream();
         out.print(buffer.toString());
         out.close();
         return;
     }
     protected void service(HttpServletRequest req, HttpServletResponse resp)
         throws ServletException, IOException {


         String method = req.getMethod();


         if (method.equals(METHOD_GET)) {
             long lastModified = getLastModified(req);
             if (lastModified == -1) {
                 doGet(req, resp);
             } else {
                 long ifModifiedSince;
                 try {
                     ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE);
                 } catch (IllegalArgumentException iae) {
                     ifModifiedSince = -1;
                 }
                 if (ifModifiedSince < (lastModified / 1000 * 1000)) {
                     maybeSetLastModified(resp, lastModified);
                     doGet(req, resp);
                 } else {
                     resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
                 }
             }


         } else if (method.equals(METHOD_HEAD)) {
             long lastModified = getLastModified(req);
             maybeSetLastModified(resp, lastModified);
             doHead(req, resp);


         } else if (method.equals(METHOD_POST)) {
             doPost(req, resp);


         } else if (method.equals(METHOD_PUT)) {
             doPut(req, resp);


         } else if (method.equals(METHOD_DELETE)) {
             doDelete(req, resp);


         } else if (method.equals(METHOD_OPTIONS)) {
             doOptions(req,resp);


         } else if (method.equals(METHOD_TRACE)) {
             doTrace(req,resp);


         } else {
    
             String errMsg = lStrings.getString("http.method_not_implemented");
             Object[] errArgs = new Object[1];
             errArgs[0] = method;
             errMsg = MessageFormat.format(errMsg, errArgs);


             resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg);
         }
     }
     private void maybeSetLastModified(HttpServletResponse resp,
                                       long lastModified) {
         if (resp.containsHeader(HEADER_LASTMOD))
             return;
         if (lastModified >= 0)
             resp.setDateHeader(HEADER_LASTMOD, lastModified);
     }
     @Override
     public void service(ServletRequest req, ServletResponse res)
         throws ServletException, IOException {


         HttpServletRequest  request;
         HttpServletResponse response;


         try {
             request = (HttpServletRequest) req;
             response = (HttpServletResponse) res;
         } catch (ClassCastException e) {
             throw new ServletException("non-HTTP request or response");
         }
         service(request, response);
     }
 }
 class NoBodyResponse extends HttpServletResponseWrapper {
     private NoBodyOutputStream                noBody;
     private PrintWriter                        writer;
     private boolean                        didSetContentLength;


    
     NoBodyResponse(HttpServletResponse r) {
         super(r);
         noBody = new NoBodyOutputStream();
     }
     void setContentLength() {
         if (!didSetContentLength) {
             if (writer != null) {
                 writer.flush();
             }
             super.setContentLength(noBody.getContentLength());
         }
     }
     @Override
     public void setContentLength(int len) {
         super.setContentLength(len);
         didSetContentLength = true;
     }
     @Override
     public void setHeader(String name, String value) {
         super.setHeader(name, value);
         checkHeader(name);
     }
     @Override
     public void addHeader(String name, String value) {
         super.addHeader(name, value);
         checkHeader(name);
     }
     @Override
     public void setIntHeader(String name, int value) {
         super.setIntHeader(name, value);
         checkHeader(name);
     }
     @Override
     public void addIntHeader(String name, int value) {
         super.addIntHeader(name, value);
         checkHeader(name);
     }
     private void checkHeader(String name) {
         if ("content-length".equalsIgnoreCase(name)) {
             didSetContentLength = true;
         }
     }
     @Override
     public ServletOutputStream getOutputStream() throws IOException {
         return noBody;
     }
     @Override
     public PrintWriter getWriter() throws UnsupportedEncodingException {


         if (writer == null) {
             OutputStreamWriter w;


             w = new OutputStreamWriter(noBody, getCharacterEncoding());
             writer = new PrintWriter(w);
         }
         return writer;
     }
 }
 class NoBodyOutputStream extends ServletOutputStream {


     private static final String LSTRING_FILE =
         "javax.servlet.http.LocalStrings";
     private static ResourceBundle lStrings =
         ResourceBundle.getBundle(LSTRING_FILE);


     private int                contentLength = 0;
     NoBodyOutputStream() {
       
     }
     int getContentLength() {
         return contentLength;
     }
     @Override
     public void write(int b) {
         contentLength++;
     }
     @Override
     public void write(byte buf[], int offset, int len) throws IOException {
         if (buf == null) {
             throw new NullPointerException(
                     lStrings.getString("err.io.nullArray"));
         }


         if (offset < 0 || len < 0 || offset+len > buf.length) {
             String msg = lStrings.getString("err.io.indexOutOfBounds");
             Object[] msgArgs = new Object[3];
             msgArgs[0] = Integer.valueOf(offset);
             msgArgs[1] = Integer.valueOf(len);
             msgArgs[2] = Integer.valueOf(buf.length);
             msg = MessageFormat.format(msg, msgArgs);
             throw new IndexOutOfBoundsException(msg);
         }
         contentLength += len;
     }
 }

标签:String,resp,req,private,源码,void,Servlet,public
From: https://blog.51cto.com/u_14121041/6416349

相关文章

  • 视频直播源码,动态合并element-ui el-table列和行
    视频直播源码,动态合并element-uiel-table列和行HTML: <template>  <div>    <el-table     :data="tableData"     show-summary     :span-method="arraySpanMethod"    style="width:100%">      <......
  • 【Netty底层数据交互源码】
    (文章目录)如何学习Netty的底层深入了解Netty的底层实现需要对JavaNIO、OSI模型、TCP/IP协议栈等底层网络知识有一定的了解。下面是一些建议,可以帮助你更深入地了解Netty的底层实现:学习JavaNIO:JavaNIO是Java中用于处理I/O操作的一套库。在深入了解Netty的底层实现时,你需要......
  • 从源码分析 Go 语言使用 cgo 导致的线程增长
    TDengineGo连接器https://github.com/taosdata/driver-go使用cgo调用taos.so中的API,使用过程中发现线程数不断增长,本文从一个cgo调用开始解析Go源码,分析造成线程增长的原因。转换cgo代码对driver-go/wrapper/taosc.go进行转换gotoolcgotaosc.go执行后生成......
  • HMaster启动源码分析
       写之前先吐槽一下自己的sb公司环境,电脑上不了网,优盘又不能插。所以做点笔记基本上都是晚上回家再写一遍。哎,废话不说了   先贴个hbase在构造函数中起来的RPC服务的UML图:http://blackproof.iteye.com/blog/2029170   HMaster启动会调用Run方法,概述为  HBase启动......
  • 手把手实现springboot整合flowable、附源码-视频教程
    手把手实现springboot整合flowable、附源码-视频教程[toc]视频教程点击:https://www.bilibili.com/video/BV1fa411j7Q5/插件安装BPMN绘图可视化工具>FlowableBPMNvisualizer导入依赖<dependency><groupid>org.springframework.boot</groupid><artifact......
  • 3. Servlet原理
    Servlet是JavaWeb应用程序中的重要组件之一,它是一个Java类,用于处理客户端HTTP请求和生成HTTP响应。Servlet的原理如下:服务器启动时,Servlet容器读取部署描述符文件(web.xml),并解析部署的Servlet和URL映射规则。客户端发送HTTP请求到服务器,并在URL中包含了Servlet的映射规则。......
  • JBPM5 Designer 2.3源码问题
    最新本2.4发布,但是里面是使用Maven的module方式来管理,鉴于知识有限,不会这种方式,所以选择2.3版本的源码[color=blue][b]2.4[/b][/color]war:[url]http://sourceforge.net/projects/jbpm/files/designer/designer-2.4/[/url]源码:[url]https://github.com/dro......
  • Servlet3.0新功能: 异步处理
    2EE6和Glassfish3V正式发布了,J2EE6正式发布了Servlet3.0,为了能更好的对WEB2.0提供支持,3.0添加了异步处理的机制.HTTP1.1相对于HTTP1.0的影响.HTTP1.1最大的一个改变就是提供了长连接,这样HTTP不再是一次请求,一次连接的协议了,只要HTTP的connection不关闭,一次HTTP连接......
  • Hibernate Tools源码的使用
    Eclipse中Hibernatetools的安装和使用[url]http://zhoualine.iteye.com/blog/1190141[/url]源码里面有一个很好的格式化类:org.hibernate.tool.hbm2x.[color=darkblue][b]XMLPrettyPrinter[/b][/color]调用publicvoidformatFiles(){ formatXml("......
  • dubbo源码学习(四)初始化过程细节:解析服务
    今天将真正去看dubbo内部的实现过程,看dubbo的源码前我先把dubbo的用户指南和开发指指南大概的看了一遍,这样再看dubbo源码比较轻松。从用户指南和开发指指南可以找到相应的切入点,今天将介绍的是dubbo的初始化解析bean的过程:解析服务基于dubbo.jar内的META......