创建cookie对象调用它的构造方法,构造方法接收两个参数,是一种键值对描述:键:cookie对象的字符串描述;值:要保存的cookie对象。

Value:字符串

The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path

login.jsp:用户登录界面;

dologin.jsp:处理用户登录请求的页面,要保存cookie,页面下方有一个超链接,链接到查看用户信息的页面;

users.jsp:查看用户信息的页面

【python】python新手必碰到的问题---encode与decode,中文乱码[转]

login.jsp:

<%@ page language="java" import="java.util.*,java.net.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><base href="<%=basePath%>"><title>My JSP 'index.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><h1>用户登录</h1><hr><% request.setCharacterEncoding("utf-8");String username="";String password = "";Cookie[] cookies = request.getCookies();if(cookies!=null&&cookies.length>0){for(Cookie c:cookies){if(c.getName().equals("username")){username =  URLDecoder.decode(c.getValue(),"utf-8");}if(c.getName().equals("password")){password =  URLDecoder.decode(c.getValue(),"utf-8");}}}%><form name="loginForm" action="dologin.jsp" method="post"><table><tr><td>用户名:</td><td><input type="text" name="username" value="<%=username %>"/></td></tr><tr><td>密码:</td><td><input type="password" name="password" value="<%=password %>" /></td></tr><tr><td colspan="2"><input type="checkbox" name="isUseCookie" checked="checked"/>十天内记住我的登录状态</td></tr><tr><td colspan="2" align="center"><input type="submit" value="登录"/><input type="reset" value="取消"/></td></tr></table></form></body>
</html>

dologin.jsp:

<%@ page language="java" import="java.util.*,java.net.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><base href="<%=basePath%>"><title>My JSP 'dologin.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><h1>登录成功</h1><hr><br><br><br><% request.setCharacterEncoding("utf-8");//首先判断用户是否选择了记住登录状态String[] isUseCookies = request.getParameterValues("isUseCookie");if(isUseCookies!=null&&isUseCookies.length>0){//把用户名和密码保存在Cookie对象里面String username = URLEncoder.encode(request.getParameter("username"),"utf-8");//使用URLEncoder解决无法在Cookie当中保存中文字符串问题String password = URLEncoder.encode(request.getParameter("password"),"utf-8");Cookie usernameCookie = new Cookie("username",username);Cookie passwordCookie = new Cookie("password",password);usernameCookie.setMaxAge(864000);//保存的单位是spasswordCookie.setMaxAge(864000);//设置最大生存期限为10天response.addCookie(usernameCookie);response.addCookie(passwordCookie);}else{Cookie[] cookies = request.getCookies();if(cookies!=null&&cookies.length>0){for(Cookie c:cookies){if(c.getName().equals("username")||c.getName().equals("password")){c.setMaxAge(0); //设置Cookie失效response.addCookie(c); //重新保存。}}}}%><a href="users.jsp" target="_blank">查看用户信息</a></body>
</html>

users.jsp:

<%@ page language="java" import="java.util.*,java.net.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><base href="<%=basePath%>"><title>My JSP 'users.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><h1>用户信息</h1><hr><% request.setCharacterEncoding("utf-8");String username="";String password = "";Cookie[] cookies = request.getCookies();if(cookies!=null&&cookies.length>0){for(Cookie c:cookies){if(c.getName().equals("username")){username = URLDecoder.decode(c.getValue(),"utf-8");}if(c.getName().equals("password")){password = URLDecoder.decode(c.getValue(),"utf-8");}}}%><BR><BR><BR>用户名:<%=username %><br>密码:<%=password %><br></body>
</html>

cookie保存比方说浏览记录、访问习惯、关注的爱好和兴趣等。

session安全性比cookie高。

观看慕课老师milanlover视频JSP状态管理+cookie相关推荐

  1. 观看慕课老师milanlover视频JSP内置对象+getpost+request+response+请求转发与请求重定向

    out是内置对象,不用new来创建. 把饭盛到碗里,碗相当于程序中的缓冲区. <%@ page language="java" import="java.util. ...

  2. 观看慕课老师milanlover视频JSP页面生命周期+分别通过脚本和表达式实现九九乘法表

    但是多线程要注意临界资源的共享和保护问题. workplace地址: work地址: 新建项目: 插播一条编译错误: .java:说明有jsp编译生成的字节码文件. 大体上jsp页面的生命周期:Ini ...

  3. 观看慕课老师milanlover视频JSP基础语法+注释+脚本+声明+表达式

    JSP可理解为Java服务器端页面:是一种动态网站开发技术的标准,同时也是JavaEE的参考标准. v <%@ page language="java" import=&qu ...

  4. 观看慕课老师milanlover视频JavaBean+Jsp动作元素+page/request/session/application作用域范围+Model1

    Javabeans类写在src包里. 建一个实体类的po包,建一个用户类. 使用普通方式创建javabean实例: 新建Users.java: package com.po; /*用户类*/ publ ...

  5. 观看慕课老师milanlover视频Tomcat装在Servlet的三种情况+Servlet获取表单数据+Servlet相对路径

    在src新建TestServlet1: package servlet;import java.io.IOException; import java.io.PrintWriter;import ja ...

  6. 观看慕课老师milanlover视频用servlet获取初始化参数+MVC

    新建Index.jsp: <%@ page language="java" import="java.util.*" contentType=" ...

  7. 观看慕课老师milanlover视频Servlet+手工编写Servlet+用Eclipse编写Servlet+Servlet执行流程和生命周期

    eclipse 配置JDK HTML <a> 标签的 href 属性 新建MyFirstServletDemo项目: 新建index.jsp: <%@ page language=& ...

  8. 观看慕课老师milanlover视频include指令+include动作+forward动作+param动作

    format()方法 使用inclue指令包含了一个date页面,在这个页面上显示了 被包含页面的内容. 新建date.jsp: <%@ page language="java&quo ...

  9. JSP→Javabean简介设计原则、JSP动作、Javabean三个动作、Javabean四个作用域范围、Model1简介弊端、JSP状态管理、include动作指令、forword动作、param

    Javabean简介设计原则 JSP动作 useBean动作 setProperty动作 getProperty动作 Javabean的四个作用域范围 Model1简介弊端 JSP状态管理 Cooki ...

最新文章

  1. 计算机二级函数知识,2017年全国计算机二级考试MS Office高级应用知识点:INDIRECT函数...
  2. (一)七种AOP实现方法
  3. 解决Android Stadio 导入Android 项目,没有可运行的Module
  4. Content Provider之一大菊观
  5. 【转】android 中如何限制 EditText 最大输入字符数
  6. android sdk 封装html5,Android平台以WebView方式集成HTML5+SDK方法
  7. 惠普打印机136w硒鼓芯片怎么清零_关于惠普彩激升级后无法识别硒鼓的处理方案...
  8. 【Flink】Flink Kafka 消费卡死 消费组卡死 topic无写入 实际有数据 topic正常
  9. 苹果留给 iOS 开发者的时间不多了:30 天内必须更新旧版本
  10. 密钥库证书的SHA-1指纹
  11. 【Unity】4.5 树木创建器
  12. DWM1000 收发RXLED TXLED控制代码修改
  13. 命令解析optparse
  14. 用计算机函数,信息技术应用 用计算机画函数图象教案设计(一等奖)
  15. Vmware报错 This product may not be installed on a computer that has Microsoft HyperV installed. 解决
  16. 今日头条的企业服务产品线
  17. stc15w404as引脚图_STC15W408AS系列
  18. 计算机领域,客观事物的属性,客观事物
  19. Pandas DataFrame 使用技巧
  20. 理科爱好者杂志理科爱好者杂志社理科爱好者编辑部2022年第3期目录

热门文章

  1. 单细胞分析实录(15): 基于monocle2的拟时序分析
  2. 2023年湖北建设厅七大员八大员报名怎么收费呢?
  3. android三维地图,图吧导航(com.mapbar.android.mapbarmap) - 10.3.3.64c0967 - 应用 - 酷安
  4. Dynamic-IPSec
  5. Vj程序设计作业H7
  6. Mars的心路历程-失望
  7. 自媒体账号被扣分限流了怎么办?继续更新还是注销?3招教你解决
  8. Bokeh 手把手安装教程
  9. 你会对老板说这十句傻话吗
  10. 16句微博上的经典语录