当前位置: 代码迷 >> Java Web开发 >> [求助]有关于JSP彩色验证码的一些问题,哪位大侠帮帮忙?
  详细解决方案

[求助]有关于JSP彩色验证码的一些问题,哪位大侠帮帮忙?

热度:387   发布时间:2005-05-03 21:24:00.0
[求助]有关于JSP彩色验证码的一些问题,哪位大侠帮帮忙?
我最近在弄毕业设计,对于验证部分。我希望加入彩色验证码。便从网上寻找,运行后有一些问题。

//image.jsp
<%@ page contentType="image/jpeg"%>
<%@ page 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("Comic Sans MS",Font.PLAIN,20));

//画边框
//g.setColor(new Color());
//g.drawRect(0,0,width-1,height-1);

// 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
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);
}

// 取随机产生的认证码(4位数字)
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
session.setAttribute("rand",sRand);

// 图象生效
g.dispose();

// 输出图象到页面
ImageIO.write(image, "JPEG", response.getOutputStream());
%>

//check.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" %>
<html>
<head>
<title>check</title>
<SCRIPT language="javascript">
function chkMsg(){
if(login.username.value == '' || login.password.value == '' || login.secr.value == ''){
alert('请输入完整信息');
return false;
}
return true;
}
</SCRIPT>
</head>

<body>
<form name="login" method="POST" action="confirm.jsp" onSubmit="return chkMsg()">
<table>
<tr>
<td>帐户<input type="text" name="username"></td>
<td>密码<input type="password" name="password"></td>
<td>验证码<input type="text" name="secr" ></td>
<td><img border="0" src="image.jsp"></td>
<td><input type="submit" name="sub" value="确定"></td>
</tr>
</table>
</form>
</body>
</html>

//confirm.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" import="java.util.*" import="java.text.*" import="java.io.*" errorPage="" %>
<jsp:useBean id="chklogin" class="test.firm" scope="page" />

<%
String adminname = request.getParameter("username");
String adminpass = request.getParameter("password");
String rand = (String)session.getAttribute("rand");
String input = request.getParameter("secr");

String sql = "SELECT * FROM custom where custid='"+adminname+"' and custpass='"+adminpass+"'";
ResultSet rs = chklogin.executeQuery(sql);
if(!rand.equals(input))
{
out.println("<script language=javascript> alert('你输入的验证码不正确!'); location.href='index.jsp';</script>");
}
else if (rs.next())
{
String cash = rs.getString("custcash");
rs.close();
session.setAttribute("adminname",adminname);
session.setAttribute("cash",cash);
response.sendRedirect("jump.jsp");
}
else
{
out.println("<script language=javascript> alert('你输入的用户名或密码不正确!'); location.href='index.jsp';</script>");
}
%>
<%
chklogin.closeStmt();
chklogin.closeConn();
%>

//jump.jsp
<%@ page contentType="text/html; charset=GB2312"%>
<html>
<head>
<title>jump</title>
</head>
<body>
<h1>ok,您验证以成功</h1>
</body>
</html>

//test.firm
ackage test;

import java.awt.*;
import javax.swing.JPanel;
import java.sql.*;

public class firm
{
String strDBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
String strDBUrl="jdbc:odbc:MYSQL";
public Connection conn=null;
public Statement stmt=null;
ResultSet rs=null;
//<!--注册数据库驱动程序-->
public firm(){
try{
Class.forName(strDBDriver);
}catch(java.lang.ClassNotFoundException e){
System.err.println("firm():"+e.getMessage());
}
}
//<!--建立数据库连接及定义数据查询-->
public ResultSet executeQuery(String sql){
rs=null;
try{
conn=DriverManager.getConnection(strDBUrl,"sa","");//创建数据库连接对象
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
}catch(SQLException ex){
System.err.println("aq.executeQuery:"+ex.getMessage());
}
return rs;
}
//<!--定义数据操作-->
public void executeUpdate(String sql){
stmt=null;
rs=null;
try{
conn=DriverManager.getConnection(strDBUrl,"sa","");
stmt=conn.createStatement();
stmt.executeQuery(sql);
rs.close();
stmt.close();
conn.close();
}catch(SQLException ex){
System.err.println("aq.executeUpdate:"+ex.getMessage());
}
}

//<!--关闭数据库连接-->
public void closeStmt(){
try{
stmt.close();
}catch(SQLException e){
e.printStackTrace();
}
}
public void closeConn(){
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}

public String ex_chinese(String str){
if(str==null){
str ="" ;
}else{
try{
str = new String(str.getBytes("iso-8859-1"),"gb2312");
}catch (Exception ex) {
}
}
return str ;
}

public static void main(String args[]){
firm fm= new firm();
}
}

数据源为MYSQL 表为custom 其中列名分别为custid,custpass,custcash


运行后问题为:
StandardWrapperValve[debugjsp]: Servlet.service() for servlet debugjsp threw exception

java.lang.IllegalStateException: getOutputStream() has already been called for this response

java.lang.IllegalStateException: getOutputStream() has already been called for this response

??at org.apache.catalina.connector.ResponseBase.getWriter(ResponseBase.java:755)

??at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:165)

??at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:166)

??at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:158)

??at org.apache.jasper.runtime.JspWriterImpl.flush(JspWriterImpl.java:205)

??at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:177)

??at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:198)

??at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:193)

??at org.apache.jsp.image$jsp._jspService(image$jsp.java:145)

??at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:107)

??at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)

??at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:202)

??at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:382)

??at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:474)

??at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)

??at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)

??at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)

??at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:243)

??at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)

??at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)

??at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)

??at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:190)

??at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)

??at org.apache.catalina.valves.CertificatesValve.invoke(CertificatesValve.java:246)

??at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)

??at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)

??at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)

??at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2343)

??at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)

??at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)

??at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)

??at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)

??at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:170)

??at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)

??at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)

??at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)

??at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)

??at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)

??at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)

??at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)

??at org.apache.catalina.connector.http.HttpProcessor.process(HttpProcessor.java:1012)

??at org.apache.catalina.connector.http.HttpProcessor.run(HttpProcessor.java:1107)

??at java.lang.Thread.run(Thread.java:536)
搜索更多相关主题的帖子: JSP  彩色  验证  

----------------解决方案--------------------------------------------------------
图像验证码没有问题。我已经试验过了,我想问题可能出在你的mysql的连接和查询的那个地方,建议你把图像验证的那个功能部分去掉后先做一下用户名和密码的验证功能。等这个功能成功后再把图像验证的那个部分加上去。我这里没有mysql所以只能试验到这个地方了!
----------------解决方案--------------------------------------------------------