是个indexs.jsp,是生成验证码的代码.
<%@ page contentType="image/jpeg" import="java.awt.*,
java.awt.image.*,java.util.*,javax.imageio.*" %>
<%!
Color getRandColor(int fc,int bc)
{
Random random = new Random();
if(fc>255) fc=255;
if(bc>255) bc=255;
int r=fc+random.nextInt(bc-fc);
int g=fc+random.nextInt(bc-fc);
int b=fc+random.nextInt(bc-fc);
return new Color(r,g,b);
}
%>
<% response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
int width=60, height=20;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = image.getGraphics();
Random random = new Random();
g.setColor(getRandColor(200,250));
g.fillRect(0, 0, width, height);
g.setFont(new Font("Times New Roman",Font.PLAIN,18));
g.setColor(getRandColor(160,200));
for (int i=0;i<155;i++)
{
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x,y,x+xl,y+yl);
} String sRand="";
for (int i=0;i<4;i++){
String rand=String.valueOf(random.nextInt(10));
sRand+=rand;
g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
g.drawString(rand,13*i+6,16);
} session.setAttribute("rand",sRand);
g.dispose();
ImageIO.write(image, "JPEG",response.getOutputStream() );
%> ----------------------------------------------------------------------------------------------------------------------------------
这个是输入的验证是否正确 validate.jsp
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<%
String rand = (String)session.getAttribute("rand");
String input = request.getParameter("checkcode");
if(rand.equals(input)){
out.print("<script>alert('验证通过!');</script>");
} else{
out.print("<script>alert('请输入正确的验证码!');location.href='login.jsp';</script>");
}
%>
-----------------------------------------------------------------------------------------------------------------------------------这个显示验证码的页面 index.jsp<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%--
The taglib directive below imports the JSTL library. If you uncomment it,
you must also add the JSTL library to the project. The Add Library... action
on Libraries node in Projects view can be used to add the JSTL 1.1 library.
--%>
<%--
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
--%><!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">
<meta http-equiv="Page-Exit" content="revealTrans(Duration=3,Transition=5)">
</head>
<body>
<form action="as.html" method="POST" name="login">
<table border="1">
<tr>
<td>
验证码:
</td>
<td>
<input type="text" name="checkcode" />
<img alt="code..." name="randImage" id="randImage" src="indexs.jsp" width="60" height="20" border="1" align="absmiddle" />
</td>
</tr>
<tr>
<td><input type="submit" value="Submit" /></td>
<td><input type="reset" value="Reset" /></td>
</tr>
</table>
</form>
</body>
</html>
------------------------------------------------------------------------------------------------------------------------------------
上面能成功跳转。
------------------------------------------------------------------------------------------------------------------------------------
我试了好久都没有找到,用JS在index.jsp进行验证码验证,然后在跳转到成功页面。可是试了好久都找不到。各位帮帮忙,我刚学的JSP。
<%@ page contentType="image/jpeg" import="java.awt.*,
java.awt.image.*,java.util.*,javax.imageio.*" %>
<%!
Color getRandColor(int fc,int bc)
{
Random random = new Random();
if(fc>255) fc=255;
if(bc>255) bc=255;
int r=fc+random.nextInt(bc-fc);
int g=fc+random.nextInt(bc-fc);
int b=fc+random.nextInt(bc-fc);
return new Color(r,g,b);
}
%>
<% response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
int width=60, height=20;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = image.getGraphics();
Random random = new Random();
g.setColor(getRandColor(200,250));
g.fillRect(0, 0, width, height);
g.setFont(new Font("Times New Roman",Font.PLAIN,18));
g.setColor(getRandColor(160,200));
for (int i=0;i<155;i++)
{
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x,y,x+xl,y+yl);
} String sRand="";
for (int i=0;i<4;i++){
String rand=String.valueOf(random.nextInt(10));
sRand+=rand;
g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
g.drawString(rand,13*i+6,16);
} session.setAttribute("rand",sRand);
g.dispose();
ImageIO.write(image, "JPEG",response.getOutputStream() );
%> ----------------------------------------------------------------------------------------------------------------------------------
这个是输入的验证是否正确 validate.jsp
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<%
String rand = (String)session.getAttribute("rand");
String input = request.getParameter("checkcode");
if(rand.equals(input)){
out.print("<script>alert('验证通过!');</script>");
} else{
out.print("<script>alert('请输入正确的验证码!');location.href='login.jsp';</script>");
}
%>
-----------------------------------------------------------------------------------------------------------------------------------这个显示验证码的页面 index.jsp<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%--
The taglib directive below imports the JSTL library. If you uncomment it,
you must also add the JSTL library to the project. The Add Library... action
on Libraries node in Projects view can be used to add the JSTL 1.1 library.
--%>
<%--
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
--%><!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">
<meta http-equiv="Page-Exit" content="revealTrans(Duration=3,Transition=5)">
</head>
<body>
<form action="as.html" method="POST" name="login">
<table border="1">
<tr>
<td>
验证码:
</td>
<td>
<input type="text" name="checkcode" />
<img alt="code..." name="randImage" id="randImage" src="indexs.jsp" width="60" height="20" border="1" align="absmiddle" />
</td>
</tr>
<tr>
<td><input type="submit" value="Submit" /></td>
<td><input type="reset" value="Reset" /></td>
</tr>
</table>
</form>
</body>
</html>
------------------------------------------------------------------------------------------------------------------------------------
上面能成功跳转。
------------------------------------------------------------------------------------------------------------------------------------
我试了好久都没有找到,用JS在index.jsp进行验证码验证,然后在跳转到成功页面。可是试了好久都找不到。各位帮帮忙,我刚学的JSP。
解决方案 »
- jsp的运行
- 最近开发了一个基于Ajax技术和SSH框架的Web OS, 大家有兴趣的话来试用下,同时散分!
- [高分提问]在线急等,如何从url查找具体用到的类???
- java如何读取ini 文件
- 急求一份用spring+hibernate+struts 用于web开发的实例源码!
- 请问我安装的weblogic的console中的左边的导航树怎么点击无效?
- tomcat 5.5以版本如何才看到dos的运行窗口,谢谢!
- 出个小小的题目
- 请教一个Weblogic问题(高手请进)
- 如何在字符串中判断全角字符和半角字符?在线等待!!!
- Heritrix和struts2.0或者struts1.2结合出现的奇怪问题!!求救!!
- spring与Hibernate整合后的延迟加载问题
</form>然后,你定义一个js函数submit,就可以了