---
title: "android上的i-jetty"
author: "llhdf"
date: "2012-10-17T17:44:03Z"
tags: []
categories: ["Web前端"]
slug: "android-i-jetty"
draft: false
lastmod: "2012-10-17T17:44:03Z"
id: 1700485
url: "https://llhdf.iteye.com/blog/1700485"
---

http://wenku.baidu.com/view/f637b256f01dc281e53af0f3.html

http://www.cnblogs.com/java-koma/archive/2012/02/07/2341791.html


android上的i-jetty （1）环境搭建

介绍下如果把android设备作为一个web服务器使用，

 

 

编译i-jetty

1. 将源码download下来，http://code.google.com/p/i-jetty/downloads/list

2. 解压文件，进入pom.xml所在目录，执行命令: mvn clean install, 参照（http://code.google.com/p/i-jetty/wiki/BuildInstructions）

   执行过程中，出现了异常导致失败，是生成classes.dex时内存溢出的问题，我们用eclipse来解决吧。

3. 创建i-jetty的android项目，将下载的i-jetty源码覆盖进去，

4. 将下面目录下的 jar引入到i-jetty项目中：

   C:\Documents and Settings\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.zip E:\demo\WEB-INF\classes E:\demo\WEB-INF\lib

2. 打成war包

   cd E:\demo

   jar -cvf demo.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

 

android tablet(sdk 3.0)上，i-jetty的类加载器无法动态解压加载classes.zip， 报以下异常：

dalvikvm(22984): Can't open dex cache '/data/dalvik-cache/mnt@sdcard@JettyContext217761537@webinf@WEB-INF@lib@classes.zip@classes.dex': No such file or directory
原因是/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，需要这样做：


public void init(ServletConfig config) throws ServletException
{
    super.init(config);
    android.content.ContentResolver resolver = (android.content.ContentResolver) config.getServletContext().getAttribute("org.mortbay.ijetty.contentResolver");
    android.content.Context androidContext = (android.content.Context) config.getServletContext().getAttribute("org.mortbay.ijetty.context");
}