关于在页面得到的servlet验证码总是上一次保存在session中的
1. 页面
- <%@page import="java.util.Date"%>
- <%@ 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>
- <%
- String context = request.getContextPath();
- %>
- <script type="text/javascript">
- function reloadIdentifyCode() {
- document.getElementById("btn").disable = true;
- document.getElementById("code").src = "<%=context %>/generateidentifycode?time=" + new Date().getTime();
- }
- function isIdentifyCodeRight() {
- //must quote the jsp sentence
- var input = document.getElementById("identify_code").value;
- <%
- String identifyCode = (String) session.getAttribute("identifyCode");
- %>
- alert("You input:" + input + ", the answer is:" + "<%=identifyCode %>");
- if(input == "<%=identifyCode %>") {
- return true;
- }
- else {
- return false;
- }
- }
- </script>
- </head>
- <body>
- <div id="" align="center">
- <form action="<%=context %>/user/findpwd" method="post">
- <table>
- <tr>
- <td>用户名:</td>
- <td><input id="userName" type="text" value="Email/手机号/用户名" name="userName"></td>
- </tr>
- <tr>
- <td>验证码:</td>
- <td><input id="identify_code" type="text" maxlength="6" value="" name="identifyCode">
- <img id="code" alt="" src="<%=context %>/generateidentifycode?time=<%=new Date().getTime() %>">
- <input id="btn" type="button" value="换张图片" onclick="reloadIdentifyCode()"></td>
- </tr>
- <tr>
- <td colspan="2"><input type="submit" value="发送验证码到邮箱"></td>
- </tr>
- </table>
- </form>
- </div>
- </body>
- </html>
<%@page import="java.util.Date"%>
<%@ 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>
<%
String context = request.getContextPath();
%>
<script type="text/javascript">
function reloadIdentifyCode() {
document.getElementById("btn").disable = true;
document.getElementById("code").src = "<%=context %>/generateidentifycode?time=" + new Date().getTime();
}
function isIdentifyCodeRight() {
//must quote the jsp sentence
var input = document.getElementById("identify_code").value;
<%
String identifyCode = (String) session.getAttribute("identifyCode");
%>
alert("You input:" + input + ", the answer is:" + "<%=identifyCode %>");
if(input == "<%=identifyCode %>") {
return true;
}
else {
return false;
}
}
</script>
</head>
<body>
<div id="" align="center">
<form action="<%=context %>/user/findpwd" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input id="userName" type="text" value="Email/手机号/用户名" name="userName"></td>
</tr>
<tr>
<td>验证码:</td>
<td><input id="identify_code" type="text" maxlength="6" value="" name="identifyCode">
<img id="code" alt="" src="<%=context %>/generateidentifycode?time=<%=new Date().getTime() %>">
<input id="btn" type="button" value="换张图片" onclick="reloadIdentifyCode()"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="发送验证码到邮箱"></td>
</tr>
</table>
</form>
</div>
</body>
</html>
2. 中间servlet
- package com.jesse.onlineshop.servlet;
- import java.io.IOException;
- import java.util.Date;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import com.jesse.onlineshop.bean.User;
- import com.jesse.onlineshop.exception.DaoException;
- import com.jesse.onlineshop.service.UserService;
- import com.jesse.onlineshop.service.impl.UserServiceImpl;
- /**
- * 因为session先于验证码图片加载,在jsp页面通过session得到的验证码总是上一次的,
- * 所以,这里借助Ajax通过异步机制来比较用户输入的验证码和session中保存的验证码是 否一致来达到验证的目的
- * @author Administrator
- *
- */
- public class FindPassWordServlet extends HttpServlet {
- private static final long serialVersionUID = 7331068570820532059L;
- private User user;
- private UserService userService = new UserServiceImpl();
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException {
- String input = req.getParameter("identifyCode"); //获取用户输入的code
- String answer = (String) req.getSession(false).getAttribute( //获取session中保存的code
- "identifyCode");
- if (!input.equalsIgnoreCase(answer)) {
- req.getRequestDispatcher("/user/wrongcode.jsp").forward(req, resp);
- } else {
- String userName = req.getParameter("userName");
- String regex = "[0-9]{11}";
- Pattern pattern = Pattern.compile(regex);
- Matcher matcher = pattern.matcher(userName);
- if (matcher.matches()) {
- } else if (userName.contains("@")) {
- try {
- user = userService.getUserByEmail(userName);
- } catch (DaoException e) {
- e.printStackTrace();
- }
- } else {
- try {
- user = userService.getUserByName(userName);
- } catch (DaoException e) {
- e.printStackTrace();
- }
- }
- String email = user.getEmail();
- userName = user.getUserName();
- String url = req.getContextPath() + "/confidential/user/changepwd?user="+userName+"&&time=" + new Date().getTime();
- try {
- userService.addChangePwdReqRecord(userName);
- } catch (DaoException e) {
- e.printStackTrace();
- throw new ServletException(e.getMessage());
- }
- userService.sendChangePassWordRequest(email, url);
- req.getRequestDispatcher("findpwdsuccess.jsp").forward(req, resp);
- }
- }
- }
package com.jesse.onlineshop.servlet; import java.io.IOException;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.jesse.onlineshop.bean.User;
import com.jesse.onlineshop.exception.DaoException;
import com.jesse.onlineshop.service.UserService;
import com.jesse.onlineshop.service.impl.UserServiceImpl; /**
- 因为session先于验证码图片加载,在jsp页面通过session得到的验证码总是上一次的,
- 所以,这里借助Ajax通过异步机制来比较用户输入的验证码和session中保存的验证码是 否一致来达到验证的目的
- @author Administrator
public class FindPassWordServlet extends HttpServlet {
private static final long serialVersionUID = 7331068570820532059L;
private User user;
private UserService userService = new UserServiceImpl();
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String input = req.getParameter("identifyCode"); //获取用户输入的code
String answer = (String) req.getSession(false).getAttribute( //获取session中保存的code
"identifyCode");
if (!input.equalsIgnoreCase(answer)) {
req.getRequestDispatcher("/user/wrongcode.jsp").forward(req, resp);
} else {
String userName = req.getParameter("userName");
String regex = "[0-9]{11}";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(userName);
if (matcher.matches()) {
} else if (userName.contains("@")) {
try {
user = userService.getUserByEmail(userName);
} catch (DaoException e) {
e.printStackTrace();
}
} else {
try {
user = userService.getUserByName(userName);
} catch (DaoException e) {
e.printStackTrace();
}
}
String email = user.getEmail();
userName = user.getUserName();
String url = req.getContextPath() + "/confidential/user/changepwd?user="+userName+"&&time=" + new Date().getTime();
try {
userService.addChangePwdReqRecord(userName);
} catch (DaoException e) {
e.printStackTrace();
throw new ServletException(e.getMessage());
}
userService.sendChangePassWordRequest(email, url);
req.getRequestDispatcher("findpwdsuccess.jsp").forward(req, resp);
}
}
}
关于在页面得到的servlet验证码总是上一次保存在session中的的更多相关文章
- //可以不保存在session中, 并且前面我保存在request,这里session也可以获取 chain.doFilter(request, response); //只有登录名不为空时放行,防止直接登录 成功的页面
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOE ...
- ASP.NET上传文件,已经上传的大小保存在session中,在另一个页面中读取session的值不行
想自己做个ASP.NET上传文件时显示进度条的, 按照自己的想法,其实也就是显示每次已经上传的字节,从网上找到一个方法是能够把文件变成流以后再慢慢写入的,我在那个循环写入的时候每循环一次都把已经上传的 ...
- Jsp+servlet 验证码案例
昨晚在csdn看到一位前辈写一个ajax+servlet+jsp验证.顿时心血来潮,在阅读前辈的代码下我亲手体验一下,做了一个验证码生成工具类.以供大家做个參考. 1:加入VeriyCodeUtils ...
- Struts2自定义结果视图(servlet验证码)
1.编写一个类实现com.opensymphony.xwork2.Result,或者继承org.apache.struts2.dispatcher.StrutsResultSupport 2.自定义的 ...
- Tp验证码:$Verify = new \Think\Verify(); $Verify->entry(n);【参数n,页面有多个验证码时用】
一.验证码参数:(中文字符集和英文字符集在父类里面都可以取到,可修改) //1.生成验证码 $Verify = new \Think\Verify(); $Verify->entry(n);[参 ...
- jsp页面转发到servlet
一个简单的例子来了解一下jsp页面转发到servlet的过程,环境 eclipse.tomcat 1.工程目录结构如下 2.各部分代码如下 1>index.jsp <%@ page lan ...
- 使用session和cookie实现用户登录:一个登录页面,一个servlet,一个登录成功页面
文件目录 1.登录页面 <%@ page language="java" contentType="text/html; charset=utf-8" p ...
- 关于使用struts2时子窗体页面跳转后在父窗体打开的问题以及Session过期后的页面跳转问题
问题1:传统的系统界面,iframe了三个页面,上,左,右,用户点击注销的按钮在上面得top.jsp里面,方法:<a href="../adminAction/admin_logout ...
- Servlet实现文件上传,可多文件上传
一.Servlet实现文件上传,需要添加第三方提供的jar包 接着把这两个jar包放到 lib文件夹下: 二: 文件上传的表单提交方式必须是POST方式, 编码类型:enctype="mul ...
随机推荐
- shell学习笔记1: shell 中的变量与常见符号使用方法
变量 声明即用 a=2 b="123" 调用 ${varName}或者 $varName echo $b echo ${a} 常见变量 $?:判断上一个语句是否成功 $0:执行脚本 ...
- iOS之CGcontext.h方法和属性简介
/* CoreGraphics - CGContext.h Copyright (c) 2000-2012 Apple Inc. All rights reserved. */ #ifndef CGC ...
- iOS程序两中启动图方式和一些坑LaunchImage 和 Assets.xcassets(Images.xcassets)
一.通过LaunchScreen.storyboard 作启动图 1>在LaunchScreen.storyboard中拖拽一个imageView放上启动图片 注意:记得勾选右边的 User a ...
- DataGridView绑定DataTable的正确姿势
1. 将DataTable 绑定到BindingSource 2. 将BindingSource绑定到DataGridView 3. DataGridView修改完要从Datatable取值时,同步过 ...
- webservice技术--服务器端
1.webservice实现单点登录具体逻辑为 ①软通web端作为客户端,请求wi社区后台,进行登录请求 ②wi社区后台验证t,核实无误后,走登录逻辑,直接进入欢迎页 ③如果有错误,封装错误xml,返 ...
- poi 3669 meteor shower (bfs)
题目链接:http://poj.org/problem?id=3669 很基础的一道bfs的题,然而,我却mle了好多次,并且第二天才发现错在了哪里_(:з)∠)_ 写bfs或者dfs一定要记得对走过 ...
- JavaScript性能优化篇js优化
JavaScript性能优化篇js优化 随着Ajax越来越普遍,Ajax引用的规模越来越大,Javascript代码的性能越来越显得重要,我想这就是一个很典型的例子,上面那段代码因为会被频繁使用, ...
- Python学习day15-函数进阶(3)
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...
- mybatis和java一些知识记录
<where> <if test="userName != null and userName != ''"> and user_name like con ...
- 新启vue_cli项目+引入Element
[1]安装vue_cli vue init webpack 项目名字 [2]安装Element-UI cnpm install element-ui -S //写入dependencies cnpm ...