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

      首先我们来看看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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值