JSP —— 内置对象 Request 与 Response 与重定向

JSP 有9个内置对象:Request、Response、Session、Out、PageContext、Application、Config、Page、Exception。


简单介绍:

Request:即 javax.servlet.http.HttpServletRequest 对象,对应于一个HTTP 请求。作用范围:一次HTTP 请求(跳转的情况根据后面引用的文章应该是:forward跳转时,request作用范围为几个servlet或页面内都有效;sendRedirect跳转时,第一次request的servlet或页面内有效,因为实际上是两次独立的HTTP 请求)。

Response:即 javax.servlet.http.HttpServletResponse 对象,对应于一个HTTP响应。作用范围:一次HTTP 响应。

Session:即 javax.servlet.http.HttpSession 对象,会话信息。作用范围:一次用户会话时间段。

pageContext:与Application对象类似,只是作用范围限制于本网页内。

Application:为ServletContext类的对象,服务器启动后,该网站即对应一个Application对象,具有单例特性。作用范围:服务器启动期间。


Out:即 javax.jsp.JspWrite  对象,是一个输出流,用来向客户端输出数据。该对象用于各种对象的输出。
Exception:异常处理对象。

Page:表示从页面产生的一个Servlet实例,相当于这个JSP 产生Servlet类的this,可以通过Page对象访问实例的属性和函数。
Config:表示一个javax.servlet.ServletConfig对象。该对象用于存取Servlet实例的初始化参数。

注:

JSP 的范围(Scope,用于JavaBean)分别为page(注意不是Page对象)、request、session、application。这4个范围分别由pageContext、Request、Session、Application4个内置对象对应来保存对象(即保存对应的JavaBean),方法名都是setAttribute和getAttribute。更多查看JavaBean。

使用时首字母是小写的!


一、Request

常用方法:

1、Object getAttribute(String name):返回name指定的属性值,属性不存在时返回null。

2、void setAttribute(String name,Object value):在属性列表中添加/删除指定的属性。

3、String getParameter(String name):获取客户端发送给服务器的参数值。

4、String[] getParameterValues(String name):获得请求中指定参数的所有值。

5、String getProtocol():返回请求使用的协议,http1.0或http1.1.

6、String RequestURL():返回发出请求的客户端地址,但是不包括请求的参数字符串。

7、String getRemoteAddr():获取发出请求的客户端的IP 地址。

8、HTTPSession getSession():获取Session。

9、String getServletPath():获取客户端请求页面的路径。

10、getContentLength():获取HTTP请求信息体的长度。

11、getHeads(String s):获取请求头相应信息。s可取的参数有:accept、referer、accept-language、content-type、accept-encoding、user-agent(客户端浏览器内核等信息)、host、content-length、connection、cookie等。所以其它许多方法只是比较常用所以单独拿出来,如getCookies(String name)。


注意事项:

1、获取HTTP 请求头中的表单参数时,应该先判断获取到的值是否为null,因为用户可能还未填写,否则会报空指针错误。如:

String userAge=request.getParameter("age");
//下面一条可能报空指针错误
int age=Interger.parseInt("userAge");

2、获取输入的中文信息时,为避免乱码,先转换编码再显示:

String originalStr=request.getParameter("someText");
//转换编码再显示
byte[] b=originaStr.getBytes("ISO-8859-1");
String newStr=new String(b);


二、Response

所有方法:

Method Summary
 voidaddCookie(Cookie cookie)
          Adds the specified cookie to the response.
 voidaddDateHeader(String name, long date)
          Adds a response header with the given name and date-value.
 voidaddHeader(String name,String value)
          Adds a response header with the given name and value.
 voidaddIntHeader(String name, int value)
          Adds a response header with the given name and integer value.
 booleancontainsHeader(String name)
          Returns a boolean indicating whether the named response header has already been set.
 StringencodeRedirectUrl(String url)
          Deprecated. As of version 2.1, use encodeRedirectURL(String url) instead
 StringencodeRedirectURL(String url)
          Encodes the specified URL for use in the sendRedirect method or, if encoding is not needed, returns the URL unchanged.
 StringencodeUrl(String url)
          Deprecated. As of version 2.1, use encodeURL(String url) instead
 StringencodeURL(String url)
          Encodes the specified URL by including the session ID in it, or, if encoding is not needed, returns the URL unchanged.
 voidsendError(int sc)
          Sends an error response to the client using the specified status code and clearing the buffer.
 voidsendError(int sc,String msg)
          Sends an error response to the client using the specified status.
 voidsendRedirect(String location)
          Sends a temporary redirect response to the client using the specified redirect location URL.
 voidsetDateHeader(String name, long date)
          Sets a response header with the given name and date-value.
 voidsetHeader(String name,String value)
          Sets a response header with the given name and value.
 voidsetIntHeader(String name, int value)
          Sets a response header with the given name and integer value.
 voidsetStatus(int sc)
          Sets the status code for this response.
 voidsetStatus(int sc,String sm)
          Deprecated. As of version 2.1, due to ambiguous meaning of the message parameter. To set a status code usesetStatus(int), to send an error with a description usesendError(int, String). Sets the status code and message for this response.

重定向:

实现页面跳转的方式很多,如:设置响应头的refresh属性、html中设置http-equive的meta元数据(感觉和第一条实现原理一样)、通过js设置window.location="newUrl"、sendRedirect方法、使用<jsp:forward  page="url">标签。如果在servlet中跳转,则使用DispatcherServlet 接口的实例(通过request.getRequestDispatcher("xxx.jsp")获得,该接口有forward与include两个方法)。

区别forward标签与sendRedirect方法:
参考:浅谈JSP的Forward及sendRedirect区别

部分原文内容:

使用forward时,由于只是发送一次request请求,request设置的属性(setAttribute)依然能保留在下一个页面。

使用sendRedirect时,由于发送两次request请求,所以在下一个不能获取request属性。但可以通过重写URL的方式将内容传递过去。


这是个好网站:维基百科 HTTP header 参数列表

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值