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

阅读: 评论:0

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

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&#Parameter("age");
//下面一条可能报空指针错误
int age=Interger.parseInt("userAge");

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

String originalStr&#Parameter("someText");
//转换编码再显示
byte[] b&#Bytes("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 接口的实例(通过RequestDispatcher("xxx.jsp")获得,该接口有forward与include两个方法)。

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

部分原文内容:

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

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


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

本文发布于:2024-02-03 08:18:52,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170691953049796.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:重定向   对象   JSP   Response   Request
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23