request和response

1. Request与Response

1.1Web应用运行机制

首先,我们先来看一看Request与Response在Web应用程序运行时,是怎么样的。
在这里插入图片描述
通过上图的Web应用程序运行机制,我们可以知道关于Request与Response的信息:

Web应用程序接收一次请求,就创建一个Request对象和Response对象。
通过Request对象可以在服务器端获取客户端发送的请求数据内容。
通过Response对象可以生成服务器端向客户端响应的数据内容。
Request对象和Response对象并不是Web应用程序所创建的,而是由Tomcat服务器创建(JavaEE并没有Request与Response的实现类)。
JavaEE提供了javax.servlet.http包中提供了HttpServletRequestHttpServletResponse接口,这两个接口是继承于javax.servlet包中的ServletRequest和ServletResponse接口。
javax.servlet包中的ServletRequest和ServletResponse接口是与协议无关的,而javax.servlet.http包中的HttpServletRequest和HttpServletResponse接口是与Http协议有关的。

1.2. 百度百科
  • Request
    Request这个对象不用事先声明,就可以在JSP网页中使用,在编译为Servlet之后,它会转换为javax.servlet.http.HttpServletRequest形态的对象,HttpServletRequest对象是有关于客户端所发出的请求的对象,只要是有关于客户端请求的信息,都可以藉由它来取得,例如请求标头、请求方法、请求参数、客户端IP,客户端浏览器等等信息。
  • Response
    Response对象用于动态响应客户端请示,控制发送给用户的信息,并将动态生成响应。Response对象只提供了一个数据集合cookie,它用于在客户端写入cookie值。若指定的cookie不存在,则创建它。若存在,则将自动进行更新。结果返回给客户端浏览器。

2. Response

Response是Web应用程序用来封装向客户端响应信息的,是Servlet接口的service()方法的一个参数,类型为javax.servlet.http.HttpServletResponse。客户端每次发送请求时,服务器都会创建一个Response对象,并传递给Servlet接口的service()方法,来完成向客户端的响应工作。

下列是javax.servlet.ServletResponse常用的API列表:

Method Summary

ServletOutputStream
getOutputStream()
Returns a ServletOutputStream suitable for writing binary data in the response.

PrintWriter
getWriter()
Returns a PrintWriter object that can send character text to the client.

void
setCharacterEncoding(String charset)
Sets the character encoding (MIME charset) of the response being sent to the client, for example, to UTF-8.

void
setContentType(String type)
Sets the content type of the response being sent to the client, if the response has not been committed yet.

3. Request

Request是Web应用程序用来封装向客户端请求信息的,是Servlet接口的service()方法的一个参数,类型为javax.servlet.http.HttpServletRequest。客户端每次发送请求时,服务器都会创建一个Request对象,并传递给Servlet接口的service()方法,来完成向客户端的请求工作。

下列是javax.servlet.ServletRequest常用的API列表:

Method Summary

String
getCharacterEncoding()
Returns the name of the character encoding used in the body of this request.

String
getContentType()
Returns the MIME type of the body of the request, or null if the type is not known.

String
getParameter(String name)
Returns the value of a request parameter as a String, or null if the parameter does not exist.

Map<String,String[]>
getParameterMap(String name)
Returns the value of a request parameter as a Map<String,String[]>, or null if the parameter does not exist.

String
getRealPath(String path)
Deprecated. As of Version 2.1 of the Java Servlet API, use ServletContext.getRealPath(java.lang.String) instead.

String
getRemoteAddr()
Returns the Internet Protocol (IP) address of the client or last proxy that sent the request.

String
getRemoteHost()
Returns the fully qualified name of the client or the last proxy that sent the request.

int
getRemotePort()
Returns the Internet Protocol (IP) source port of the client or last proxy that sent the request.

RequestDispatcher
getRequestDispatcher(String path)
Returns a RequestDispatcher object that acts as a wrapper for the resource located at the given path.

String
getServerName()
Returns the host name of the server to which the request was sent.

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值