简体   繁体   English

Tomcat启动中用于过滤器的ClassCastException

[英]ClassCastException for Filter in Tomcat startup

Im getting an error like this. 我收到这样的错误。 This is happend when i run the program and it did not run.it showed following error. 这是在我运行程序时发生的,它没有运行。它显示以下错误。

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

Can any one tell me the reason of getting this error? 谁能告诉我出现此错误的原因? (Im doing a web java application using jsp,servlet,tomcat.) (我正在使用jsp,servlet,tomcat开发Web Java应用程序。)

Here my FilterRequest.java file code 这是我的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 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> 

Here, you implements Filter: 在这里,您implements过滤器:

public class FilterRequest implements Filter

but not the one you expected , see your import : 不是您所期望的 ,请参阅导入:

import java.util.logging.Filter;

you should replace it with: 您应该将其替换为:

import javax.servlet.Filter

Then, you should not redirect the user to the login page if he is already trying to access it (or it will result in an infinite loop with the filter) : 然后,如果用户已经尝试访问登录页面,则不应将其重定向到登录页面(否则将导致过滤器无限循环):

You should replace this if statement : 您应该替换此if statement

if (u!= null)

with: 有:

if (u!= null || request.getServletPath().equals("/login2.jsp"))

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

From the error it seems you have declared com.mobitel.bankdemo.web.FilterRequest as a Filter in your DD , but forgot to implement javax.servlet.Filter in your class. 从该错误看来,您似乎已经在DD com.mobitel.bankdemo.web.FilterRequest声明为Filter ,但是却忘记了在类中实现javax.servlet.Filter

You are implementing 您正在实施 import java.util.logging.Filter; , you should implement javax.servlet.Filter . ,则应实现javax.servlet.Filter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM