求助:求教一个过滤器filter问题和配置
想用一个过滤器来设置权限,不过没接触过不知道怎么写,能不能帮我写一个例子看看,大致是这样的,有admin和other两个用户权限,admin用户只能访问/admin/*目录下的页面,other只能访问/other/*下的目录,如果访问了不是自己权限能访问的页面则重定向到一个error.jsp页面。哪个高人能帮偶写一个么,感激不尽啊,本人初学者。。
另外我试着在wed.xml配置过滤器,在 <filter> 目录里面加上 <display-name> 这个标签总是报告错误,删了则报告下面的 <description> 标签错误,两个都删了就没事,请问是什么原因,删了这两个表情有问题吗?
谢谢了哈,感激不尽。
------解决方案--------------------我大概写了一个,不过现在还有一个问题:filter重定向到error.jsp页面时成了死循环(一直不停的重定向),filter好象连error.jsp页面也过滤了,难道是web.xml里filter的 <url-pattern> 写错了???大家帮忙看看是哪里有问题。
***************login.jsp*****************
<%@ page language= "java " import= "java.util.* " pageEncoding= "gbk "%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN ">
<html>
<head>
<title> My JSP 'a.jsp ' starting page </title>
</head>
<body>
<form action= "login " method= "post ">
username: <input type= "text " name= "name "/> <br>
password: <input type= "password " name= "pwd "/> <br>
<!-- 这里让用户自己输入权限 -->
level: <input type= "text " name= "level "/> <br>
<input type= "submit " value= "提交 ">
</form>
</body>
</html>
***************LoginServlet.java*****************
import
java.io.IOException;
import
javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws
ServletException,
IOException {
String name = request.getParameter( "name ");
String pwd = request.getParameter( "pwd ");
String level = request.getParameter( "level ");
//admin权限
if(name.equals( "test ")&&pwd.equals( "test ")&&level.equals( "admin "))
{
request.getSession().setAttribute( "level ", "admin ");
}
//other权限
else if(name.equals( "test ")&&pwd.equals( "test ")&&level.equals( "other "))
{
request.getSession().setAttribute( "level ", "other ");
}
else
{
response.sendRedirect( "error.jsp ");
}
}
}
***************CheckAdminFilter.java*****************
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.HttpServletResponse;
public class CheckAdminFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException,