博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jsp------实现MD5加密
阅读量:5288 次
发布时间:2019-06-14

本文共 6742 字,大约阅读时间需要 22 分钟。

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" %>MyIndex
<% 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"); %>
欢迎您回来
<%=account %>,欢迎您登陆本网站 注销登陆
<% }else{ %>
用户登录
账  号:
密  码:
有效期: 关闭浏览器即失效 30天内有效 永久有效
 
<% } %>

 

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
>>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; }}

 

转载于:https://www.cnblogs.com/tianhengblogs/p/5557638.html

你可能感兴趣的文章
ubuntu 11.04侧边栏怎么添加图标
查看>>
DotNetBar For Windows Forms 12.5.0.2 官方原版及注册
查看>>
修改Oracle 表空间名称 tablespace name
查看>>
12枚硬币问题
查看>>
Python+Django+Ansible Playbook自动化运维项目实战(二)
查看>>
www与m站间的转换
查看>>
mxnet(gluon) 实现DQN简单小例子
查看>>
像MIUI一样做Zabbix二次开发(7)——问答
查看>>
3.6节练习
查看>>
PRML-1.2.4 高斯分布
查看>>
lua
查看>>
Logstash 基础入门
查看>>
安装VS2012以后打开office 2007 的任何程序都跳出VS2012配置界面的解决方案
查看>>
.NET Core 时代已经到了,你准备好了吗
查看>>
什么叫PV,UV,PR值
查看>>
Linux文件管理下
查看>>
SQL Server 事务隔离级别详解
查看>>
第9章 前端开发 口述题
查看>>
创建触发器
查看>>
django
查看>>