JSP内置9大对象学习总结(二)

来源:互联网 发布:安特数据 编辑:程序博客网 时间:2024/05/25 23:59

 

二:response对象
response对象是javax.servlet.http.HttpServletResponse类型的对象,代表的是客户端的响应,主要是将JSP容器处理过的对象传回到客户端。response对象只在本页面内有效。
response对象常用方法列表,具体详见J2EE_API,下载地址http://download.csdn.net/source/2921112
void
setHeader(String name, String value)
 Sets a response header with the given name and value.
void
addHeader(String name, String value)
 Adds a response header with the given name and value.
void
addCookie(Cookie cookie)
 Adds the specified cookie to the response.
void
sendError(int sc, String msg)
 Sends an error response to the client using the specified status.
void
sendRedirect(String location)
 Sends a temporary redirect response to the client using the specified redirect location URL.
ServletOutputStream
getOutputStream()
 Returns a
ServletOutputStream suitable for writing binary data in the response.
void
setContentType(String type)
 Sets the content type of the response being sent to the client, if the response has not
been committed yet.
说明:setContentType(String type)这个方法其实是比较重要的,当我们做文件下载时,我们需要向客户端通知我们发送的文件的类型,这样客户端就可以选择是下载还是打开了。否则客户端一律打开就会出现乱码现象。Type的种类在百度百科中可以搜的到http://baike.baidu.com/view/1547292.htm
1.重定向网页
进行重定向后,request中的属性全部消失,进入一个新的request对象的作用域。
例如:request.sendRedirect(“http://student.csdn.net/space.php?uid=209919&do=blog&id=53044”);
2.处理HTTP头
最近在做文件共享的一个东西,整了好久才弄出来。通过setHeader(String name, String value)方法可以设置HTTP文件头。例如:response.setHeader( "Content-disposition", "attachment;filename= "+fileName);
设置响应类型:response.setContentType("application/x-download");
文件的上传与下载:在这一块我自己认为是比较麻烦的。目前要么是文件下载时出现中文文件名字乱码,要么就是服务器稍微动一下会出现找不到文件的现象。
3.设置输出缓冲区
通常情况下,服务器要输出到客户端的内容都是先输出到一个缓冲区内,然后再发往客户端,当出现以下三种情况时,会把缓冲区的内容直接写到客户端。
①  缓冲区已满;
②  缓冲区未满,但JSP页面的输出信息已经全部写到了缓冲区;
③  在JSP页面中调用了flushBuffer()方法或者out对象的flush()方法。
通过response.setBufferSize()方法可以设置缓冲区的大小。如果设置为0则不缓冲。
三:session对象
session对象是由服务器端自动创建的与用户请求相关的对象。服务器会为每个访问者生成一个session对象。session对象内部使用Map类来保存对象,也就是以键值(key-value)对的形式来保存数据,其中可以保存的类型是Object类型的,java中Object类是根基类,所以我们可以保存java中的任何类型的数据。
我目前做的是用于记录在线用户的登录账号,页面验证,购物车相关功能,或者记录浏览的内容等,至于是否合理本人正在学习,暂时未知。另外还有可以记录用户信息的Cookie,和同学争论(不是据理力争^_^)过,本人认为Cookie在登录验证方面是不太安全的,对于安全性比较高的应用,不应该记录在客户端,我看过Cookie里面的内容,应该是加过密的,但是本人认为这种加密对于安全级别比较高的应用是无济于事的,还是可以通过伪造cookie的方法来盗取账号的。
session对象概要及其主要关键点:
  ·   HTTP是无状态(stateless)协议;
  ·   Web Server 对每一个客户端请求都没有历史记忆;
  ·   Session用来保存客户端状态信息;
  ·   Web Server 写入;存于客户端;
  ·   客户端的每次访问都把上次的session记录传递给Web Server
  ·   Web Server读取客户端提交的session来获取客户端的状态信息;
session对象常用方法列表,具体详见J2EE_API,下载地址http://download.csdn.net/source/2921112
Object
getAttribute(String name)
 Returns the object bound with the specified name in this session, or
null if no object is bound under the name.
long
getCreationTime()
 Returns the time when this session was created, measured in milliseconds since midnight January 1, 1970 GMT.
String
getId()
 Returns a string containing the unique identifier assigned to this session.
void
setAttribute(String name, Object value)
 Binds an object to this session, using the name specified.
void
invalidate()
 Invalidates this session then unbinds any objects bound to it.
void
setMaxInactiveInterval(int interval)
 Specifies the time, in seconds, between client requests before the servlet container will invalidate this session.
void
removeAttribute(String name)
 Removes the object bound with the specified name from this session.
1.获取session对象的方法
 1)使用session关键字直接引用session对象
2)使用request对象的getSession()方法
 3)使用PageContext对象的getSession()方法
由于session对象是JSP页面的内置对象,因此默认状态下是不需要我们创建的,或者不能“被”创建的。
2.创建及获取session信息
a.jsp页面中我们用来创建一个session,并打印其内容。
<%
         String sessionMsg = “this is a session”;
         Session.setAttribute(“msg”,”sessionMsg”);
         out.print(“保存在session中的对象为”+sessionMsg);
%>
在b.jsp中我们获取session对象中保存的内容。
<%
         String msg = (String)session.getAttribute(“message”);
         out.print(“保存在session中的对象为”+msg);
%>
3.从会话中移除制定的对象
通过key来移除valuesession.removeAttribute(“message”);合理使用session,合理释放资源。
4.销毁session对象
我们经常遇到的注销,这样可以减轻服务器的负担,当该用户不需要和服务器进行交互时可以注销,销毁session对象。session.invalidate()。
5.会话超时管理
默认状态下session对象中的内容自上次不在交互开始延迟30分钟。
我们可以通过session.setMaxInactiveInterval(int interval)方法设置的更短(安全级别比较高),或者更长,该方法中的参数的单位是秒。
其它管理会话生命周期的方法:
    getLastAccesssedTime();     返回客户端最后一次与会话相关联的请求时间。
    getMaxInactiveInterval();   以秒为单位返回一个会话内两个请求时间的最大时间间隔
6.session应用
用于验证登录,一般是单独写个checkLogin.jsp每个页面都包含该文件,用于进行验证。
login.jsp
<%
User u= new User(“userName”,”pwd”);
Session.setAttribute(“curUser”,(User)u );
%>
 
checkLogin.jsp
<%
User curUser = (Member)session.getAttribute("curUser");
if(curUser == null) {
           response.sendRedirect(request.getContextPath()+"/index.jsp");
}
%>
Session可以保存临时会话信息,可以做购物车等。
原创粉丝点击