Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could I use Java servlet Filter class from other referenced class library?

I have five web applications in my project and want to filter HTTP requests, but I don't want to write a Filter class for each web application because there will be no application specific behaviour. What is the most practical way to do this?

I've tried creating only one Filter in another class library called MyClassLibrary but it doesn't work. Other classes in this library can be used by the web applications so I don't think there's a reference problem.

The web.xml filter part for one of the applications is:

<filter>
    <filter-name>SampleFilter</filter-name>
    <filter-class>MyClassLibrary.Filters.SampleFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>SampleFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

This causes an exception:

java.lang.ClassNotFoundException: MyClassLibrary.Filters.SampleFilter

Platform : Windows, NetBeans, GlassFish

like image 546
electron Avatar asked Nov 13 '22 03:11

electron


1 Answers

Please check the following

  • Make sure your class is compiled without any errors
  • Make sure your deployment of those web applications is having reference to project/jar that contains your library
  • From the name, it looks like SampleFilter is an inner class. If so is the case, make sure that Filters and SampleFilter are static inner class of MyClassLibrary and has no args constructor
  • Are you missing package name in front of MyClassLibrary?
like image 134
Gladwin Burboz Avatar answered Nov 14 '22 21:11

Gladwin Burboz