最近写了个BBS,想通过 filter (过滤器)来完成登录验证工作,实现了javax.servlet.filter接口
但配置好启动TOMCAT 6 后,就报错,去掉web.xml中的filter配置后,启动就没问题,但过滤器也就失去作用了。
请大家帮我看下,哪里出了问题,还是TOMCAT 6的过滤器配置有特殊要求。web.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app> <filter>
  <filter-name>logonValidate</filter-name>
  <filter-class>javax.servlet.Filter</filter-class>
 </filter>
 <filter-mapping>
  <filter-name>logonValidate</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

 <servlet>
  <description>通告栏</description>
  <display-name>通告栏</display-name>
  <servlet-name>Callboard</servlet-name>
  <servlet-class>rbt.infor.Callboard</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>Callboard</servlet-name>
  <url-pattern>/servlet/Callboard</url-pattern>
 </servlet-mapping>
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
 <login-config>
  <auth-method>BASIC</auth-method>
 </login-config>
 <resource-ref>
  <description>DB Connection</description>
  <res-ref-name>jdbc/sql</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>
 </resource-ref>
</web-app>但只要在web.xml中加入了红色filter部分的配置后,就会报错,如下:
严重: Exception starting filter logonValidate
java.lang.InstantiationException: javax.servlet.Filter
at java.lang.Class.newInstance0(Class.java:340)
at java.lang.Class.newInstance(Class.java:308)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:255)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:397)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:108)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3709)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4363)
at org.apache.catalina.startup.HostConfig.checkResources(HostConfig.java:1116)
at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1214)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:293)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1337)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1601)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1610)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1590)
at java.lang.Thread.run(Thread.java:619)
2009-4-20 11:45:21 org.apache.catalina.core.StandardContext start
严重: Error filterStart过滤器类的源代码如下:
package rbt.filter;import java.io.IOException;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
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;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;public class FilterLogonValidate implements Filter {
public  Context ctx=null;
public DataSource ds=null; public void destroy() {
// TODO Auto-generated method stub } public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletResponse rsp=(HttpServletResponse)response;
HttpServletRequest rqt=(HttpServletRequest)request;
rsp.setContentType("text/html;charset=gb2312");
rsp.setCharacterEncoding("gb2312");
rsp.setHeader("Pragma", "No-cache");
rsp.setHeader("Cache-Control", "no-cache");
rsp.setDateHeader("Expires", 0);
HttpSession session=rqt.getSession();

//PrintWriter out=rsp.getWriter();
Connection conn=null;

boolean trueOrfalse=false;
String username=null;
String pwd=null;
String validateCode=null;

if(session.getAttribute("username")!=null){//用户已经登录
trueOrfalse=true;
}else{//用户尚未登录
username=rqt.getParameter("username");
pwd=rqt.getParameter("pwd");
validateCode=rqt.getParameter("code");
username=username.trim();
pwd=pwd.trim();
validateCode=validateCode.trim();

//验证登录信息
if(username!=null&&!(username.equals(""))&&pwd!=null&&!(pwd.equals(""))&&validateCode!=null&&validateCode.equals(session.getAttribute("validateCode"))){
String sql="select id,username from rbt_person where username=? and password=?";
PreparedStatement pst=null;
ResultSet rst=null;
try {
conn=ds.getConnection();
pst=conn.prepareStatement(sql);
pst.setString(1, username);
pst.setString(2, pwd);

rst=pst.executeQuery();
if(rst.next()){
session.setAttribute("id", rst.getString(1));
session.setAttribute("username", rst.getString(2));
trueOrfalse=true;
}else{//数据库中无此用户
trueOrfalse=false;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}else{//验证信息输入错误返回登录界面
trueOrfalse=false;

}
}
if(trueOrfalse){
chain.doFilter(rqt, rsp);
}else{
rqt.getRequestDispatcher("/logon").forward(rqt, rsp);
}
} public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub try {
ctx=new InitialContext();
ds=(DataSource)ctx.lookup("java:comp/env/jdbc/sql");
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}

解决方案 »

  1.   

    问题找到了,原来是WEB.XML 中没有指定xml版本
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
      

  2.   

    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">