当前位置: 代码迷 >> J2EE >> 验证码途径正确,却无法正确显示,提示内容非法
  详细解决方案

验证码途径正确,却无法正确显示,提示内容非法

热度:85   发布时间:2016-04-22 03:01:33.0
验证码路径正确,却无法正确显示,提示内容非法
<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@include file="../common/admin_head.jsp" %>
<!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="Pragma" CONTENT="no-cache"> 
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache"> 
<META HTTP-EQUIV="Expires" CONTENT="0"> 
<title>test login</title>
</head>
<body>
<form action="login.action">
<table>
<tr>
<td>login name:<s:textfield name="loginName" label="loginName"/></td>
</tr>
<tr>
<td>password:<s:password name="loginPwd" label="password:"/></td>
</tr>
<tr>
<td><s:textfield name="rand" label="validation"/><img src="<%=basepath%>/common/rand.jsp"/></td>
</tr>
<tr>
<td><s:submit/></td>
</tr>
</table>
</form>
</body>
</html>



basepath
<%@ page contentType="text/html; charset=gbk"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%
//取得当前Web应用的名称
String basepath = request.getContextPath();
%>


rand.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=40, height=16;
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("Arial",Font.BOLD,12));
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(0,0,0));
  g.drawString(rand,9*i+3,12);
}
session.setAttribute("rand",sRand);
g.dispose();
ImageIO.write(image, "JPEG", response.getOutputStream());
%>

------解决方案--------------------
额。。。
  相关解决方案