`
lxdyycg
  • 浏览: 22531 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

j2ee学习总结之数据访问范围

    博客分类:
  • j2ee
阅读更多
******Cookie对象:javax.servlet.http.Cookie
Cookie只能是文本内容,最长是4K;
1.创建Cookie对象:
new Cookie(String name,String value)
2.设置Cookie的有效时间
setMaxAge(int age)
3.将Cookie对象发送给客户端浏览器
HttpServletResponse对象调用:addCookie(Cookie cookie)
4.从客户端浏览器获取Cookie
HttpServletRequest的对象调用:getCookies() 返回Cookie[]数组

******应用对象ServletContext:
容器启动时在加载web应用的同时为每个web应用创建一个唯一的ServletContext对象,供该web应用中的所有组件所共享;
1.获取对象的方式:
1>HttpServlet
getServletContext()
2>ServletConfig
getServletConfig().getServletContext()
3>HttpSession
request.getSession().getServletContext()
2.常用的方法:
setAttribute("param_name","value")
getAttribute("param_name")
removeAttribute("param_name")
getAttributeNames()
--返回Enumeration
getInitParameter("param_name")
--在web.xml中设置<context-param><param-name>...</param-name><param-value>...</param-value></context-param>
--通常作为web应用中所有servlet共享的全局参数
getRealPath(url_path)
--用于得到url_path对应的物理磁盘的路径


******会话对象HttpSession:
web容器为每个用户创建一个HttpSession对象,并将标识该对象的ID发送给客户端的浏览器,浏览器在之后的请求中都会将该ID发送给服务器;
1.获取对象的方式:
1>HttpServletRequest
getSession()
2.设置对象无效的方法:
--invalidate()
--serMaxInactiveInterval(int seconds)
3.使用会话的方法:
getId()
getServletContext()
4.在HttpSession中共享数据的方法:
setAttribute(String name,Object object)
getAttribute(String name)
removeAttribute(String name)
getAttributeNames() Enumeration
5.保存HttpSession对象ID的方式:
1>存放在浏览器的Cookie中
--在客户端的浏览器禁用Cookie的时候该方法会失效
2>URL重写
--通过使用HttpServletResponse的encodeURL(String url)方法来实现对当前url的重写(即在url的后面拼接上参数jessionid)

******URL重写(URL rewriter)jsessionid必须是小写
方式1:<form action="CServlet;jsessionid=<%=session.getId()%>">
方式2:<form action='<%=response.encodeURL("CServlet")'>
方式3(tomcat不支持):<input type="hidden" name="jsessionid" value="<%=session.getId()%>" /></form>


******请求对象HttpServletRequest
1.获取表单中数据的方法:
getParameter(String name)

2.在请求中共享数据的方法:
setAttribute(String naem,Object object)
getAttribute(String name)
removeAttribute(String name)
getAttributeNames() Enumeration

******页面对象pageContext:
1.pageContext作为JSP中的默认的对象之一,可以直接在JSP中使用;
2.方法:
setAttribute(String name,Object object)
getAttribute(String name)
removeAttribute(String name)
getAttributeNames()
--返回Enumeration
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics