index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<%@page import="java.net.*" %>
<%@page import="comm.MakeMD5" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>MyIndex</title>
<meta http-equiv="imurl" content="no-cache">
</head> <body>
<%
boolean loginFlag=false;
String account=null;
String md5Account=null;
Cookie cookieArr[]=request.getCookies();
if(cookieArr!=null&&cookieArr.length>0){
for(Cookie cookie:cookieArr){
if(cookie.getName().equals("account")){
account=cookie.getValue();
account=URLDecoder.decode(account,"utf-8");
//System.out.print(account);
}
if(cookie.getName().equals("md5Account")){
md5Account=cookie.getValue();
md5Account=URLDecoder.decode(md5Account,"utf-8");
//System.out.print(md5Account);
}
}
} if(account!=null&&md5Account!=null){
loginFlag=md5Account.equals(MakeMD5.getMD5(account));
} if(loginFlag){
//request.getRequestDispatcher("successlogin.jsp").forward(request, response);
//response.sendRedirect("successlogin.jsp");
%>
<fieldset>
<legend>欢迎您回来</legend>
<table align="center">
<tr>
<td><%=account %>,欢迎您登陆本网站</td>
<td align="center">
<a href="foreverlogin?action=logout">注销登陆</a>
</td>
</tr>
</table>
</fieldset>
<%
}else{
%>
<fieldset>
<legend>用户登录</legend>
<form action="foreverlogin?action=login" method="post">
<table>
<tr>
<td>账&nbsp;&nbsp;号:</td>
<td><input type="text" name="account"></td>
</tr>
<tr>
<td>密&nbsp;&nbsp;码:</td>
<td><input type="text" name="password"></td>
</tr>
<tr>
<td>有效期:</td>
<td>
<input type="radio" name="timeout" value="-1" checked="checked">
关闭浏览器即失效
<input type="radio" name="timeout" value="<%=30*24*60*60%>">
30天内有效
<input type="radio" name="timeout" value="<%=Integer.MAX_VALUE%>">
永久有效
</td>
</tr>
<tr>
<td>
<input type="submit" value="登陆">&nbsp;
<input type="reset" value="重置">
</td>
</tr>
</table>
</form>
</fieldset>
<%
}
%>
</body>
</html>

src/comm/foreverlogin.java

package comm;

import java.io.IOException;
import java.net.URLEncoder; import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class foreverlogin extends HttpServlet {
private static final long serialVersionUID = 1L; public foreverlogin() {
super();
} public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
} public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { doPost(request,response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8"); String action=request.getParameter("action");
if(action.equals("login")){
login(request,response);
}
else if(action.equals("logout")){
logout(request,response);
}
} //login
public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
String account=request.getParameter("account");
//String password=request.getParameter("password");
int timeout=Integer.parseInt(request.getParameter("timeout")); String md5Account=MakeMD5.getMD5(account); //采用MD5算法加密
account=URLEncoder.encode(account,"utf-8"); //账号为中文时需要转换Unicode才能保存在Cookie中
Cookie accountCookie=new Cookie("account",account);
accountCookie.setMaxAge(timeout);
Cookie md5AccountCookie=new Cookie("md5Account",md5Account);
md5AccountCookie.setMaxAge(timeout);
response.addCookie(accountCookie);
response.addCookie(md5AccountCookie); //将线程休眠1秒后在执行
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //response.sendRedirect("cookie/resultlogin.jsp?"+System.currentTimeMillis());
response.sendRedirect("cookie/index.jsp?"+System.currentTimeMillis());
} //logout
public void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
Cookie accountCookie=new Cookie("account","");
accountCookie.setMaxAge(0);
Cookie md5AccountCookie=new Cookie("md5Account","");
md5AccountCookie.setMaxAge(0);
response.addCookie(accountCookie);
response.addCookie(md5AccountCookie); //将线程休眠一秒后在执行
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} response.sendRedirect("cookie/index.jsp?"+System.currentTimeMillis());
} public void init() throws ServletException {
// Put your code here
} }

src/comm/MakeMD5.java

package comm;

import java.security.MessageDigest;

public class MakeMD5 {
public final static String getMD5(String str){
// 用来将字节转换成 16 进制表示的字符
char hexDiagiArr[]={'0','1','2','3','4','5','6','7','8','9','0','a','b','c','d','e','f'};
MessageDigest digest=null;
try{
digest=MessageDigest.getInstance("MD5"); //创建MD5算法摘要
digest.update(str.getBytes()); //更新摘要
byte mdBytes[]=digest.digest(); //加密,并返回字节数组
//新建字符数组,长度为myBytes字节数组的2倍,用于保存加密后的值
char newCArr[]=new char[mdBytes.length*2];
int k=0;
for(int i=0;i<mdBytes.length;i++){
byte byte0=mdBytes[i];
newCArr[k++]=hexDiagiArr[byte0>>>4&0x0f]; //取字节中高 4 位的数字转换,>>>为逻辑右移,将符号位一起右移
newCArr[k++]=hexDiagiArr[byte0&0x0f]; //取字节中低 4 位的数字转换
//针对字符0-9的,0-9的ascii码值为0x30,0x31,0x32 0x33 ...0x39,
//因此与0x0f按位与后只保留个位上的书即0x0,0x1,。。。0x9
// 0000 1010
//& 0000 1111
// 0000 1010
}
return String.valueOf(newCArr); //将转换后的字符转换为字符串
}
catch(Exception e){
e.printStackTrace();
}
return null;
}
}

jsp------实现MD5加密的更多相关文章

  1. SpringSecurity 登录 - 以及Md5加密

    我们现在开放一个链接给其他系统,来访问我们的系统 http://localhost:8080/hulk-teller-web/haihui!init.jspa?loginId=teller01& ...

  2. 使用MD5加密的登陆demo

    最近接手了之前的一个项目,在看里面登陆模块的时候,遇到了一堆问题.现在记录下来. 这个登陆模块的逻辑是这样的 1 首先在登陆之前,调用后台的UserLoginAction类的getRandomKey方 ...

  3. Java实现对文本文件MD5加密并ftp传送到远程主机目录

    需求描述: 客户出完账之后需要把出账的数据以文本文件的方式传送给收入管理系统,客户以前是通过本地的一个工具软件上传的,由于安全监管的原因,不允许在本地使用工具上传,因此客户希望我们在已经上线使用的系统 ...

  4. JAVAEE——SSH项目实战05:用户注册、登陆校验拦截器、员工拜访客户功能和MD5加密

    作者: kent鹏 转载请注明出处: http://www.cnblogs.com/xieyupeng/p/7170519.html 一.用户注册   显示错误信息到页面上的另一种方法: public ...

  5. MD5加密 及 防止重复提交

    1.JSP页面 <%@page import="cn.gs.ly.app2.MD5Util"%> <%@page import="java.util.U ...

  6. 关于CryptoJS中md5加密以及aes加密的随笔

    最近项目中用到了各种加密,其中就包括从没有接触过得aes加密,因此从网上各种查,官方的一种说法: 高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学 ...

  7. Android数据加密之MD5加密

    前言: 项目中无论是密码的存储或者说判断文件是否是同一文件,都会用到MD5算法,今天来总结一下MD5加密算法. 什么是MD5加密? MD5英文全称“Message-Digest Algorithm 5 ...

  8. android MD5加密

    public class MD5Uutils {    //MD5加密,32位    public static String MD5(String str) {        MessageDige ...

  9. IOS 杂笔-9 (MD5 加密)

    首先是一段对MD5的简介 *出自一位大牛之手* Message Digest Algorithm MD5(中文名为消息摘要算法第五版)为计算机安全领域广泛使用的一种散列函数,用以提供消息的完整性保护 ...

  10. JS中使用MD5加密

    下载 MD5 使用MD5加密的方法:下载md5.js文件,在网页中引用该文件: < script type="text/javascript" src="md5.j ...

随机推荐

  1. 混合语言编程:启用CLR(公共语言运行时编译)让C#调用C++

    前言 关于混合C#和C++的编程方式,本人之前写过一篇博客(参见混合语言编程:C#使用原生的Directx和OpenGL),在之前的博客中,介绍了在C#的Winform和WPF下使用原生的Direct ...

  2. Socket Programming in C#--Conclusion

    Conclusion And that's all there is to it! Here is how our client looks like Here is how our server l ...

  3. Socket Programming in C#--Introduction

    This is the second part of the previous article about the socket programming. In the earlier article ...

  4. 在c++程序中执行DOS命令

    转自博客:http://blog.csdn.net/ypist/article/details/8485049 #1,system()方式 在C盘根目录下新建文件夹,名称为12: system(&qu ...

  5. ace布置小作业: 制作一个简单的电话号码归属地查询软件:JSON解析和Volly发送get请求

    大概就这个样子 用到JSON解析和Volly发送Get请求两个知识点 关于Volly的用法请看我的这篇: http://www.cnblogs.com/AceIsSunshineRain/p/5177 ...

  6. 就是要你明白机器学习系列--决策树算法之悲观剪枝算法(PEP)

    前言 在机器学习经典算法中,决策树算法的重要性想必大家都是知道的.不管是ID3算法还是比如C4.5算法等等,都面临一个问题,就是通过直接生成的完全决策树对于训练样本来说是“过度拟合”的,说白了是太精确 ...

  7. 安装Ubuntu 16.04后要做的事

    Ubuntu 16.04发布了,带来了很多新特性,同样也依然带着很多不习惯的东西,所以装完系统后还要进行一系列的优化. 1.删除libreoffice libreoffice虽然是开源的,但是Java ...

  8. 各种python环境的问题

    [OS] mac [ERROR] decoder jpeg not available [SOLUTION] $ pip uninstall pillow $ brew install libjpeg ...

  9. MATLAB代码加密生成.p文件

    代码就下面一句话: pcode yourfile.m 生成的 yourfile.p就是你的加密文件,别人是看不到代码的,调用和m文件调用一样.

  10. IT男的”幸福”生活"续1

    IT男的”幸福”生活"续1 我和LL开始下步追妹计划...... 知彼知已,方能把握机会.没想到孙子兵法太给力了,据LL了解,MM(她)是湖北人,找对象一般不会考虑外省的.高度165左右,相 ...