token生成代码:
<% //生成一个唯一的串 String token = UUID.randomUUID().toString(); // 将这个唯一的串 存到 session 域对象中去 session.setAttribute("token_session", token); %>
验证token代码
<%@page import="java.util.UUID"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% //生成一个唯一的串 String token = UUID.randomUUID().toString(); // 将这个唯一的串 存到 session 域对象中去 session.setAttribute("token_session", token); %> <h3>客户信息添加的页面</h3> <form action="${pageContext.request.contextPath }/add" method="post"> <table> <tr> <td>姓名</td> <td> <input type="text" name="name"> </td> </tr> <tr> <td>性别</td> <td> <input type="radio" name="gender" value="male">男 <input type="radio" name="gender" value="female">女 </td> </tr> <tr> <td>生日</td> <td> <input type="text" name="birthday"> </td> </tr> <tr> <td>邮箱</td> <td> <input type="text" name="email"> </td> </tr> <tr> <td>手机</td> <td> <input type="text" name="cellphone"> </td> </tr> <tr> <td>爱好</td> <td> <input type="checkbox" name="preference" value="弹棉花">弹棉花 <input type="checkbox" name="preference" value="玩毛线">玩毛线 <input type="checkbox" name="preference" value="夜生活">夜生活 <input type="checkbox" name="preference" value="敲代码">敲代码 <input type="checkbox" name="preference" value="睡觉">睡觉 </td> </tr> <tr> <td>类型</td> <td> <select name="type"> <option value="普通">普通</option> <option value="VIP">VIP</option> <option value="超白金">超白金</option> </select> </td> </tr> <tr> <td>描述</td> <td> <textarea rows="5" cols="75" name="description"></textarea> <input type="hidden" name="token" value="${token_session }"> </td> </tr> <tr> <td colspan="2"> <input type="submit" value="添加"> </td> </tr> </table> </form> </body> </html>
0则评论给“防止表单重复提交[token机制]”