问题描述
我有一个 Java Project
,我正在创建一个Web界面,使用动态Web项目
从Eclipse。 Web项目由单个 servlet
和两个 JSP
组成。这样的东西:
I have a Java Project
, for which I'm now creating a Web interface, using a Dynamic Web Project
from Eclipse. The Web project consists of a single servlet
and two JSP
's. Something like this:
/JavaApplication
/src
/lib
/resources
/WebApplication
/src
/Servlet.java
/WebContent
/WEB-INF
index.jsp
other.jsp
现在,我需要从<$ c $中引用 JavaApplication
c> WebApplication ,以便使用其类来处理Web请求。什么是最好的方式来完成这个?我的想法是创建一个 .jar
的 JavaApplication
,其中包含所有的 .class
文件, / resources
和 / libs
。这样,我可以在Web应用程序中包含.jar,我可以有一个包含整个应用程序的单个 .war
文件。
Now, I need to reference JavaApplication
from WebApplication
, in order to use its classes to process web requests. What's the best way to accomplish this ? My idea is to create a .jar
of the JavaApplication
, containing all the .class
files, /resources
, and /libs
. In this way, I could include the .jar in the web application, and I could have a single .war
file that contained the entire application.
你觉得怎么样?这个问题通常解决了吗?
What do you think? How is this problem typically solved ?
注意:我不想将Java项目转换为Web项目。
Note: I don't want to convert the Java Project into a Web project.
推荐答案
在Eclipse下,您可以为您的案例中的Web应用程序为给定的项目声明项目引用。为此,请右键单击您的Web应用程序项目,然后转到属性>项目引用,然后选择 JavaApplication
项目。这应该允许您从 WebApplication
中的 JavaApplication
项目中调用代码,而无需构建WAR。这是开发的解决方案。
Under Eclipse, you can declare Project References for a given project, the web application in your case. To do so, right click on your web application project, then go for Properties > Project References and select the JavaApplication
project. This should allow you to call code from the JavaApplication
project from the WebApplication
without having to build a WAR. This is a solution for development.
对于标准的部署(IDE外),您应该创建一个标准战争为此,您必须将您的 JavaApplication
打包为JAR,其中包括 .class
文件和 / resources
但不依赖的库(JARs在 / lib
下)。这些依赖关系实际上最终会在WAR的 WEB-INF / lib
目录中,而您的 JavaApplication
。这些步骤通常是自动化的,其中包括或。
For standard deployment (outside the IDE), you should indeed create a standard WAR. To do so, you'll have to package your JavaApplication
as a JAR including the .class
files and the files under /resources
but not the libraries it depends on (JARs under /lib
). These dependencies will actually end up in the WEB-INF/lib
directory of the WAR, beside the JAR of your JavaApplication
. These steps are typically automated with tools like Ant or Maven.
这篇关于Java Web Project引用另一个Java项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!