jsp内置隐含对象和servlet常用类

本文介绍了HTTP协议中的会话管理机制,包括Cookie和Session的工作原理及其使用方法。详细解释了Cookie的创建、读取及设置有效期等功能,以及Session的生命周期管理和状态保持特性。

javax.servlet.http.Cookie(cookie)

 

Cookie(String name,String value)

int getMaxAge()

void setMaxAge(int expiry)  Sets the maximum age of the cookie in seconds

String getName()

String getValue()

void setValue(String newValue)

 

javax.servlet.http

   Interface HttpSession(session)

Object getAttribute(String name)

Enumeration getAttributeNames()

java.util.Enumeration getAttributeNames()

String getId()

void setAttribute(String name,Object value)

void setMaxInactiveInterval(int interval) in seconds, that the servlet container will keep this session open between client accesses

 说明:

打开浏览器请求一个页面,JSESSIONID(sessionId)会储存在Cookie随reponse一起返回客户端。用户点击该窗口中的超链接,再次请求中有如下部分:
Connection: Keep-Alive
Cookie: JSESSIONID=4D47D2E6D9A6DE536411C0536657B599

只有在同一个IE窗口中,sessionId才会相同。如果同时打开了两个IE窗口,虽然这两个窗口访问同一个页面,但是sessionId不相同。关闭IE窗口,再次访问,sessionId与之前的不同

 javax.servlet.http

    Interface HttpServletRequest(request)

Cookie[] getCookies()

String getHeader(String name)

Enumeration getHeaderNames()

Enumeration getHeaders()

String getMethod()

String getRequestURI()

String getRequestURL()

HttpSession getSession()

从ServletRequest继承来的方法:

Object getAttribute(String name)

Enumeration getAttributeNames()

String getParameter(String name)

Enumeration getParameterNames()

String[] getParameterValues()

void setAttribute(String name,Object o)

 

javax.servlet.http

 Interface HttpServletResponse(response)

void addCookie(Cookie cookie)

void sendRedirect(String location)

void setContentType(String type)for example, text/html; charset=ISO-8859-4.

 

javax.servlet

 Interface ServletContext(application)

Object getAttribute(String name)

Enumeration getAttributeNames()

void setAttribute(String name,Object o)

 

pageContext

 pageContext对象能够存取其他隐含对象。
1.pageContext对象存取其他隐含对象属性的方法,此时需要指定范围的参数。
ObjectgetAttribute(Stringname,intscope)
EnumerationgetAttributeNamesInScope(intscope)
voidremoveAttribute(Stringname,intscope)
voidsetAttribute(Stringname,Objectvalue,intscope)
范围参数有四个,分别代表四种范围:PAGE_SCOPE、REQUEST_SCOPE、SESSION_SCOPE、APPLICATION_SCOPE
2.PageContext对象取得其他隐含对象的方法
ExceptiongetException()回传目前网页的异常,不过此网页要为errorpage,
JspWritergetOut()回传目前网页的输出流,例如:out
ObjectgetPage()回传目前网页的Servlet实体(instance),例如:page
ServletRequestgetRequest()回传目前网页的请求,例如:request
ServletResponsegetResponse()回传目前网页的响应,例如:response
ServletConfiggetServletConfig()回传目前此网页的ServletConfig对象,例如:config
ServletContextgetServletContext()回传目前此网页的执行环境(context),例如:application
HttpSessiongetSession()回传和目前网页有联系的会话(session),例如:session
3.PageContext对象提供取得属性的方法
ObjectgetAttribute(Stringname,intscope)回传name属性,范围为scope的属性对象,回传类型为Object
EnumerationgetAttributeNamesInScope(intscope)回传所有属性范围为scope的属性名称,回传类型为Enumeration
intgetAttributesScope(Stringname)回传属性名称为name的属性范围
voidremoveAttribute(Stringname)移除属性名称为name的属性对象
voidremoveAttribute(Stringname,intscope)移除属性名称为name,范围为scope的属性对象
voidsetAttribute(Stringname,Objectvalue,intscope)指定属性对象的名称为name、值为value、范围为scope
ObjectfindAttribute(Stringname)寻找在所有范围中属性名称为name的属性对象

exception
若要使用exception对象时,必须在page指令中设定。%@pageisErrorPage="true"%才能使用。
exception提供的三个方法:
getMessage()
getLocalizedMessage()、
printStackTrace(newjava.io.PrintWriter(out))

page
page对象代表JSP本身,更准确地说它代表JSP被转译后的Servlet,它可以调用Servlet类所定义的方法。

application、session、request、page的作用范围由大到小。

page只在当前页面有效。

request在一次请求的全过程中有效。包括用户请求访问的当前web组件,以及,和当前web组建共享同一请求的其他web组件(比如jsp页面中include的,forward标记包含的)。存放在HttpServletRequest对象中。

      一次请求的全过程:指从服务器端代码(如jsp、Servlet)接受请求并响应开始,至响应完毕(无结果或将结果完全送回客户端并断开连接)的期间。

session是用户全局变量,在整个会话期间都有效。只要页面不关闭就一直有效(或者直到用户一直未活动导致会话过期,或调用HttpSession的invalidate()方法)。存放在HttpSession对象中。

application是程序全局变量,对每个用户每个页面都有效。存放在ServletContext对象中。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值