0

到目前为止,我正在尝试将示例 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 应用程序编码:

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>
4

2 回答 2

2

JSP 在 Android 上是不可能的。

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

你需要做什么:

  1. 从 web.xml 或 webdefault.xml 中删除 JspServlet。无论如何,您无法在Android上使用它。
  2. 在您的开发机器上预编译您的 JSP,并在您的 web.xml 中包含已编译的 JSP 引用。看看那里的各种 JSPC 工具,它们都支持这一点。
  3. 使用 i-jetty 项目中概述的技术对后编译的 WAR 文件进行 Dexify。

这将允许您使用 JSP 进行开发,并在 Android 上使用它们,但不是作为原始*.jsp文件本身,而是作为编译*.class文件(然后将其分解为 Android 字节码格式)。

于 2013-05-30T14:53:26.443 回答
0

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

NanoHTTPD 要点链接

于 2014-08-04T07:44:00.760 回答