cookie,session和jsp
Cookie
如何获得:Cookie[] cookies = request.getCookies();
如何向客户端写cookie:response.addCookie(Coookie cookie)
如何创建cookie:Cookie cookie = new Cookie(String name,String value);
获得名字:cookie.getName()
获得值:cookie.getValue();
设置路径:cookie.setPath(String path);
设置有效期:cookie.setMaxAge(int age) 单位是:秒
作用范围:默认是一次会话,关闭浏览器就销毁
关于cookie存储中文的问题:
往回写cookie:
String value = URLEncoder.encode("老谭", "UTF-8");//"老谭"--fadsf%%33
Cookie cookie = new Cookie("aa",value);
response.addCookie(cookie);
获得:
Cookie [] cookies = request.getCookies();
Cookie aa= CookieUtils.findCookie(cookies, "aa");
if(aa!=null){
String value = aa.getValue();//fadsf%%33--"老谭"
String a = URLDecoder.decode(value, "utf-8");
}Session
如何获得:HttpSession session = request.getSession();
作为域对象存取数据
session.setAttribute(String name,Object value);Object vlaue = session.getAttribute(String name);removeAttribute(String name);
范围:一次会话(根本原因:存SesssionID的cookie默认是会话级别的)
配置sessionion过期时间:web.xml
<session-config>
<session-timeout>5</session-timeout> <!--单位是分钟-->
</session-config>
JSP三大指令:
page:
全局错误友好页面的配置
<error-page>
<error-code>404</error-code>
<location>/404.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/500.jsp</location>
</error-page>
include: <%@ include file="" %> 静态包含
taglib:
九大内置对象(面试点三):
request HttpServetRequest
response HttpServetResponse
session HttpSession
application ServletContext
page Object
out JspWriter
pageContext PageContext
config ServletConfig
exception Throwable
技能 拔高点:pageContext:
1.获取其他8个内置对象:getXXX()方法
2.可以向四个域中存取数据:
pageContext.setAttribute("pname", "pvalue", PageContext.PAGE_SCOPE);
pageContext.setAttribute("rname", "rvalue", PageContext.REQUEST_SCOPE);
pageContext.setAttribute("sname", "svalue", PageContext.SESSION_SCOPE);
pageContext.setAttribute("aname", "avalue", PageContext.APPLICATION_SCOPE);
<%= pageContext.getAttribute("pname", PageContext.PAGE_SCOPE) %>
<%= pageContext.getAttribute("rname", PageContext.REQUEST_SCOPE) %>
<%= pageContext.getAttribute("sname", PageContext.SESSION_SCOPE) %>
<%= pageContext.getAttribute("aname", PageContext.APPLICATION_SCOPE) %>
动作标签:
静态包含和动态包含的区别(面试点四)
静态包含:相当于源代码的拷贝,只会翻译成一个Java类,有一个执行结果
动态包含:各自分别去翻译,各自执行,最终包含的是执行的结果