[英]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.我的项目中有五个 Web 应用程序,想要过滤 HTTP 请求,但我不想为每个 Web 应用程序编写一个
Filter
类,因为不会有特定于应用程序的行为。 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.我尝试在另一个名为
MyClassLibrary
类库中只创建一个Filter
,但它不起作用。 Other classes in this library can be used by the web applications so I don't think there's a reference problem. Web 应用程序可以使用此库中的其他类,因此我认为不存在引用问题。
The web.xml
filter part for one of the applications is:其中一个应用程序的
web.xml
过滤器部分是:
<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
java.lang.ClassNotFoundException: MyClassLibrary.Filters.SampleFilter
Platform : Windows, NetBeans, GlassFish平台:Windows、NetBeans、GlassFish
Please check the following 请检查以下内容
If the exception is ClassNotFoundException this means that you haven't set the class MyClassLibrary in the classpath of your application. 如果异常是ClassNotFoundException,则意味着您尚未在应用程序的类路径中设置类MyClassLibrary。
Make sure that your class MyClassLibrary is referenced from the app of this web.xml. 确保从此web.xml的应用程序引用了您的类MyClassLibrary。
Regards. 问候。
I have a filter that works and just modifying the filter-class value to the same class but in a jar, including that jar, does not work out.我有一个可以工作的过滤器,只是将过滤器类值修改为同一个类,但在一个 jar 中,包括那个 jar,不起作用。 The path was wrong, I had noClassDefFound error, so I changed it correctly.
路径错误,我没有ClassDefFound错误,所以我正确更改了它。 No error appears but the filter don't work anymore.
没有出现错误,但过滤器不再工作。
My class compiled without errors.我的班级编译没有错误。 The library is exported with the web app, the class is in the jar in the WEB-INF folder.
库随网络应用程序一起导出,类位于 WEB-INF 文件夹中的 jar 中。
The only difference is that the class was in my app before for me to make it work.唯一的区别是该类之前在我的应用程序中供我使用。 And now, after it worked, I moved it in an external library and only changed the filter-class.
现在,在它工作后,我将它移到一个外部库中,只更改了过滤器类。 I built the library and included it in my app.
我构建了库并将其包含在我的应用程序中。
Even though I have no error, I see in the logs that the filter does not work anymore.即使我没有错误,我在日志中看到过滤器不再工作。 Any idea?
任何的想法?
** UPDATE I don't know why, but the filter works if I modify the web.xml in the tomcat directory but not if I modify the application web.xml in my project. ** 更新我不知道为什么,但是如果我修改了 tomcat 目录中的 web.xml,过滤器会起作用,但如果我修改项目中的应用程序 web.xml,则过滤器不起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.