说明

本篇由于介绍Servlet容器回传请求方法service(ServletRequest req, ServletResponse res);传入参数用户请求参数request和请求返回参数response的介绍。

request和response

Servlet-api.jar 中定义了两个基本的接口用于处理用户请求处理(ServletRequest)和用户响应返回(ServletResponse)。同时,对应的javax.servlet.http中对其进行扩展,针对该接口重定义了两个对应的接口:HttpServletRequest和HttpServletResponse,这里主要通过源码介绍其基本的用法,后面会通过示例对其进行扩展演示使用方法。

  1. Request

用户请求,即用户对服务端发起的用户请求数据,servlet-api.jar中提供了两个接口:一个通用原始的ServletRequest,一个针对http协议的接口HttpServletRequest(继承自ServletRequest).

如下,为ServletRequest的源码:


package javax.servlet; import java.io.*;
import java.util.*; public interface ServletRequest { /**
*通过参数名获取value,若参数名不存在,返回null.
* 获取的attribute可能是servlet设置的客户端信息,也可以是
*通过setAttribute设置的attribute.
*
*/
public Object getAttribute(String name); /**
*attribute的所有属性名集合.
*/
public Enumeration<String> getAttributeNames(); /**
*返回正文中的字串编码
*/
public String getCharacterEncoding(); public void setCharacterEncoding(String env) throws UnsupportedEncodingException; /**
*返回正文的长度,若请求失败(未知),返回-1.
*/
public int getContentLength(); /**
*获取正文MIME类型,获取失败返回null.
*/
public String getContentType(); /**
*获取输入流,读取请求内容,ServletInputStream继承了InputStream方法,添加按行读取
*方法
*/
public ServletInputStream getInputStream() throws IOException; /**
*返回请求的参数通过给定的name(单个数值时),返回String或者null.
*按顺序匹配,匹配首个name.
*/
public String getParameter(String name); /**
*返回Enumeration<String>对象,空的或者多个String集合.
*/
public Enumeration<String> getParameterNames(); public String[] getParameterValues(String name); public Map<String, String[]> getParameterMap(); /**
*返回请求协议
*/
public String getProtocol(); /**
*请求scheme.
*/
public String getScheme(); /**
*获取服务器的host名
*/
public String getServerName(); /**
*请求服务端监听端口
*/
public int getServerPort(); /**
*获取一个BufferedReader输入流
*/
public BufferedReader getReader() throws IOException; /**
*返回客户端的ip地址
*/
public String getRemoteAddr(); /**
*返回客户端的主机名
*/
public String getRemoteHost(); public void setAttribute(String name, Object o); public void removeAttribute(String name); /**
*返回客户端的Locale环境
*/
public Locale getLocale(); /**
*默认返回Local的Enumeration.
*/
public Enumeration<Locale> getLocales(); /**
*是否做了安全策略(HTTPS)
*/
public boolean isSecure(); /**
*获取一个RequestDispatcher对象,可用来做跳转使用.
*/
public RequestDispatcher getRequestDispatcher(String path); /**
* @deprecated As of Version 2.1 of the Java Servlet API,
* use {@link ServletContext#getRealPath} instead.
*/
public String getRealPath(String path); /**
*
* @since Servlet 2.4
*/
public int getRemotePort(); /**
*
* @since Servlet 2.4
*/
public String getLocalName(); /**
*
* @since Servlet 2.4
*
*/
public String getLocalAddr(); /**
*
* @since Servlet 2.4
*/
public int getLocalPort(); /**
*
* @since Servlet 3.0
*/
public ServletContext getServletContext(); /**
*
* @since Servlet 3.0
*/
public AsyncContext startAsync() throws IllegalStateException; /**
*
* @since Servlet 3.0
*/
public AsyncContext startAsync(ServletRequest servletRequest,
ServletResponse servletResponse)
throws IllegalStateException; /**
*
* @since Servlet 3.0
*/
public boolean isAsyncStarted(); /**
*
* @since Servlet 3.0
*/
public boolean isAsyncSupported(); /**
*
* @since Servlet 3.0
*/
public AsyncContext getAsyncContext(); /**
*
* @since Servlet 3.0
*/
public DispatcherType getDispatcherType();
}

如上,为ServletRequest的源码,主要是获取请求参数和输入流。

  1. Response

用户请求响应,响应返回用户请求数据,和Request类似,主要是获取客户端和服务端基本信息以及获取一个输出流用于返回响应数据。源码如下:

public interface ServletResponse {

    public String getCharacterEncoding();

    /**
* @since Servlet 2.4
*/ public String getContentType(); /**
*获取输出流ServletOutputStream
*/
public ServletOutputStream getOutputStream() throws IOException; /**
*获取输出流PrintWriter
*/
public PrintWriter getWriter() throws IOException; /**
*
* @since Servlet 2.4
*
*/
public void setCharacterEncoding(String charset); public void setContentLength(int len); public void setContentType(String type); public void setBufferSize(int size); public int getBufferSize(); public void flushBuffer() throws IOException; /**
*
* @since Servlet 2.3
*/ public void resetBuffer(); public boolean isCommitted(); public void reset(); public void setLocale(Locale loc); public Locale getLocale(); }

返回较为简单,主要是通过输出流,将需要返回的数据写入即可。servlet目前貌似只支持htpp协议,Tomcat的对应连接器配置如下:

    <Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>

Tomcat会对于请求的数据格式进行验证,若非http格式数据会禁止访问,返回404。因此一般对于servlet的创建需要继承HttpServlet来处理。

Enjoytoday,EnjoyCoding

最新文章

  1. 关于mybatis的理解
  2. Centos 7 在VBox下不能联网
  3. SharePoint 2013 和卷影复制服务(VSS)概述
  4. java 22 - 16 多线程之生产者和消费者的问题
  5. 如何在Actionbarsherlock中一直显示overflow效果?
  6. HTML 学习笔记 JavaScript (节点)
  7. lua 模块功能
  8. SQL 比较乱
  9. 实体框架 (EF) 入门 =&gt; 四、CodeFirst 枚举支持
  10. 3036: 绿豆蛙的归宿 - BZOJ
  11. jquery 清空动态append添加元素,remove方法
  12. 标准I/O库之流和FILE对象
  13. wget www.baidu.com执行流程分析
  14. PHP获取Mp3文件信息
  15. Centos6.5_x86上Oracle11g2 32位的安装与卸载以及相关问题汇总
  16. 【BZOJ3196】二逼平衡树(树状数组,线段树)
  17. Spring使用笔记(二)Bean装配
  18. python3配置 opencv
  19. Codeforces Round #425 (Div. 2) Problem B Petya and Exam (Codeforces 832B) - 暴力
  20. 五、Oracle的常识点

热门文章

  1. C# Parallel 多线程并发
  2. List&lt;string&gt;和string[]数组之间的相互转换,需要的朋友可以参考下
  3. Consul-template+nginx实现自动负载均衡
  4. spring的事物管理配置
  5. 【产品】移动应用监控平台调研——bugly&amp;fabric
  6. UML简单介绍—类图详解
  7. FlowPortal 6.00c 使用xFormDesigner复制粘贴中文总是乱码
  8. docker Dockerfile里使用的命令说明
  9. 编译出错:must be index or base register
  10. Java中dimension类详解