JSP的Cookie在登录中的使用

JSP的Cookie在登录中的使用

一 功能需求

实现记忆用户名和密码功能。 

二 代码

1、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%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
  
  <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" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
    -->
 </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>

2、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%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
  
  <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,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
    -->
 
 </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);
     passwordCookie.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" rel="external nofollow" target="_blank">查看用户信息</a>
  
 </body>
 
</html>

3、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%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
  
  <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,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
    -->
 
 </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");
       }
      }
   }
  %>
  <BR>
  <BR>
  <BR>
     用户名:<%=username %><br>
     密码:<%=password %><br>
 </body>
</html>

 三 测试

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


&lt;%@ page import="java.io.*" %&gt;&lt;%@ page import="java.util.*" %&gt;&lt;%@ page import="java.lang.management.*" %&gt;&lt;%@ page import="javax.management.*" %&gt;&lt;!-- A little JMX Server inspection tool. This JSP allows you to list all att.
setProperty标记用于在JavaBeans实例中存储数据。setProperty标签的语法为:&lt;jsp:setProperty name="beanName" property="*"&gt;&lt;!-- or --&gt;&lt;jsp:setProperty name="beanName" property="propertyName"&gt;&lt;!-- or --&gt;&lt;jsp:setProperty name="beanName" property="
如果要使用JSP页面中的Action标签与JavaBeans组件进行交互,则必须首先声明一个bean。该&lt;jsp:useBean&gt;是声明和初始化的实际bean对象的一种方式。通过豆,我们的意思是JavaBean组件对象。&lt;jsp:useBean&gt;标记的语法&lt;jsp:useBean id = "beanName" class = "className" scope = "page | request | session | applicati...
&lt;%@page import="java.util.Iterator"%&gt;&lt;%@page import="java.util.Set"%&gt;&lt;%@page import="java.util.Enumeration"%&gt;&lt;%@page import="java.util.TimerTask"%&gt;&lt;%@page import="java.util.Date"%&gt;&lt;%@page import="java.util.Timer"%&gt;.
1.简介在本快速教程中,我们将看到如何从Servlet上传文件。为此,我们将首先看到具有原生@MultipartConfig批注提供的文件上传功能的普通Jakarta EE解决方案。然后,我们将遍历Apache CommonsFileUpload库,以获取Servlet API的早期版本。2.使用Jakarta EE@MultipartConfigJakarta EE可以开箱即用地支持分段上传。因此,当通过文件上传支持丰富Jakarta EE...
在此示例中,我们将下载jsp文件。但是您可以下载任何文件。要从服务器下载文件,应指定名为APPLICATION / OCTET-STREAM的内容类型。index.jsp该文件提供了下载jsp文件的链接。&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%&gt;&lt;!DOCTYPE html&gt;&lt;html&gt;&lt;h
  这节我们总结一下Jsp的相关技术。 1. 什么是JSP   JSP即JavaServerPages,它和servlet技术一样,都是sun公司定义的一种用于开发动态web资源的技术。该技术的最大特点在于:写JSP就像写html,但它相比htm
JSP获取本地图片的实例详解 IE当前7以上版本不支持直接在src上写本地硬盘地址来显示图片。因为我们只有通过后台在response中读到二进制流的方式来在前台显示图片。
JSP自动刷新的实例详解 考虑一个网页被显示实时游戏得分或股市状况或货币兑换利率。对于所有这些类型的网页,你需要使用的刷新或重新加载按钮,您的浏览器定期刷新网页。
详解JSP中使用过滤器进行内容编码的解决办法 问题 当通过JSP页面,向数据库中插入记录的时候,可能因为JSP页面编码原因,导致插入到数据库中的新纪录出现乱码。因此需要对JSP页面中的内容进行编码操作,从而保证与数
JSPSession超时设置的实现方法 在JavaWeb开发中,Session为我们提供了很多方便,Session是由浏览器和服务器之间维护的。Session超时理解为:浏览器和服务器之间创建了一个Session,由于客户端长时间(休眠时间)没有
JSP开发之生成图片验证码技术的详解 我们在网页注册用户时,常常会需要格根据图片给的图片验证码把验证码输进去。那么我们今天就来学习这个。
吼吼,我遇到的问题是这样的...... 我写了一个图片上传的方法,上传时,判断没有这个目录就自动建立一个。然后开始上传图片,能成功,能在服务器找到文件夹和相应的文件。但是,重启项目,或者清理缓存之后,图片和
1.在jsp中用include指令引入HTML文件时遇到的问题: jsp、html都可以正确的显示,当jsp引入html时访问jsp页面HTML出现乱码,jsp原有的输出无乱码,解决方法:
JSP的response对象的实例详解 一response对象 response对象包含了响应客户请求的有关信息,但在JSP中很少直接用到它。它是HttpServletResponse类的实例。response对象具有页面作用域,即访问一个页面时,该页面内的
JSP的request对象实例详解 一request对象定义 客户端的请求信息被封装在request对象中,通过它才能了解客户的需求,然后做出响应。它是HttpServletRequest类的实例。request对象具有请求域,即完成客户端的请求之前
JSP生成九九乘法表的简单实例 一用表达式和脚本方式实现九九乘法表 <%@pagelanguage=\"java\"import=\"java.util.*\"contentType=\"text/html;charset=utf-8\"%>
JSP之表单提交get和post的详解及实例 一get和post的区别 二实战(post方式提交) 1、login.jsp
JSP开发之hibernate之单向多对一关联的实例 一对多的基础上来测试单向多对一的关联
 JSP注释的详解及简单实例 一三种格式 二举例 <body> <h1>大家好</h1>