怎么在表单前验证用户输入的验证码是否输入正确?不提交到后台验证
验证码生成后,放在session对象 中,在线等 ,附代码最好
验证码生成后,放在session对象 中,在线等 ,附代码最好
解决方案 »
- js中的onclick事件
- TD里怎么垂直对齐的问题
- 求一个正则表达式的解释
- 为什么窗口移动速度越来越快?
- 下拉菜单居中问题,急
- 我想选中一条记录后显示某一个字段的值的地方自动变成OPTION那样的下拉选择框,并且显示相应的记录,请高手指导!十万火急!
- 求一份js写的登陆程序,当不同的用户输入用户名和密码以后,经过确认转到指定的页面
- 在线.Net编辑器,看看了拉?
- window.form1.submit()或者document.form1.submit()在netscape中不起作用,请问该如何解决
- JQ自定义运动问题?初级
- 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>");
}
%>
这个是处理,,你可以看一下