Servlet源码解析:Session、Request以及Response

来源:互联网 发布:淘宝优惠券在哪里领取 编辑:程序博客网 时间:2024/06/16 13:39

      首先我们来看看ServletRequest的源码:

public interface ServletRequest {    //获取request的属性(注意不是请求参数)    public Object getAttribute(String name);    //获取request的所有属性的名字    public Enumeration<String> getAttributeNames();    //获取request编码    public String getCharacterEncoding();    //设置request编码    public void setCharacterEncoding(String env)            throws java.io.UnsupportedEncodingException;    //获取内容长度(http报文的body字段)    public int getContentLength();    //获取内容长度    public long getContentLengthLong();    //获取Content-Type    public String getContentType();    //获取输入流    public ServletInputStream getInputStream() throws IOException;    //获取请求参数name对应的键值    public String getParameter(String name);    //获取所有请求参数名    public Enumeration<String> getParameterNames();    //获取所有请求参数键值    public String[] getParameterValues(String name);    //获取所有请求参数    public Map<String, String[]> getParameterMap();    //获取请求协议(完整名)scheme/version    public String getProtocol();    //获取协议名    public String getScheme();    //获取域名    public String getServerName();    //获取服务器http端口    public int getServerPort();    //获取reader用于读取body字段    public BufferedReader getReader() throws IOException;    //获取IP地址或最后的代理IP地址    public String getRemoteAddr();    //如果servlet不行或者不想处理域名则这个函数返回ip    public String getRemoteHost();    //设置request属性    public void setAttribute(String name, Object o);    //移除属性    public void removeAttribute(String name);    //根据Accept-Language获取国际化资源,如果浏览器没提供这个字段则使用默认Locale    public Locale getLocale();    //Accept-Language可以是多个资源    public Enumeration<Locale> getLocales();    //该request是不是使用HTTPS    public boolean isSecure();    //获取Request分发器,它能继续访问下一个servlet再返回结果给客户端,它有    //include和forward两种方式    public RequestDispatcher getRequestDispatcher(String path);    //获取实际路径    public String getRealPath(String path);    //获取浏览器端口    public int getRemotePort();    //获取服务器域名    public String getLocalName();    //获取服务器IP地址    public String getLocalAddr();    //获取服务器端口    public int getLocalPort();    //获取Servlet应用上下文    public ServletContext getServletContext();    //servlet3开始支持异步任务    public AsyncContext startAsync() throws IllegalStateException;    //开始异步任务    public AsyncContext startAsync(ServletRequest servletRequest,            ServletResponse servletResponse) throws IllegalStateException;    //异步任务是否开始    public boolean isAsyncStarted();    //是否支持异步任务    public boolean isAsyncSupported();    //获取异步上下文    public AsyncContext getAsyncContext();    //servlet3开始新增的类型对象,包括REQUEST,FORWARD,INCLUDE,ASYNC,ERROR几种类型    public DispatcherType getDispatcherType();}

    ServletRequest有个子接口叫HttpServletRequest,它只是在ServletRequest的基础上增加了一些获取Cookies、Session、http报文头、认证相关(Servlet3)的方法这里不做详细介绍了。

    我们再来看看ServletResponse的源码:

   

public interface ServletResponse {    //获取编码信息    public String getCharacterEncoding();    //获取Content-Type    public String getContentType();    //获取输出流    public ServletOutputStream getOutputStream() throws IOException;    //获取字符输出流    public PrintWriter getWriter() throws IOException;    //设置编码信息     public void setCharacterEncoding(String charset);    //设置内容长度信息    public void setContentLength(int len);    public void setContentLengthLong(long length);    public void setContentType(String type);    //设置buffer的大小直接影响被加载入内存的时间和servlet执行效率,buffer缓存将要输出的内容    public void setBufferSize(int size);    public int getBufferSize();    //向客户端提交response的内容    public void flushBuffer() throws IOException;    //仅仅清除buffer的内容不包括header和状态    public void resetBuffer();    //response是否已经提交内容    public boolean isCommitted();    //清除buffer的所有内容    public void reset();    public void setLocale(Locale loc);    public Locale getLocale();}

      和前面ServletRequest的情况一样,不再详细介绍ServletResponse的子接口HttpServletResponse。

      接下来我们再看看HttpSession这个接口:

public interface HttpSession {    //获取创建时间    public long getCreationTime();    //获取session id    public String getId();    //获取最后一次客户端连接时间    public long getLastAccessedTime();    public ServletContext getServletContext();    //设置session的失效timeout,0或-1表示永久有效    public void setMaxInactiveInterval(int interval);    public int getMaxInactiveInterval();    //将被移除方法,没有替代方法    public HttpSessionContext getSessionContext();    //获取属性    public Object getAttribute(String name);    //将被废弃方法,用getAttribute方法代替    public Object getValue(String name);    //获取所有属性名    public Enumeration<String> getAttributeNames();    //将被废弃方法    public String[] getValueNames();    public void setAttribute(String name, Object value);    //将被废弃方法    public void putValue(String name, Object value);    public void removeAttribute(String name);    //将被废弃方法    public void removeValue(String name);    //使session失效    public void invalidate();    //是否是新创建的session,客户端第二次访问同一个session时它返回false    public boolean isNew();}

      Servlet规范的核心内容就在于Servlet、Response、Request、Session、Cookie、RequestDispatcher这几个核心接口或类,像jsper,el,webservice,security,filter,listener的相关实现挺简单的,可以抽空看看,这里就不再赘述了。

     


     

0 0