库中的 Java 重载方法未在 Web 服务器中运行时失败
我正在尝试编写一个小型库,它可以在标准 java 应用程序中使用,也可以作为 servlet 的一部分。
我定义了几个重载方法,如下所示:
// imports etc.
public ExampleLibrary {
/**
* This one is meant to be used by a J2SE app
*/
public String processData(Map headers) throws MyException {
// process
// return result
}
/**
* This one is meant to be used by a servlet
*/
public String processData(HttpServletRequest request) throws MyException {
// extract headers from request
// process
// return result
}
// other methods.....
}
当用作 Servlet 的一部分时,这非常有用,但是当用作 J2SE 应用程序的一部分时,它不起作用。
在我的 J2SE 应用程序中,我执行了以下操作:
ExampleLibrary example = ExampleLibrary.getInstance();
Map headers = new HashMap();
headers.put("someheader1", "someheaderval1");
headers.put("someheader2", "someheaderval2");
String res = example.processData(headers);
我在编译时得到此信息: “无法访问 javax.servlet.http.HttpServletRequest 未找到 javax.servlet.http.HttpServletRequest 类文件”
我期望编译器选择正确的 processData() 方法并忽略另一个方法,因为显然 Servlet 中没有 Servlet 类文件一个 J2SE 应用程序。有什么想法如何解决这个问题吗?
I am trying to write a small library that can either be used in a standard java app or as part of a servlet.
I have defined a couple of overloaded methods as follows:
// imports etc.
public ExampleLibrary {
/**
* This one is meant to be used by a J2SE app
*/
public String processData(Map headers) throws MyException {
// process
// return result
}
/**
* This one is meant to be used by a servlet
*/
public String processData(HttpServletRequest request) throws MyException {
// extract headers from request
// process
// return result
}
// other methods.....
}
This works great when used as part of a Servlet, however it does not work when used as part of a J2SE app.
In my J2SE app I did the following:
ExampleLibrary example = ExampleLibrary.getInstance();
Map headers = new HashMap();
headers.put("someheader1", "someheaderval1");
headers.put("someheader2", "someheaderval2");
String res = example.processData(headers);
I get this at compile time:
"cannot access javax.servlet.http.HttpServletRequest class file for javax.servlet.http.HttpServletRequest not found"
I was expecting the compiler to choose the correct processData() method and ignore the other one as obviously there is no Servlet class file in a J2SE app. Any ideas how to solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以通过在编译时在类路径中包含
servlet.jar
来解决您的问题。但这里有一个问题:
如果库的用户调用这两个方法之一,编译器需要找出实际调用这两个方法中的哪一个。如果您有不同的方法名称,一切都很好,并且编译器不需要访问
HttpServletRequest
。但是,如果名称相同,编译器需要访问 HttpServletRequest 才能决定这两种方法中哪一个最适合调用和参数。因此,您的库的用户将不需要需要
servlet.jar
来运行他的程序,但如果您使用方法重载,他将需要它能够编译他的程序。下面是一个虚拟示例来说明这一点:
虚拟测试第三方库:
首先,让我们创建一个虚拟第三方库 jar(并丢弃源代码):
虚拟测试您的库:
测试在没有
thirdpartylib.jar< 的情况下使用您的库/代码>
You can solve your problem by including
servlet.jar
on your classpath when compiling.Here's the catch though:
If a user of your library calls one of the two methods, the compiler needs to figure out which of the two methods to actually call. If you have different method names, everything is fine and, the compiler does not need access to the
HttpServletRequest
. If you have the same name however, the compiler needs to accessHttpServletRequest
in order to decide which of the two methods best fit the call and argument.Thus, The user of your lib will not need
servlet.jar
to run his program, but if you use method overloading, he will need it to be able to compile his program.Here's a dummy example that illustrates this:
Dummy test-thirdparty lib:
First, let's create a dummy third party library jar (and throw away the source):
Dummy test your lib:
Testing to use your lib without
thirdpartylib.jar
由于您的类依赖于 javax.servlet.http.HttpServletRequest,因此您必须提供后者的类定义才能加载您的类。最简单的方法是将 servlet.jar 与您的应用程序捆绑在一起。
但请注意,让 JavaSE 应用程序依赖于 JavaEE 库并不是一个好主意。
更好的解决方案是将 servlet 特定部分隐藏在您自己的接口后面,这些接口可以安全地与您的核心应用程序一起提供。然后将应用程序的 JavaEE 特定部分中的接口实现为相应 servlet 类的简单包装器。
Since your class is dependent on
javax.servlet.http.HttpServletRequest
, you must provide the class definition of the latter in order to load your class. The simplest way would be to bundle servlet.jar with your app.Note however, that altogether it is not a good idea to make a JavaSE app dependent on a JavaEE library.
A better solution would be to hide the servlet specific parts behind your own interface(s), which can be safely supplied with your core app. Then implement the interface(s) in the JavaEE specific part of your app as simple wrappers around the corresponding servlet class.
尝试使用不同的名称而不是重载。
现在可以了。
编辑:或者,您可以在编译时将 servlet.jar 放在类路径上。您不需要它来运行程序,只需编译它即可。 java编译器需要类信息来决定选择哪个重载方法。
Try using different names instead of overloading.
Now it works.
Edit: Alternatively you can put servlet.jar on the classpath while compiling. You won't need it to run the program, just to compile it. The java compiler needs the class info to decide which overloaded method to choose.
从 Tomcat 获取 servlet.jar。它包含 servlet API。这应该可以消除该错误。
Get servlet.jar from Tomcat. It contains the servlet API. That should suppress that error.
将 rt.jar 和 servlet.jar (或 eclipse 中的 webApp 库)放在类路径中
put rt.jar and servlet.jar (or webApp library in eclipse) in class path
我怀疑您的错误与重载方法无关。
如果类路径上没有 servlet-api.jar,您根本无法编译您的类。
I suspect your error has nothing to do with overloaded methods.
You simply cannot compile your class without having servlet-api.jar on the classpath.