JS验证验证码? 怎么在表单前验证用户输入的验证码是否输入正确?不提交到后台验证验证码生成后,放在session对象 中,在线等 ,附代码最好 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 要在JS中验证的话,后台生成验证码后,必须要放到前台页面(作为隐藏域、或者JS变量) <img id="img" src="code.jsp" >我是在前台用图片的SRC链接验证码的JSP但在前台用SESSION取验证码的时候 ,取到的验证码老是前一张,不能同步 取到老一张的图片,是因为<img id="img" src="code.jsp" >要在加载图片的时候才会去调用code.jsp而这个时候首页的jsp在服务器端已经执行,并生成页面到客户端所以取的是之前一张的图片LZ可以用ajax再调用一个JSP,在那个JSP里取SESSION 没其它办法吗?我们还没学到ajax= =! public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(CONTENT_TYPE); Random ran=new Random(); ServletOutputStream out=response.getOutputStream(); BufferedImage img=new BufferedImage(60,30,BufferedImage.TYPE_INT_RGB); Graphics g=img.getGraphics(); //g.setColor(new Color(ran.nextInt(255),ran.nextInt(255),ran.nextInt(255))); g.fillRect(0,0,60,30); g.setFont(new Font("黑体",Font.PLAIN,25)); g.setColor(new Color(ran.nextInt(255),ran.nextInt(255),ran.nextInt(255))); StringBuffer str=new StringBuffer(); for (int i = 0; i < 4 ; i++) { str.append((char)(ran.nextInt(26)+65)); } g.drawString(str.toString(),5,25); for (int i = 0; i <= 10 ; i++) { //g.setColor(new Color(ran.nextInt(255),ran.nextInt(255),ran.nextInt(255))); g.drawLine(ran.nextInt(img.getWidth()),ran.nextInt(img.getHeight()),ran.nextInt(img.getWidth()),ran.nextInt(img.getHeight())); } JPEGImageEncoder jpeg=JPEGCodec.createJPEGEncoder(out); jpeg.encode(img); out.close(); request.getSession().setAttribute("code",str.toString()); }上面是出验证码,是用servlet写的,,一样的<%@ page contentType="text/html; charset=GBK" %><html><head><title>jsp1</title><script type=""> function change() { document.getElementById("imga").src="checkservlet?"+Math.random(); }</script></head><body bgcolor="#ffffff"><h1>验证码</h1><form method="post" action="jsp2.jsp"><br><img id="imga" alt="" src="checkservlet" /><a href="javascript:change();">看不清楚,换一张</a><br>验证码:<input type="text" name="cd"/><input type="Submit" name="Submit" value="Submit"></form></body></html>上面的是调用<%@page contentType="text/html; charset=GBK"%><% String code = request.getParameter("cd"); String check=session.getAttribute("code").toString(); if (!code.equals(check)) { out.println("<script>alert('验证码错误!');history.back();</script>"); } else { out.println("<script>alert('验证码正确!');history.back();</script>"); }%>这个是处理,,你可以看一下 我也碰到了这个问题,有大神建议先用随机数生成验证码,再将验证码写到图片中去,这样session中存的便是数字,就不会出现刷新后获得的是前一个验证码了! onclick事件 能不能同时调用两个js函数 jquery跨域 获取数据 ################################ 100%结贴,两个贴一起结 如何用javascript 改变当前窗口的链接! 一个关于html文本编辑器的问题 请问怎样使JS变量在两个页面中传递? confirm() 在线等 马上结 求一个简单的窗口函数 请问一个css样式的问题,大家进来看看 如何建立快捷方式?? uni.requset的data怎么作为值返回到函数 javascript 做jmail自动发送邮件的问题 for-loop, continue問題
而这个时候首页的jsp在服务器端已经执行,并生成页面到客户端
所以取的是之前一张的图片
LZ可以用ajax再调用一个JSP,在那个JSP里取SESSION
没其它办法吗?我们还没学到ajax= =!
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
Random ran=new Random();
ServletOutputStream out=response.getOutputStream();
BufferedImage img=new BufferedImage(60,30,BufferedImage.TYPE_INT_RGB);
Graphics g=img.getGraphics();
//g.setColor(new Color(ran.nextInt(255),ran.nextInt(255),ran.nextInt(255)));
g.fillRect(0,0,60,30);
g.setFont(new Font("黑体",Font.PLAIN,25));
g.setColor(new Color(ran.nextInt(255),ran.nextInt(255),ran.nextInt(255)));
StringBuffer str=new StringBuffer();
for (int i = 0; i < 4 ; i++) {
str.append((char)(ran.nextInt(26)+65));
}
g.drawString(str.toString(),5,25);
for (int i = 0; i <= 10 ; i++) {
//g.setColor(new Color(ran.nextInt(255),ran.nextInt(255),ran.nextInt(255)));
g.drawLine(ran.nextInt(img.getWidth()),ran.nextInt(img.getHeight()),ran.nextInt(img.getWidth()),ran.nextInt(img.getHeight()));
}
JPEGImageEncoder jpeg=JPEGCodec.createJPEGEncoder(out);
jpeg.encode(img);
out.close();
request.getSession().setAttribute("code",str.toString());
}
上面是出验证码,是用servlet写的,,一样的
<%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<title>
jsp1
</title>
<script type="">
function change()
{
document.getElementById("imga").src="checkservlet?"+Math.random();
}
</script>
</head>
<body bgcolor="#ffffff">
<h1>
验证码
</h1>
<form method="post" action="jsp2.jsp">
<br>
<img id="imga" alt="" src="checkservlet" />
<a href="javascript:change();">看不清楚,换一张</a>
<br>
验证码:<input type="text" name="cd"/>
<input type="Submit" name="Submit" value="Submit">
</form>
</body>
</html>
上面的是调用
<%@page contentType="text/html; charset=GBK"%>
<%
String code = request.getParameter("cd");
String check=session.getAttribute("code").toString();
if (!code.equals(check)) {
out.println("<script>alert('验证码错误!');history.back();</script>");
}
else {
out.println("<script>alert('验证码正确!');history.back();</script>");
}
%>
这个是处理,,你可以看一下