我写了一个验证码,Servlet实现的,登录的jsp文件调用,还有一个过滤器Filter,是过滤管理员是否登录,若还没,返回去登录。但出现了一个问题:正常到AdminLogin.jsp去登录,验证码可以显示;但如果你还没有登录可是写了一个地址访问网站,其中路径中含有/admin,他会被过滤器过滤返回了登录页面,可这时验证码就不出现了,,我发下代码,请各位大侠指点迷津:
jsp文件的路径:
WebRoot/AdminLogin.jsp
WebRoot/admin/manager.jsp
若登录成功后会进入主界面manager.jsp
ValidImg.Servelt:
package com.chary.controlservlet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class ValidImg extends HttpServlet
{
/**
*
*/
private static final long serialVersionUID = 892610220851833005L;
public ValidImg()
{
super();
}
public void init() throws ServletException
{
// Put your code here
}
public void service(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{
//阻止页面刷新
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg");
BufferedImage image = new BufferedImage(100,20,BufferedImage.TYPE_INT_RGB);
String saveString = "";
String temp = "";
Graphics g = image.getGraphics();
g.setColor(Color.LIGHT_GRAY);
g.fillRect(0, 0, 100, 25);
this.drawRandLine(g);
g.setFont(new Font("Arial",Font.BOLD,18));
for(int i=0;i<6;i++)
{
temp = this.getRandString();
saveString+=temp;
g.setColor(this.getRandColor());
g.drawString(temp, 15*i+10,15);
}
HttpSession session = request.getSession(true);
session.setAttribute("validate",saveString);
g.dispose();
ImageIO.write(image,"JPEG",response.getOutputStream());
}
private void drawRandLine(Graphics g)
{
for(int i=0;i<10;i++)
{
int x = (int)(Math.random()*100);
int y = (int)(Math.random()*25);
int x1 = (int)(Math.random()*100);
int y1 = (int)(Math.random()*25);
g.setColor(this.getRandColor());
g.drawLine(x,y,x1,y1);
}
}
private Color getRandColor()
{
int red = (int)(Math.random()*256);
int green = (int)(Math.random()*256);
int blue = (int)(Math.random()*256);
return new Color(red,green,blue);
}
private String getRandString()
{
int randInt = (int)(Math.random()*3)+1;
long tempChar=0;
String s=null;
char ch = '\u0000';
switch(randInt)
{
case 1:
tempChar=(int)(Math.random()*25)+65;
ch = (char)tempChar;
return String.valueOf(ch);
case 2:
tempChar=(int)(Math.random()*25)+97;
ch = (char)tempChar;
return String.valueOf(ch);
default :
tempChar=(int)(Math.random()*10);
return String.valueOf(tempChar);
}
}
}
AdminLogin.jsp:
<%@ page language="java" contentType="text/html; charset=gb2312" pageEncoding="GB2312"%>
<!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=gb2312">
<title>购物系统后台管理</title>
<style type="text/css">
<!--
body,td,th {
font-family: 宋体;
}
body {
background-color: #168AC2;