Android裝置上i-jetty環境的搭建

來源:互聯網
上載者:User

編譯i-jetty

1、將源碼download下來,http://code.google.com/p/i-jetty/downloads/list

2、解壓檔案,進入pom.xml所在目錄,執行命令:mvncleaninstall,參照http://code.google.com/p/i-jetty/wiki/BuildInstructions)

執行過程中,出現了異常導致失敗,是產生classes.dex時記憶體溢出的問題,我們用Eclipse來解決吧。

3、建立i-jetty的android項目,將下載的i-jetty源碼覆蓋進去,

4、將下面目錄下的jar引入到i-jetty項目中:

C:\DocumentsandSettings\troyz\.m2\repository\org\eclipse\jetty

還需要servlet-api-2.5.jar。

5、編譯並將i-jetty安裝到裝置中。

將web項目發布到裝置上去。參照http://code.google.com/p/i-jetty/wiki/DownloadableWebapps)

因為android上的java虛擬機器不能直接解釋執行.class檔案,所以首先需要把web項目中的.class檔案和.jar檔案轉換成虛擬機器能識別的.dex檔案。

這裡假設web項目的目錄在:E:\demo。

1、需要將WEB-INF/classes目錄和lib目錄下的檔案,用dx命令處理成classes.dex並放到lib目錄下。

dx.bat--dex--output=E:\demo\WEB-INF\lib\classes.zipE:\demo\WEB-INF\classesE:\demo\WEB-INF\lib

2、打成war包。

cd E:\demo

jar-cvfdemo.war*.*

3、產生classes.zip後,可以將原先的.class和.jar刪除掉。

4、通過i-jetty的download功能,把war下載到裝置上去,也可以直接把demo.war放到/sdcard/jetty/webapps/目錄下。

5、啟動i-jetty伺服器。

i-jetty對jsp的支援暫時似乎不完美,可能是我沒有找到好的方式,對html是支援的,包括html中的js,支援servlet。

androidtablet(sdk3.0)上,i-jetty的類載入器無法動態解壓載入classes.zip,報以下異常:

dalvikvm(22984):Can'topendexcache'/data/dalvik-cache/mnt@sdcard@JettyContext217761537@webinf@WEB-INF@lib@classes.zip@classes.dex':Nosuchfileordirectory

原因是/data/dalvik-cache目錄不可寫。

http://code.google.com/p/android/issues/detail?id=966

有一種解決方案:在打包android項目時,把web項目中的java檔案和jar檔案放到android項目中一起打包,這樣就避免讓i- jetty動態載入dex檔案了。war包中就不需要classes.zip,jar,.class檔案了。但是這種方案有一個缺點:不能動態部署新的 war包。

6、如果要在Web程式中,使用android的Context,需要這樣做:

  1. public void init(ServletConfig config) throws ServletException   
  2. {   
  3.     super.init(config);   
  4.     android.content.ContentResolver resolver = (android.content.ContentResolver) config.getServletContext().getAttribute("org.mortbay.ijetty.contentResolver");   
  5.     android.content.Context androidContext = (android.content.Context) config.getServletContext().getAttribute("org.mortbay.ijetty.context");   
  6. }  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.