0

我收到这样的错误。当我运行程序并且它没有运行时发生这种情况。它显示以下错误。

Jul 10, 2013 1:21:24 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre6\bin;.;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jre6/bin/client;C:/Program Files/Java/jre6/bin;C:/Program Files/Java/jre6/lib/i386;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Kaspersky Lab\Kaspersky Anti-Virus 6.0 for Windows Workstations MP4\;C:\Program Files\Java\jdk1.6.0_07/bin;C:\Program Files\MySQL\MySQL Server 5.2\bin;D:\common libs\com.mysql.jdbc_5.1.5.jar;;C:\Users\lcladmin\Documents\Softwares\java related\eclipse-jee-indigo-win32_2\eclipse;
Jul 10, 2013 1:21:24 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:BankDemoWeb' did not find a matching property.
Jul 10, 2013 1:21:24 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Jul 10, 2013 1:21:24 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Jul 10, 2013 1:21:24 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 552 ms
Jul 10, 2013 1:21:24 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Jul 10, 2013 1:21:24 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.41
Jul 10, 2013 1:21:25 PM org.apache.catalina.core.StandardContext filterStart
SEVERE: Exception starting filter FilterRequest
java.lang.ClassCastException: com.mobitel.bankdemo.web.FilterRequest cannot be cast to javax.servlet.Filter
    at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:260)
    at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:107)
    at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4746)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5399)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Jul 10, 2013 1:21:25 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: Error filterStart
Jul 10, 2013 1:21:25 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: Context [/BankDemoWeb] startup failed due to previous errors
Jul 10, 2013 1:21:25 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Jul 10, 2013 1:21:25 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Jul 10, 2013 1:21:25 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 533 ms

谁能告诉我得到这个错误的原因?(我正在使用 jsp、servlet、tomcat 做一个 web java 应用程序。)

这是我的 FilterRequest.java 文件代码

package com.mobitel.bankdemo.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.logging.Filter;
import java.util.logging.LogRecord;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.mobitel.bankdemo.domain.User;

public class FilterRequest implements Filter{
    private static final long serialVersionUID = 1L;
    FilterConfig filterConfig = null;
    public FilterRequest() {
        super();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {       
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
    }
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
            HttpServletRequest request = (HttpServletRequest) req;
            HttpServletResponse response = (HttpServletResponse) resp;
                      HttpSession session = request.getSession(true);
          User u = (User) session.getAttribute("loggedUser");
          System.out.println(u+"i");
          if (u!= null)
          {
             System.out.println(u+"u");
             chain.doFilter(req, resp);
             return;
          }else{
              String message = "Please Login!";             
              req.setAttribute("loginMsg", message);
              response.sendRedirect("login2.jsp");
          }             
    }
    public void destroy() {
        // do cleanup stuff
    }
    @Override
    public boolean isLoggable(LogRecord arg0) {
        // TODO Auto-generated method stub
        return false;
    }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>BankDemoWeb</display-name>
  <welcome-file-list>
    <welcome-file>home.jsp</welcome-file>
  </welcome-file-list>

  <servlet>
    <servlet-name>AccountController</servlet-name>
    <servlet-class>com.mobitel.bankdemo.web.AccountControllerServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>AccountController</servlet-name>
    <url-pattern>/account</url-pattern>
  </servlet-mapping>

  <session-config>
    <session-timeout>5</session-timeout>
  </session-config>

   <filter>
        <filter-name>FilterRequest</filter-name>
        <filter-class>com.mobitel.bankdemo.web.FilterRequest</filter-class>             
  </filter>
  <filter-mapping>
        <filter-name>FilterRequest</filter-name>
        <url-pattern>/*</url-pattern>
  </filter-mapping> 

4

2 回答 2

3

在这里,您implements过滤:

public class FilterRequest implements Filter

不是您期望的,请查看您的导入:

import java.util.logging.Filter;

您应该将其替换为:

import javax.servlet.Filter

然后,如果用户已经尝试访问登录页面,则不应将其重定向到登录页面(否则将导致过滤器无限循环):

你应该替换这个if statement

if (u!= null)

和:

if (u!= null || request.getServletPath().equals("/login2.jsp"))
于 2013-07-10T09:11:25.537 回答
2

java.lang.ClassCastException:com.mobitel.bankdemo.web.FilterRequest 无法转换为 javax.servlet.Filter

从错误看来,您已在 DD 中声明com.mobitel.bankdemo.web.FilterRequest为 a Filter,但忘记javax.servlet.Filter在您的类中实现。

您正在实施import java.util.logging.Filter;, 你应该执行javax.servlet.Filter.

于 2013-07-10T08:48:55.507 回答