Eclipse + Tomcat -直接从工作区提供web应用

时间:2023-01-24 18:31:09

What is the ideal way to configure Tomcat to serve directly from my project's directory inside of my workspace? (related)

将Tomcat配置为直接从我的工作空间中的项目目录中提供服务的理想方式是什么?(相关)

I want my static web resources to be instantly available anytime I make a change to them without having to publish them out to a separate directory. It seems like the best way to accomplish this is to serve files directly rather than a published/deployed copy of the file.

我希望我的静态web资源可以随时可用,只要我对它们进行更改,而不需要将它们发布到一个单独的目录中。似乎最好的方法是直接服务文件而不是发布/部署的文件副本。


Alternatively, I'm open to other options (different application servers or IDEs) that make developing Java web applications much quicker. Publishing and redeploying every small change is eating too much time.

或者,我也可以接受其他选项(不同的应用服务器或ide),它们可以使开发Java web应用程序更快。发布和重新部署每一个小变化都占用了太多的时间。

I've tried JRebel and FileSync (and they both work to a certain degree) but they try and patch a problem that shouldn't exist.

我尝试过JRebel和FileSync(它们都在一定程度上起作用),但它们试图修补一个不应该存在的问题。

9 个解决方案

#1


9  

I've never really liked the WTP plugin that gets bundled with Eclipse.

我从来都不喜欢与Eclipse绑定的WTP插件。

In the past I've had a lot of success with the Sysdeo Tomcat plugin for Eclipse.

在过去,我成功地使用了Eclipse的Sysdeo Tomcat插件。

It uses the compiled classes that Eclipse builds for you, so when you make an interface-compatible change (like changing some stuff inside a method), this gets deployed immediately without requiring a restart. Changing method signatures or adding new methods within a class do require a restart, but since there's no lengthy WAR-building step, the whole build/deploy cycle is reduced anyway.

它使用Eclipse为您构建的编译类,因此,当您进行接口兼容的更改(如更改方法中的某些内容)时,无需重新启动就可以立即部署。更改方法签名或在类中添加新方法确实需要重新启动,但是由于没有冗长的构建步骤,因此整个构建/部署周期都减少了。

Additionally, the Sysdeo plugin uses the static assets from your workspace, so no need to copy or deploy these. Just make a change, refresh your browser, and you see the change right away.

此外,Sysdeo插件使用来自工作空间的静态资产,因此不需要复制或部署这些资产。只要做一个改变,刷新你的浏览器,你马上就能看到改变。

Unfortunately it looks like development of the plugin ground to a halt a couple of years ago. The latest supported version of Eclipse, according to the matrix on their website, is 3.6. According to this page however, the plugin still works with Eclipse 4.2 (Juno).

不幸的是,在几年前,这个插件的开发似乎停滞了。根据他们网站上的矩阵,最新支持的Eclipse版本是3.6。但是根据这个页面,插件仍然可以使用Eclipse 4.2 (Juno)。

Hope this helps. Using Sysdeo is really a much nicer experience than WTP!

希望这个有帮助。使用Sysdeo真的比WTP更好!

#2


5  

I do not want to come off as promoting anything, but Netbeans IDE supports all of these out of the box, no plugins necessary, it also includes a pre configured tomcat;

我不想做任何宣传,但是Netbeans IDE支持开箱即用的所有这些,没有必要的插件,它还包括一个预先配置的tomcat;

It supports copying static resources on save, so there is no need to rebuild your whole application:

它支持在save上复制静态资源,因此不需要重新构建整个应用程序:

Eclipse + Tomcat -直接从工作区提供web应用

As for any java changes, if you are in debug mode there is an Apply Code Changes button, which will rebuild and deploy your changed files (assuming no structural changes were made)

对于任何java更改,如果处于调试模式,则有一个Apply Code changes按钮,该按钮将重新构建和部署已更改的文件(假设没有进行结构性更改)

or alternatively use JRebel (for deploying changed java files) which has excellent features

或者也可以使用JRebel(用于部署已更改的java文件),它具有优秀的特性

#3


4  

I am pretty sure that Maven can do this kind of stuff. Maybe there are even Archetypes for Tomcat which have this kind of behaviour already configured.

我很确定Maven可以做这样的事情。可能Tomcat的原型已经配置了这种行为。

I am using a Maven Archetype together with JBoss AS. When I change my .css or .xhtml file or any other static resources and save it in Eclipse, the resources are immediatally synchronzied with my deployment.

我正在与JBoss AS一起使用Maven原型。当我更改.css或.xhtml文件或任何其他静态资源并将其保存在Eclipse中时,这些资源与我的部署立即同步。

Edit:

编辑:

Here is a quick tutorial for my solution:

下面是我的解决方案的快速教程:

What I am using:

我使用:

  • JBoss AS 7.1.1
  • JBoss是安装7.1.1
  • Eclipse Juno for Java EE developers
  • Java EE开发人员的Eclipse Juno。

Older or newer versions should also do the work.

旧版本或新版本也应该这样做。

  1. Install JBoss Tools Plug-In over the Eclipse Marketplace in Eclipse IDE
    This will install several Plug-Ins to your IDE, like the JBoss AS Server Connector or Maven.
  2. 在Eclipse IDE的Eclipse市场上安装JBoss工具插件,这将为您的IDE安装几个插件,比如JBoss作为服务器连接器或Maven。
  3. Add JBoss Repositories to your Maven configuration
    Window -> Preferences -> Maven -> User Settings -> open file
    Add the repositories from JBoss Maven Getting started
  4. 添加JBoss库到您的Maven配置窗口->首选项-> Maven ->用户设置->打开文件添加从JBoss Maven开始的存储库。
  5. Add a JBoss Server Runtime
    Window -> Preferences -> Servers -> Runtime Environments
    Follow the Wizard, pretty standard
  6. 添加JBoss服务器运行时窗口->首选项->服务器->运行环境遵循向导,相当标准。
  7. Add a Serverinstance from the Eclipse Servers Tab
  8. 从Eclipse Servers选项卡中添加一个Serverinstance。
  9. Create a new Maven Project
    Take care that the checkbox Create a simple project(skip archetype) is NOT checked
    Choose the Archetype jboss-javaee6-webapp-blank-archetype 7.1.3.CR7
  10. 创建一个新的Maven项目,注意创建一个简单项目的复选框(跳过原型)不被选中,选择原型jbo -javaee6-webapp-blank- prototype 7.1.3.CR7

Eclipse + Tomcat -直接从工作区提供web应用

Now your Project is ready to go:)

现在你的项目已经准备好了:)

#4


2  

It's unclear if you are using the Eclipse WTP approach with Tomcat or some custom deployment strategy.

不清楚您是在使用Tomcat的Eclipse WTP方法,还是使用一些自定义部署策略。

I switched away from running Tomcat in Eclipse because of unreliable re-publishing of changed JARs. I'm now using Jetty via the Run Jetty Run Eclipse plug-in (available in the Eclipse Marketplace). This appears to run from the workspace and does not do the republish-to-a-deployment-directory approach that Eclipse uses with Tomcat.

由于不可靠地重新发布已更改的jar,我不再在Eclipse中运行Tomcat。我现在通过运行Jetty运行Eclipse插件(在Eclipse市场上可以使用)使用Jetty。这似乎是在工作区中运行的,不执行Eclipse与Tomcat一起使用的重新发布到部署目录的方法。

You don't get dynamic code reload with this option, but you can add that feature back with JRebel.

使用此选项不会获得动态代码重载,但可以使用JRebel将该特性添加回。

Going further left-field, switch to a JVM language/framework that supports seeing your changes instantly without re-publishing/restarting such as:

再往左看,切换到JVM语言/框架,它支持立即看到更改,而无需重新发布/重新启动,例如:

  • Play 2 (Scala and Java)
  • 第二幕(Scala和Java)
  • Grails (Groovy)
  • Grails(Groovy)
  • Vert.X (various JVM languages).
  • 绿色。X(各种JVM语言)。

#5


1  

I suggest using sbt together with its web plugin.

我建议使用sbt和它的web插件。

The key feature of sbt is the continuous integration mode: This basically means, sbt fires a command, if a file changes.

sbt的关键特性是持续集成模式:这基本上意味着,如果文件发生更改,sbt将触发命令。

So normal sbt commands (related with the web plugin) are

所以普通的sbt命令(与web插件相关)就是这样

container:start
container:stop
container:reload / ... container:reload

容器:启动容器:停止容器:再装载/…容器:重载

but using it in the continuous integration mode (a command starting with ~)

但是在连续集成模式中使用它(以~开头的命令)

~;container:start; container:reload /

~;容器:开始;容器:重载/

it will automatically reload a web application when source code or any other files have changed.

当源代码或其他文件发生更改时,它将自动重新加载web应用程序。

So I just save servlets, Java sources, Scala sources whatever, and hit reload in the browser, and that really saves a lot of time.

所以我只保存servlet、Java源代码、Scala源代码等等,然后在浏览器中点击reload,这真的节省了很多时间。

This is the Grails feeling, but with Java as the main programming language.

这是Grails的感觉,但是Java是主要的编程语言。

Additional information

额外的信息

  • I have sbt running in a terminal (I am not aware if there is a plugin for Eclipse to start it from within Eclipse)
  • 我在一个终端中运行sbt(我不知道Eclipse是否有一个插件可以在Eclipse中启动它)
  • sbt can compile both Java and Scala projects out of the box.
  • sbt可以直接编译Java和Scala项目。
  • The web plugin is using jetty (I am not aware if it's possible to embed tomcat as well)
  • web插件正在使用jetty(我不知道是否也可以嵌入tomcat)
  • There is no need to know Scala to configure sbt, but you should read the tutorial.
  • 没有必要知道Scala配置sbt,但是您应该阅读教程。
  • This solution is not tied to the IDE; so it works without an IDE as well (just from the command line).
  • 此解决方案不绑定到IDE;因此,它在没有IDE的情况下也能工作(仅从命令行)。

#6


1  

This depends on whether you object to taking some action, like hitting Publish, or if you object to the copy happening even if it's transparent. Static content like CSS, HTML or JavaScript should get automatically deployed (i.e. copied) when you save, assuming you have "Automatically publish when resources change" under "Publishing" selected in Tomcat's server configuration. You should refresh in your browser and see the changes, barring some browser caching.

这取决于你是否反对采取一些行动,比如点击发布,或者如果你反对复制,即使它是透明的。如果您在Tomcat的服务器配置中选择了“发布”,那么在保存时,CSS、HTML或JavaScript等静态内容应该会自动部署(例如复制)。您应该在浏览器中刷新并查看更改,除非有一些浏览器缓存。

If you truly want Tomcat to look directly at your project's static files, then run Tomcat with nothing deployed (unless you have Java classes to deploy in the same project) and edit the server.xml file under Servers > Apache Tomcat v7.0 at localhost-config and add some Context elements under Host like this:

如果您真的希望Tomcat直接查看您的项目的静态文件,那么运行Tomcat(除非您有Java类在同一个项目中部署)并编辑服务器。服务器> Apache Tomcat v7.0下的xml文件,在localhost-config中添加一些上下文元素,如下所示:

<Context docBase="C:\path\workspace\project\WebContent\css" path="/project/css" reloadable="true" />

#7


0  

By default it shares only the apps located in $CATALINA_HOME/webapps. You can change it in the $CATALINA_BASE/conf/server.xml in the <Host />.

默认情况下,它只共享位于$CATALINA_HOME/webapps中的应用。您可以在$CATALINA_BASE/conf/server中更改它。在 <主机 />中。

Check the documentation for attribute appBase.

查看属性appBase的文档。

Hope it helped.

希望它帮助。

#8


0  

Use grizzly webserver. It's completely implemented in java, hence you have full plattform independency and you can start it directly from your workspace without configuring any external programs. It's easy to deploy resources statically. You just have to derive javax.ws.rs.core.Application and add your resources as in the example.

灰熊网络服务器使用。它完全是在java中实现的,因此您具有完全的plattform独立性,您可以直接从您的工作空间启动它,而无需配置任何外部程序。静态部署资源很容易。你只需要推导javax.ws.r .core。应用程序并添加您的资源,如示例中所示。

import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.core.Application;

/**
 * Application class that contains resources for the RESTful web service.
 *
 */
public class MyApplication extends Application {
    public Set<Class<?>> getClasses() {
        Set<Class<?>> s = new HashSet<Class<?>>();
        s.add(com.rest.test.SomeClass.class);
        return s;
    }
}

This is needed to configure the servlet adapter. It's also possible to add resources dynamically. But I can't tell you how fast the updating of the resources is with the dynamic approach. Anyway there is enough documentation available online.

这是配置servlet适配器所必需的。也可以动态地添加资源。但是我不能告诉您使用动态方法更新资源的速度有多快。无论如何,网上有足够的文档。

private static GrizzlyWebServer getServer(int port,
    String webResourcesPath, String contextPath) {
    GrizzlyWebServer gws = new GrizzlyWebServer(port, webResourcesPath);

    ServletAdapter sa = new ServletAdapter();

    /* here they are added statically */
    sa.addInitParameter("javax.ws.rs.Application", "com.rest.MyApplication");


    sa.setContextPath(contextPath);
    sa.setServletInstance(new ServletContainer());
    sa.setProperty("load-on-startup", 1);
    gws.addGrizzlyAdapter(sa, new String[] { contextPath });

    return gws;

}

}

#9


-1  

Normally webapplications are hosted under webapps directory, but its also possible to configure an external directory as a webapplication host directory in tomcat. You can simply set your eclipse workspace project output directory as an application base. This can be done either by using Tomcat's manager application to deploy an application from an external directory or by simply editing server.xml (which is under conf directory) to define your application like below:

通常web应用程序托管在webapps目录下,但也可以将外部目录配置为tomcat中的webapplication host目录。您可以简单地将eclipse工作区项目输出目录设置为应用程序库。这可以通过使用Tomcat的manager应用程序从外部目录部署应用程序或通过简单的编辑服务器来实现。xml(在conf目录下)定义您的应用程序如下:

....

<Context docBase="D:\your\eclipse\workspace\project\WebContent" path="/projectbaseurl"   reloadable="true"/>

</Host>
</Engine>
</Service>
</Server>

#1


9  

I've never really liked the WTP plugin that gets bundled with Eclipse.

我从来都不喜欢与Eclipse绑定的WTP插件。

In the past I've had a lot of success with the Sysdeo Tomcat plugin for Eclipse.

在过去,我成功地使用了Eclipse的Sysdeo Tomcat插件。

It uses the compiled classes that Eclipse builds for you, so when you make an interface-compatible change (like changing some stuff inside a method), this gets deployed immediately without requiring a restart. Changing method signatures or adding new methods within a class do require a restart, but since there's no lengthy WAR-building step, the whole build/deploy cycle is reduced anyway.

它使用Eclipse为您构建的编译类,因此,当您进行接口兼容的更改(如更改方法中的某些内容)时,无需重新启动就可以立即部署。更改方法签名或在类中添加新方法确实需要重新启动,但是由于没有冗长的构建步骤,因此整个构建/部署周期都减少了。

Additionally, the Sysdeo plugin uses the static assets from your workspace, so no need to copy or deploy these. Just make a change, refresh your browser, and you see the change right away.

此外,Sysdeo插件使用来自工作空间的静态资产,因此不需要复制或部署这些资产。只要做一个改变,刷新你的浏览器,你马上就能看到改变。

Unfortunately it looks like development of the plugin ground to a halt a couple of years ago. The latest supported version of Eclipse, according to the matrix on their website, is 3.6. According to this page however, the plugin still works with Eclipse 4.2 (Juno).

不幸的是,在几年前,这个插件的开发似乎停滞了。根据他们网站上的矩阵,最新支持的Eclipse版本是3.6。但是根据这个页面,插件仍然可以使用Eclipse 4.2 (Juno)。

Hope this helps. Using Sysdeo is really a much nicer experience than WTP!

希望这个有帮助。使用Sysdeo真的比WTP更好!

#2


5  

I do not want to come off as promoting anything, but Netbeans IDE supports all of these out of the box, no plugins necessary, it also includes a pre configured tomcat;

我不想做任何宣传,但是Netbeans IDE支持开箱即用的所有这些,没有必要的插件,它还包括一个预先配置的tomcat;

It supports copying static resources on save, so there is no need to rebuild your whole application:

它支持在save上复制静态资源,因此不需要重新构建整个应用程序:

Eclipse + Tomcat -直接从工作区提供web应用

As for any java changes, if you are in debug mode there is an Apply Code Changes button, which will rebuild and deploy your changed files (assuming no structural changes were made)

对于任何java更改,如果处于调试模式,则有一个Apply Code changes按钮,该按钮将重新构建和部署已更改的文件(假设没有进行结构性更改)

or alternatively use JRebel (for deploying changed java files) which has excellent features

或者也可以使用JRebel(用于部署已更改的java文件),它具有优秀的特性

#3


4  

I am pretty sure that Maven can do this kind of stuff. Maybe there are even Archetypes for Tomcat which have this kind of behaviour already configured.

我很确定Maven可以做这样的事情。可能Tomcat的原型已经配置了这种行为。

I am using a Maven Archetype together with JBoss AS. When I change my .css or .xhtml file or any other static resources and save it in Eclipse, the resources are immediatally synchronzied with my deployment.

我正在与JBoss AS一起使用Maven原型。当我更改.css或.xhtml文件或任何其他静态资源并将其保存在Eclipse中时,这些资源与我的部署立即同步。

Edit:

编辑:

Here is a quick tutorial for my solution:

下面是我的解决方案的快速教程:

What I am using:

我使用:

  • JBoss AS 7.1.1
  • JBoss是安装7.1.1
  • Eclipse Juno for Java EE developers
  • Java EE开发人员的Eclipse Juno。

Older or newer versions should also do the work.

旧版本或新版本也应该这样做。

  1. Install JBoss Tools Plug-In over the Eclipse Marketplace in Eclipse IDE
    This will install several Plug-Ins to your IDE, like the JBoss AS Server Connector or Maven.
  2. 在Eclipse IDE的Eclipse市场上安装JBoss工具插件,这将为您的IDE安装几个插件,比如JBoss作为服务器连接器或Maven。
  3. Add JBoss Repositories to your Maven configuration
    Window -> Preferences -> Maven -> User Settings -> open file
    Add the repositories from JBoss Maven Getting started
  4. 添加JBoss库到您的Maven配置窗口->首选项-> Maven ->用户设置->打开文件添加从JBoss Maven开始的存储库。
  5. Add a JBoss Server Runtime
    Window -> Preferences -> Servers -> Runtime Environments
    Follow the Wizard, pretty standard
  6. 添加JBoss服务器运行时窗口->首选项->服务器->运行环境遵循向导,相当标准。
  7. Add a Serverinstance from the Eclipse Servers Tab
  8. 从Eclipse Servers选项卡中添加一个Serverinstance。
  9. Create a new Maven Project
    Take care that the checkbox Create a simple project(skip archetype) is NOT checked
    Choose the Archetype jboss-javaee6-webapp-blank-archetype 7.1.3.CR7
  10. 创建一个新的Maven项目,注意创建一个简单项目的复选框(跳过原型)不被选中,选择原型jbo -javaee6-webapp-blank- prototype 7.1.3.CR7

Eclipse + Tomcat -直接从工作区提供web应用

Now your Project is ready to go:)

现在你的项目已经准备好了:)

#4


2  

It's unclear if you are using the Eclipse WTP approach with Tomcat or some custom deployment strategy.

不清楚您是在使用Tomcat的Eclipse WTP方法,还是使用一些自定义部署策略。

I switched away from running Tomcat in Eclipse because of unreliable re-publishing of changed JARs. I'm now using Jetty via the Run Jetty Run Eclipse plug-in (available in the Eclipse Marketplace). This appears to run from the workspace and does not do the republish-to-a-deployment-directory approach that Eclipse uses with Tomcat.

由于不可靠地重新发布已更改的jar,我不再在Eclipse中运行Tomcat。我现在通过运行Jetty运行Eclipse插件(在Eclipse市场上可以使用)使用Jetty。这似乎是在工作区中运行的,不执行Eclipse与Tomcat一起使用的重新发布到部署目录的方法。

You don't get dynamic code reload with this option, but you can add that feature back with JRebel.

使用此选项不会获得动态代码重载,但可以使用JRebel将该特性添加回。

Going further left-field, switch to a JVM language/framework that supports seeing your changes instantly without re-publishing/restarting such as:

再往左看,切换到JVM语言/框架,它支持立即看到更改,而无需重新发布/重新启动,例如:

  • Play 2 (Scala and Java)
  • 第二幕(Scala和Java)
  • Grails (Groovy)
  • Grails(Groovy)
  • Vert.X (various JVM languages).
  • 绿色。X(各种JVM语言)。

#5


1  

I suggest using sbt together with its web plugin.

我建议使用sbt和它的web插件。

The key feature of sbt is the continuous integration mode: This basically means, sbt fires a command, if a file changes.

sbt的关键特性是持续集成模式:这基本上意味着,如果文件发生更改,sbt将触发命令。

So normal sbt commands (related with the web plugin) are

所以普通的sbt命令(与web插件相关)就是这样

container:start
container:stop
container:reload / ... container:reload

容器:启动容器:停止容器:再装载/…容器:重载

but using it in the continuous integration mode (a command starting with ~)

但是在连续集成模式中使用它(以~开头的命令)

~;container:start; container:reload /

~;容器:开始;容器:重载/

it will automatically reload a web application when source code or any other files have changed.

当源代码或其他文件发生更改时,它将自动重新加载web应用程序。

So I just save servlets, Java sources, Scala sources whatever, and hit reload in the browser, and that really saves a lot of time.

所以我只保存servlet、Java源代码、Scala源代码等等,然后在浏览器中点击reload,这真的节省了很多时间。

This is the Grails feeling, but with Java as the main programming language.

这是Grails的感觉,但是Java是主要的编程语言。

Additional information

额外的信息

  • I have sbt running in a terminal (I am not aware if there is a plugin for Eclipse to start it from within Eclipse)
  • 我在一个终端中运行sbt(我不知道Eclipse是否有一个插件可以在Eclipse中启动它)
  • sbt can compile both Java and Scala projects out of the box.
  • sbt可以直接编译Java和Scala项目。
  • The web plugin is using jetty (I am not aware if it's possible to embed tomcat as well)
  • web插件正在使用jetty(我不知道是否也可以嵌入tomcat)
  • There is no need to know Scala to configure sbt, but you should read the tutorial.
  • 没有必要知道Scala配置sbt,但是您应该阅读教程。
  • This solution is not tied to the IDE; so it works without an IDE as well (just from the command line).
  • 此解决方案不绑定到IDE;因此,它在没有IDE的情况下也能工作(仅从命令行)。

#6


1  

This depends on whether you object to taking some action, like hitting Publish, or if you object to the copy happening even if it's transparent. Static content like CSS, HTML or JavaScript should get automatically deployed (i.e. copied) when you save, assuming you have "Automatically publish when resources change" under "Publishing" selected in Tomcat's server configuration. You should refresh in your browser and see the changes, barring some browser caching.

这取决于你是否反对采取一些行动,比如点击发布,或者如果你反对复制,即使它是透明的。如果您在Tomcat的服务器配置中选择了“发布”,那么在保存时,CSS、HTML或JavaScript等静态内容应该会自动部署(例如复制)。您应该在浏览器中刷新并查看更改,除非有一些浏览器缓存。

If you truly want Tomcat to look directly at your project's static files, then run Tomcat with nothing deployed (unless you have Java classes to deploy in the same project) and edit the server.xml file under Servers > Apache Tomcat v7.0 at localhost-config and add some Context elements under Host like this:

如果您真的希望Tomcat直接查看您的项目的静态文件,那么运行Tomcat(除非您有Java类在同一个项目中部署)并编辑服务器。服务器> Apache Tomcat v7.0下的xml文件,在localhost-config中添加一些上下文元素,如下所示:

<Context docBase="C:\path\workspace\project\WebContent\css" path="/project/css" reloadable="true" />

#7


0  

By default it shares only the apps located in $CATALINA_HOME/webapps. You can change it in the $CATALINA_BASE/conf/server.xml in the <Host />.

默认情况下,它只共享位于$CATALINA_HOME/webapps中的应用。您可以在$CATALINA_BASE/conf/server中更改它。在 <主机 />中。

Check the documentation for attribute appBase.

查看属性appBase的文档。

Hope it helped.

希望它帮助。

#8


0  

Use grizzly webserver. It's completely implemented in java, hence you have full plattform independency and you can start it directly from your workspace without configuring any external programs. It's easy to deploy resources statically. You just have to derive javax.ws.rs.core.Application and add your resources as in the example.

灰熊网络服务器使用。它完全是在java中实现的,因此您具有完全的plattform独立性,您可以直接从您的工作空间启动它,而无需配置任何外部程序。静态部署资源很容易。你只需要推导javax.ws.r .core。应用程序并添加您的资源,如示例中所示。

import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.core.Application;

/**
 * Application class that contains resources for the RESTful web service.
 *
 */
public class MyApplication extends Application {
    public Set<Class<?>> getClasses() {
        Set<Class<?>> s = new HashSet<Class<?>>();
        s.add(com.rest.test.SomeClass.class);
        return s;
    }
}

This is needed to configure the servlet adapter. It's also possible to add resources dynamically. But I can't tell you how fast the updating of the resources is with the dynamic approach. Anyway there is enough documentation available online.

这是配置servlet适配器所必需的。也可以动态地添加资源。但是我不能告诉您使用动态方法更新资源的速度有多快。无论如何,网上有足够的文档。

private static GrizzlyWebServer getServer(int port,
    String webResourcesPath, String contextPath) {
    GrizzlyWebServer gws = new GrizzlyWebServer(port, webResourcesPath);

    ServletAdapter sa = new ServletAdapter();

    /* here they are added statically */
    sa.addInitParameter("javax.ws.rs.Application", "com.rest.MyApplication");


    sa.setContextPath(contextPath);
    sa.setServletInstance(new ServletContainer());
    sa.setProperty("load-on-startup", 1);
    gws.addGrizzlyAdapter(sa, new String[] { contextPath });

    return gws;

}

}

#9


-1  

Normally webapplications are hosted under webapps directory, but its also possible to configure an external directory as a webapplication host directory in tomcat. You can simply set your eclipse workspace project output directory as an application base. This can be done either by using Tomcat's manager application to deploy an application from an external directory or by simply editing server.xml (which is under conf directory) to define your application like below:

通常web应用程序托管在webapps目录下,但也可以将外部目录配置为tomcat中的webapplication host目录。您可以简单地将eclipse工作区项目输出目录设置为应用程序库。这可以通过使用Tomcat的manager应用程序从外部目录部署应用程序或通过简单的编辑服务器来实现。xml(在conf目录下)定义您的应用程序如下:

....

<Context docBase="D:\your\eclipse\workspace\project\WebContent" path="/projectbaseurl"   reloadable="true"/>

</Host>
</Engine>
</Service>
</Server>