将jetty服务器嵌入到Android应用程序中

时间:2013-05-30 09:20:41

标签: java android jetty war embedded-jetty

到目前为止,我正在尝试将示例Android应用程序设置为嵌入式android jetty Web服务器,以便从Google代码运行示例hello-3.1.war。当我在eclipse中运行hello-3.1时,就发生了空指针异常。

HTTP ERROR 500

Problem accessing /hello-3.1/sayit. Reason:

    INTERNAL_SERVER_ERROR

Caused by:

java.lang.NullPointerException
    at org.mortbay.ijetty.hello.HelloWorld.init(HelloWorld.java:29)
    at org.mortbay.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:440)
    at org.mortbay.jetty.servlet.ServletHolder.getServlet(ServletHolder.java:339)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:401)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:766)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:450)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:326)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
    at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:928)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:549)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
    at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:410)
    at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)

Powered by Jetty://

Hello-3.1网络应用程序编码:

WEB.XML

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
    version="3.0"> 
 <!--  <servlet id="jsp">
     <servlet-name>jsp</servlet-name>
     <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
     <init-param>
         <param-name>logVerbosityLevel</param-name>
         <param-value>DEBUG</param-value>
     </init-param>
     <init-param>
         <param-name>fork</param-name>
         <param-value>false</param-value>
     </init-param>
     <init-param>
         <param-name>xpoweredBy</param-name>
         <param-value>false</param-value>
     </init-param>
     <load-on-startup>0</load-on-startup>
</servlet> -->
  <servlet>
    <servlet-name>hello</servlet-name>
    <servlet-class>org.mortbay.ijetty.hello.HelloWorld</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>hello</servlet-name>
    <url-pattern>/sayit/*</url-pattern>
  </servlet-mapping>


</web-app>

的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <parent>
    <groupId>org.mortbay.ijetty</groupId>
    <artifactId>example-webapps-parent</artifactId>
    <version>3.1</version>
    <relativePath>../pom.xml</relativePath>
  </parent>
  <modelVersion>4.0.0</modelVersion>
  <artifactId>hello</artifactId>
  <name>I-Jetty :: Hello</name>
  <version>3.1</version>
  <packaging>war</packaging>
  <url>http://maven.apache.org</url>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.5</source>
          <target>1.5</target>
          <verbose>false</verbose>
        </configuration>
      </plugin>

          <!-- Convert the compiled classes into a clases.dex. -->
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.1</version>
        <executions>
          <execution>
            <id>generate-dex</id>
            <phase>process-classes</phase>
            <goals>
              <goal>exec</goal>
            </goals>
            <configuration>
              <!-- executable>${env.ANDROID_HOME}/platform-tools/dx</executable -->
              <executable>java</executable>
              <arguments>
               <!-- <argument>-JXmx1024M</argument> -->
                <argument>-jar</argument>
                <argument>${env.ANDROID_HOME}/platform-tools/lib/dx.jar</argument>
                <argument>--dex</argument>
                <argument>--verbose</argument>
                <argument>--core-library</argument>
                <argument>--output=${project.build.directory}/classes.dex</argument>
                <argument>--positions=lines</argument>
                <argument>${project.build.directory}/classes/</argument>
              </arguments>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <executions>
          <execution>
            <id>copydex</id>
            <phase>process-classes</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <mkdir
                  dir="${project.build.directory}/${project.artifactId}-${project.version}/WEB-INF/lib" />
                <jar
                  basedir="${project.build.directory}"
                  update="true"
                  includes="classes.dex"
                  destfile="${project.build.directory}/${project.artifactId}-${project.version}/WEB-INF/lib/classes.zip" />
              </tasks>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  <dependencies>
   <dependency>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>servlet-api-2.5</artifactId>
    <version>2.5</version>


      <scope>provided</scope>
     </dependency>
 <!-- <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-jsp-2.1</artifactId>
        <version>7.2.2.v20101205</version>
    </dependency> -->
   <!--  <dependency>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>jsp-2.1-glassfish</artifactId>
        <version>2.1.v20100127</version>
    </dependency>-->
  <!--   <dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>servlet-api</artifactId>
  <version>2.5</version>
  <scope>provided</scope>
</dependency> -->
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>jsp_api</artifactId>
  <version>2.0</version>
  <scope>provided</scope>
</dependency>
    <dependency> 
      <groupId>com.google.android</groupId>
      <artifactId>android</artifactId>
      <version>${android.version}</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>
</project>

2 个答案:

答案 0 :(得分:2)

在Android上无法使用JSP。

或者更准确一点,在Android设备上编译JSP是不可能的。

您需要做什么:

  1. 从web.xml或webdefault.xml中删除JspServlet。无论如何,你无法在Android上使用它。
  2. 在开发机器上预编译JSP,并在web.xml中包含已编译的JSP引用。看看那里的各种JSPC工具,它们都支持这一点。
  3. 使用i-jetty项目中列出的技术对经过编译后的WAR文件进行Dexify。
  4. 这将允许您使用JSP进行开发,并在Android上使用它们,但不是原始*.jsp文件本身,而是编译后的*.class文件(然后将其解密为Android字节码)格式)。

答案 1 :(得分:0)

你可以看看NanoHTTPDserver,它是一个在Android上运行的轻型网络服务器,你必须自己做很多处理,但它可能只是服务于你的目的。

NanoHTTPD gist link