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-Typepublic 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/versionpublic 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不行或者不想处理域名则这个函数返回ippublic String getRemoteHost();//设置request属性public void setAttribute(String name, Object o);//移除属性public void removeAttribute(String name);//根据Accept-Language获取国际化资源,如果浏览器没提供这个字段则使用默认Localepublic Locale getLocale();//Accept-Language可以是多个资源public Enumeration<Locale> getLocales();//该request是不是使用HTTPSpublic 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-Typepublic 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 idpublic 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时它返回falsepublic boolean isNew();}人生就是一次充满未知的旅行,在乎的是沿途的风景,

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

相关文章:

你感兴趣的文章:

标签云: