1.MD5加密的工具类
public final class MD5Encode {
public static String getMD5(String source){
return getMD5(source.getBytes());
}
public static String getMD5(byte[] in) {
String result =null;
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
StringBuilder sbu =new StringBuilder();
byte[] out = md5.digest(in);
for (byte b : out) {
int o = b &0xFF;
if (o <16) {
sbu.append("0");
}
sbu.append(Integer.toHexString(o));
}
result = sbu.toString().toUpperCase();
}catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return result;
}
}
2.Controller类
@Controller
public class IndexController {
@RequestMapping("md5encode")
public String toIndex(){
return "encode";
}
@GetMapping("encode")
@ResponseBody
public String encode(HttpServletRequest request){
String s=request.getParameter("input");
String result= MD5Encode.getMD5(s);
return result;
}
}
3.jsp文件(encode.jsp)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form id="test">
<input id="input" name="input" type="text" >
<button id="encode" type="button" onclick="getencode()">加密</button>
<input id="result" name="result" type="text" >
</form>
<script src="/include/plugins/jQuery/jquery-2.2.3.min.js"></script>
<script type="text/javascript">
function getencode() {
$.ajax({
type:'get',
url:"encode?input="+$("#input").val(),
success:function (data){
$("#result").val(data.toString());
}
});
}
</script>
</body>
</html>