关于在页面得到的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 ...
随机推荐
- 流程控制&&函数
1.if 条件语句 if 判断条件: 执行语句…… elif 判断条件: 执行语句…… else: 执行语句…… 2.for 循环 ''' for 后跟变量名,in 后跟序列,注意加冒号 for 循环 ...
- BCB怎么调用DLL中的函数
推荐你看<BCB编写DLL终极手册>这篇文章如下片段:二. 静态调用 DLL使用 $BCB path\Bin\implib.exe 生成 Lib 文件,加入到工程文件中将该文件拷贝到当前目 ...
- Mysql修改表备注, 列信息
1.添加表和字段的注释 创建数据表的同时,给表和字段添加注释 -- 创建用户信息表 CREATE TABLE tb_user ( id INT AUTO_INCREMENT PRIMARY KEY C ...
- Windows的进程间通信
对于任何一个现代的操作系统,进程间通信都是其系统结构的一个重要组成部分.而说到Windows的进程(线程)间通信,那就要看是在什么意义上说了.因为正如"Windows的跨进程操作" ...
- Java-slf4j:sfl4j
ylbtech-Java-slf4j:sfl4j 1.返回顶部 1. SLF4J,即简单日志门面(Simple Logging Facade for Java),不是具体的日志解决方案,它只服务于各种 ...
- 用python获取ip信息
1.138网站 http://user.ip138.com/ip/首次注册后赠送1000次请求,API接口请求格式如下,必须要有token值 import httplib2 from urllib.p ...
- Python学习day24-面向对象的三大特征之继承
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...
- 比特镇旅游(Tourist Attractions)【暴力+Bitset 附Bitset用法】
Online Judge:NOIP2016十连测第一场 T2 Label:暴力,Bitset 题目描述 在美丽的比特镇一共有n个景区,编号依次为1到n,它们之间通过若干条双向道路连接. Byteasa ...
- LUOGU P2280 [HNOI2003]激光炸弹
传送门 解题思路 二维前缀和. 代码 #include<iostream> #include<cstdio> #include<cstring> using nam ...
- 自己动手编写JEECMS自定义栏目统计标签
今天想在给Java客二级版面加入栏目文章统计效果,如下图, 查看JEECMS的源代码发现开发者版本还没有类似现成的统计标签,一种解决的办法是使用现有的JEECMS标签,像这样Struts( [@cms ...