最近看tomcat源码,这类接口多的有点眩,整理出来看一下。(基于tomcat4)
javax.servlet.ServletRequset接口,和org.apache.catalina.Request接口,有什么联系?
其实就是对Request不同层面上的定义。
一、首先看下javax.servlet.Servlet接口
package javax.servlet;
import java.io.IOException;
public abstract interface Servlet {
public abstract void init(ServletConfig paramServletConfig)
throws ServletException;
public abstract ServletConfig getServletConfig();
public abstract void service(ServletRequest paramServletRequest,
ServletResponse paramServletResponse) throws ServletException,
IOException;
public abstract String getServletInfo();
public abstract void destroy();
}
这些方法应该非常熟悉了。
二、javax.servlet.http.HttpServlet抽象类
package javax.servlet.http;public abstract class HttpServlet extends GenericServlet implements Serializable {
...
}
丰富了servlet接口,对应http协议,添加了doGet, doPut, doPost等方法。
三、javax.servlet.ServletRequest接口
package javax.servlet;public abstract interface ServletRequest {
public abstract Object getAttribute(String paramString);public abstract Enumeration getAttributeNames();
public abstract String getCharacterEncoding();
public abstract void setCharacterEncoding(String paramString)
throws UnsupportedEncodingException;public abstract int getContentLength();
public abstract String getContentType();
public abstract ServletInputStream getInputStream() throws IOException;
public abstract String getParameter(String paramString);
public abstract Enumeration getParameterNames();
public abstract String[] getParameterValues(String paramString);
public abstract Map getParameterMap();
public abstract String getProtocol();
public abstract String getScheme();
public abstract String getServerName();
public abstract int getServerPort();
public abstract BufferedReader getReader() throws IOException;
public abstract String getRemoteAddr();
public abstract String getRemoteHost();
public abstract void setAttribute(String paramString, Object paramObject);
public abstract void removeAttribute(String paramString);
public abstract Locale getLocale();
public abstract Enumeration getLocales();
public abstract boolean isSecure();
public abstract RequestDispatcher getRequestDispatcher(String paramString);
/** @deprecated */
public abstract String getRealPath(String paramString);
}
四、javax.servlet.http.HttpServletRequest
public abstract interface HttpServletRequest extends ServletRequest {...}
添加了http协议部分,比如session,cookie等。
五、org.apache.catalina.Requst(tomcat4,后面的版本有变化)
package org.apache.catalina;public interface Request {
public String getAuthorization();
public void setAuthorization(String authorization);
public Connector getConnector();
public void setConnector(Connector connector);
public Context getContext();
public void setContext(Context context);
public String getInfo();
public ServletRequest getRequest();
public Response getResponse();
public void setResponse(Response response);
public Socket getSocket();
public void setSocket(Socket socket);
public InputStream getStream();
public void setStream(InputStream stream);
public Wrapper getWrapper();
public void setWrapper(Wrapper wrapper);
public ServletInputStream createInputStream() throws IOException;
public void finishRequest() throws IOException;
public Object getNote(String name);
public Iterator getNoteNames();
public void recycle();
public void removeNote(String name);
public void setContentLength(int length);
public void setContentType(String type);
public void setNote(String name, Object value);
public void setProtocol(String protocol);
public void setRemoteAddr(String remote);
public void setScheme(String scheme);
public void setSecure(boolean secure);
public void setServerName(String name);
public void setServerPort(int port);
}
六、org.apache.catalina.HttpRequest
public interface HttpRequest extends Request {...}
增加了对http协议头处理部分,比如url,参数,cookies等。
七、org.apache.catalina.connector.HttpRequestBase
虽然已经弃用了,但可以作为一个参考,实现了jdk和catalina定义的Request
public class HttpRequestBase
extends RequestBase
implements HttpRequest, HttpServletRequest {...}
小结
tomcat中,对于jdk中的servlet和ServletRequest肯定要实现的。而tomcat作为容器应用,会有很多自身的需求,所以自己定义了一套接口,跟jdk中不同层面。并且提供两套接口的类,让web开发者使用。
Request、Servlet及其子接口,布布扣,bubuko.com