jsp学习--cookie的使用
2017-09-17 16:48
183 查看
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <% //两种创建方式 // Cookie cookie=new Cookie("uname","ricardo"); Cookie cookie=new Cookie("uname",""); //setValue设置cookie值 cookie.setValue("ricardo"); //setMaxAge设置cookie的有效期 cookie.setMaxAge(50); //保存到cookie当中 response.addCookie(cookie); //获取cookie Cookie[] cookies=request.getCookies(); String uname=null; if(cookies!=null){ for(int i=0;i<cookies.length;i++){ //getName获取cookie的名称 if("uname".equals(cookies[i].getName())){ //getValue获取cookie的值 uname=cookies[i].getValue(); } } } %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'ch07.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <%=uname %> </body> </html>
使用cookie存储登录信息的代码实例:
相关文章推荐
- JSP学习笔记(五)-----JSP中使用JavaBean
- 学习如何使用 Cookie 编程
- jsp实现cookie的使用
- Jsp&Servelet 学习笔记- 在Tomcat中的servlet.xml使用Context元素
- JSP设计模式基础:View Helper模式——学习如何使用View Helper模式使得Model数据适应表现层的需要(2)
- 学习如何使用 Cookie 编程
- JSP学习笔记(二)-----使用Tomcat运行JSP文件
- JSP设计模式基础:View Helper模式——学习如何使用View Helper模式使得Model数据适应表现层的需要(2)
- JSP设计模式基础:View Helper模式——学习如何使用View Helper模式使得Model数据适应表现层的需要(1)
- 有关Jsp中cookie的使用
- JSP Cookie 使用完全详解
- 在JSP中使用Cookie,以及操作中文的乱码解决方法
- JSP学习笔记——过滤器(二)使用
- JSP设计模式基础:View Helper模式——学习如何使用View Helper模式使得Model数据适应表现层的需要(3)
- jsp实现cookie的使用
- [转]学习如何使用 Cookie 编程
- jsp开发网站使用cookie的一点经验
- jsp系列学习:jsp与javaBean的使用,用javaBean来封装
- JSP学习笔记(四)-----JSP动作的使用
- jsp实现cookie的使用