Embedding jetty server into android applications

2019-06-11 00:45发布

So far i am trying making sample android applications to embedded android jetty web server enables to run samples hello-3.1.war from Google code. While i run hello-3.1 in eclipse then null pointer exception has been occur.

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 application Codings:

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条回答
Luminary・发光体
2楼-- · 2019-06-11 00:54

You could have a look over NanoHTTPDserver, it's a light web-server which runs in Android, you'll have to do a lot of handling yourself but it may just server your purposes.

NanoHTTPD gist link

查看更多
一纸荒年 Trace。
3楼-- · 2019-06-11 00:57

JSP is not possible on Android.

Or to be more accurate, compiling JSP on an Android device is not possible.

What you need to do:

  1. Remove the JspServlet from your web.xml or webdefault.xml. You can't use it on Android anyway.
  2. Pre-compile your JSP's on your development machine and include the compiled JSP references in your web.xml. Look at the various JSPC tooling out there, they all support this.
  3. Dexify the post-compiled WAR file using the techniques outlined at the i-jetty project.

This will allow you to develop with JSPs, and use them on Android, but not as the raw *.jsp file itself, but rather the compiled *.class files (which are then dexified into the Android bytecode format).

查看更多
登录 后发表回答