一个十分恼头的问题,需要高手来解决啊(验证码)
我写了一个验证码,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;
}
-->
</style>
<script language="javascript">
function validImg()
{
document.loginform.img.src="ValidImg?now="+new Date();
}
</script>
</head>
<body>
<table width="763" height="392" border="0" align="center">
<tr>
<td width="733" height="198"> </td>
</tr>
<tr>
<td height="188" valign="bottom">
<form name="loginform" method="post" action="AdminManageServlet?param=1">
<table width="353" border="1" align="center" cellpadding="1" cellspacing="0" bordercolor="#6699CC">
<tr align="center">
<td colspan="3" class="code">网上购物系统后台管理</td>
</tr>
<tr>
<td width="85" height="22" align="right" valign="middle" class="code">用户名:</td>
<td width="148"><input name="loginname" type="text" class="style9" id="loginname" size="20" maxlength="20" onFocus=""></td>
<td width="77" class="style5">*</td>
</tr>
<tr>
<td height="21" align="right" valign="middle" class="code">密码:</td>
<td><input name="loginpassword" type="password" class="style9" id="loginpassword" size="20" maxlength="20"></td>
<td class="style5">*</td>
</tr>
<tr>
<td><div align="right">验证码:</div></td>
<td><label>
<input name="validcode" type="text" size="20">
</label></td>
<td><span class="style5">*</span></td>
</tr>
<tr align="center" valign="middle">
<td colspan="2"><div align="right"><img src="ValidImg" id="img"/> </div></td>
<td><a href="#" onClick="validImg()">看不清换一张</a></td>
</tr>
<tr align="center" valign="middle">
<td colspan="3"><input type="submit" class="buttonstyle" value="提交">
<input name="Submit2" type="reset" class="buttonstyle" value="重填"></td>
</tr>
</table>
<p> </p>
</form></td>
</tr>
</table>
</body>
</html>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>ValidImg</servlet-name>
<servlet-class>com.chary.controlservlet.ValidImg</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ValidImg</servlet-name>
<url-pattern>/ValidImg</url-pattern>
</servlet-mapping>
<filter>
<filter-name>login</filter-name>
<filter-class>com.chary.filter.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>login</filter-name>
<url-pattern>/admin/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>CharSetFilter</filter-name>
<filter-class>com.chary.filter.CharSetFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CharSetFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
LoginFilter.Filter:
package com.chary.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import com.chary.beans.TAdmin;
import com.chary.dao.AdminDao;
import com.chary.factory.DaoFactory;
public class LoginFilter implements Filter
{
public void init(FilterConfig filterConfig)
throws ServletException
{
}
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain)
throws IOException,
ServletException
{
HttpServletRequest req = (HttpServletRequest)request ;
HttpSession session = req.getSession() ;
if(session.getAttribute("adminMsg")!=null)
{
chain.doFilter(request,response) ;
}
else
{
request.getRequestDispatcher("/AdminLogin.jsp").forward(request,response) ;
//这个地方我还是不懂为什么是/AdminLogin.jsp而不是AdminLogin.jsp
}
}
}
[解决办法]
可能上由于请求还是上一次的请求.
request.getRequestDispatcher("/AdminLogin.jsp").forward(request,response) ;
用重定向
reponse.sendRedirect("AdminLogin.jsp");
试下把。