Jetty学习二.Eclipse中安装Jetty插件

安装和运行jetty

下载jetty的distribution压缩包之后,解压,在终端或者命令提示符中进入解压出来的jetty-distribution-xxx目录(jetty_home),执行

[html] view plain copy
  1. java -jar start.jar  


即可启动jetty(一定要在jetty_home目录下执行这条命令,不然会报filenotfound错误),默认启动的http端口和tomcat一样,都是8080

在jetty_home/webapps/目录下的war文件就是要启动的web项目。

要在eclipse jee中使用jetty,我尝试了下面两种方法。

1.添加jetty server adapter

一种是在eclipse的server视图中添加jetty的适配器,但是在eclipse jee Helios里面只能貌似只有jetty 6的适配器(jetty版本更新很快,其实jetty6是09年发布的,不算很古老),可能高版本的eclipse可以支持更高版本的jetty吧。

下载jetty6:

http://dist.codehaus.org/jetty/jetty-6.1.x/jetty-6.1.3.zip

打开eclipse,在Window->Preference中选择Server->Runtime Environments:

点击右侧Add:

扫描二维码关注公众号,回复: 634750 查看本文章

点击Download additional server adapters:

待更新完了,会出现Jetty Generic Server Adapter,选中,点Next然后一步步安装就好了。

在eclipse的servers视图中新建Server:

选择jetty6,下面一步步配置即可,Jetty_home选择刚才解压出来的jetty6的目录即可,端口不要和其他服务冲突。

在eclipse中新建一个dynamic web project,dynamic web module version选择2.2,在Web content下写一个index.jsp,右击工程,Run as->run on server,选择刚才新建的jetty 6 at localhost,就可以看到效果了,和使用tomcat没有什么不一样。

2.安装run-jetty-run插件

另外一种方法是在eclipse中安装run-jetty-run插件,我觉得这种方式比较好,可以支持jetty7、8,安装方法如下:

在eclipse菜单栏,Run->Install new software->Add,输入地址:http://run-jetty-run.googlecode.com/svn/trunk/updatesite,如下图:

OK,然后勾选RunJettyRun即可

之后一步步安装即可。

安装之后连jetty都不需要安装,插件中带了jetty6、7、8。新建dynamic web project,右击工程,Run as->Run configuration:

(首次操作时,在run configuration里可能没有出现工程,点一下左上角的新建按钮即可)会有一套缺省的配置,根据需要修改端口和其他配置之后,run就可以了。配置详见

http://code.google.com/p/run-jetty-run/wiki/GettingStarted

http://code.google.com/p/run-jetty-run/wiki/RJRConfigurations

打开浏览器,输入地址http://localhost:8080/jettytest即可查看刚才的web工程.

解决用run-jetty-run锁住css,js文件的问题

开 发中用run-jetty-run插件启动jetty调式tapestry5应用。tapestry5的live class loader用起来非常爽, 不管你改page class还是html模板都不用重启server。 但是有一个例外,那就是jetty起来之后css, js文件会被jetty锁住, 然后用eclipse修改不了。 所以改css js都非常麻烦, 每改一下就要重启下jetty。google之后发现原来: 
引用Jetty buffers static content for webapps such as html files, css files, images etc and uses memory mapped files to do this if the NIO connectors are being used. The problem is that on Windows, memory mapping a file causes the file to be locked, so that the file cannot be updated or replaced. This means that effectively you have to stop Jetty in order to update a file.

怪不得以前在ubuntu下没有这个问题,转到windows下就发现这个问题了。 

解决办法就是找到run-jetty-run插件里面的jetty.jar。jetty.jar可以在eclipse中的jetty启动里面的Classpath中找到。 看下图 


找到jetty.jar后解压,编辑org/mortbay/jetty/webapp/webdefault.xml这个文件。把useFileMappedBuffer改成false。这里也就是禁用memory mapped file.
<init-param>  
    <param-name>useFileMappedBuffer</param-name>  
    <param-value>true</param-value> <!-- change to false -->  
</init-param>  
 
 

猜你喜欢

转载自zyjustin9.iteye.com/blog/2056131