Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 嵌入式Jetty服务器-不支持/的JSP,未找到org.apache.jasper.servlet.JspServlet_Java_Jsp_Jetty_Embedded Jetty - Fatal编程技术网

Java 嵌入式Jetty服务器-不支持/的JSP,未找到org.apache.jasper.servlet.JspServlet

Java 嵌入式Jetty服务器-不支持/的JSP,未找到org.apache.jasper.servlet.JspServlet,java,jsp,jetty,embedded-jetty,Java,Jsp,Jetty,Embedded Jetty,我有以下代码来使用嵌入式Jetty服务器以及简单的servlet和.jsp网页。但是,在编译和运行代码之后: javac -cp lib/servlet-api.jar:lib/jetty-all.jar com/test/MyServlet.java javac -cp lib/servlet-api.jar:lib/jetty-all.jar com/test/ServerMain.java java -cp .:lib/servlet-api.jar:lib/jetty-all.jar

我有以下代码来使用嵌入式Jetty服务器以及简单的servlet和.jsp网页。但是,在编译和运行代码之后:

javac -cp lib/servlet-api.jar:lib/jetty-all.jar com/test/MyServlet.java 
javac -cp lib/servlet-api.jar:lib/jetty-all.jar com/test/ServerMain.java 
java -cp .:lib/servlet-api.jar:lib/jetty-all.jar com/test/ServerMain
我得到一个错误:

INFO:oejw.StandardDescriptorProcessor:main: NO JSP Support for /, did not find org.apache.jasper.servlet.JspServlet
导航到/index.jsp会出现500错误

HTTP ERROR 500
Problem accessing /index.jsp. 
Reason:
JSP support not configured
我读过这篇文章,但我认为解决方案不适用于这里,因为我正在运行Jetty embedded,而不是使用start.jar

如何解决此错误,使服务器能够成功运行和服务.jsp页面

ServerMain.java

package com.test;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;

public class ServerMain {

    public static void main(String[] args) throws InterruptedException {

        Server server = new Server(8080);
        WebAppContext webApp = new WebAppContext();
        webApp.setDescriptor("web.xml");
        webApp.setResourceBase("");
        webApp.setParentLoaderPriority(true);
        server.setHandler(webApp);

        try {
            server.start();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(-1);
        }
        server.join();
    }
}
package com.test;

import java.io.IOException;
import java.util.Properties;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        resp.setContentType("text/plain");
        resp.getWriter().println("Hello, this is a testing servlet. \n\n");
        Properties p = System.getProperties();
        p.list(resp.getWriter());

    }
}
MyServlet.java

package com.test;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;

public class ServerMain {

    public static void main(String[] args) throws InterruptedException {

        Server server = new Server(8080);
        WebAppContext webApp = new WebAppContext();
        webApp.setDescriptor("web.xml");
        webApp.setResourceBase("");
        webApp.setParentLoaderPriority(true);
        server.setHandler(webApp);

        try {
            server.start();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(-1);
        }
        server.join();
    }
}
package com.test;

import java.io.IOException;
import java.util.Properties;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        resp.setContentType("text/plain");
        resp.getWriter().println("Hello, this is a testing servlet. \n\n");
        Properties p = System.getProperties();
        p.list(resp.getWriter());

    }
}
web.xml

   <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE web-app PUBLIC "-//Oracle Corporation//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
    <servlet>
        <servlet-name>MyServlet</servlet-name>
        <servlet-class>com.test.MyServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>MyServlet</servlet-name>
        <url-pattern>/test</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

您似乎缺少一个JAR文件,其中包含类
org.apache.jasper.servlet.JspServlet
。下载一个包含它的JAR文件(look),并将其添加到类路径中。另外,在旁注中,您应该将
com/test/ServerMain
替换为真正的类名
com.test.ServerMain
。您的java语句应该如下所示:

java-cp.“:lib/servlet api.jar:lib/jetty all.jar:lib/apache jasper.jar”com.test.ServerMain

顺便提一下,Jetty项目维护了一个github项目,演示了Jetty Embedded中的JSP支持


没有尝试使用嵌入式Jetty,但在将Jetty 9.3作为服务运行时,需要添加JSP支持

cd $JETTY_BASE
$JAVA_HOME/bin/java -jar $JETTY_HOME/start.jar --add-to-startd=jsp

其中
JETTY\u BASE
是您部署应用程序的文件夹,该应用程序独立于
JETTY\u HOME
。所以我猜嵌入式Jetty也需要类似的配置。

您需要用类名调用java,所以
com.test.ServerMain
而不是
com/test/ServerMain
(因为javac是正确的)谢谢@msrd0,实际上,它使用
java com.test.ServerMain
java com/test/ServerMain
成功运行,但感谢您的提示。您确定您使用的库包括类
org.apache.jasper.servlet.JspServlet
jar tf lib/servlet api.jar
将有助于运行命令,因为在lib/servlet-api.jar或lib/jetty-all.jar中似乎找不到
JspServlet
类。然而,我在网上发现了一个包含该类的jar,在类路径中使用新jar重新编译,但错误仍然存在。您是否在运行时将其包含到类路径中?感谢@Joakim,环顾四周后,似乎需要许多JARPER jsp支持才能在jetty中工作。我希望只有一个jar文件可以下载,以保持我的项目轻量级和可移植性,但我找不到。最后决定jsp支持不值得额外的重视。@Rob144-heh,jsp绝不是轻量级的。特别是如果您使用Glassfish/Jasper工件,那么使用Apache/Jasper工件时,情况会稍微好一些。@Rob144另外,
jetty all.jar
的使用是轻量级的对立面,它实际上包含了jetty可用的所有东西和厨房水槽,其中大多数项目使用不到30%。事实上,按照某些代码的结构,不可能在单个项目中使用100%的jar(因为某些功能是可选的,一旦专门使用,就禁止使用其他功能)@Rob144
jetty all.jar
仅作为教学辅助工具存在(在文档中引用),它不是用于生产用途的。我应该把这个罐子放在哪里?@HelpingHands只要输入正确的路径作为java命令的类路径就行了。如何在java代码中设置JSP支持?有样品吗?JSP刚刚好用。如果将JSP文件放入webapps根目录中的文件夹中,则可以直接运行它。然而,一些框架(比如Spring)可能需要一些额外的设置才能进入某个页面。非常感谢。我不会怀疑像JSP这样的东西会是一个模块。(Jetty的新功能)--添加到startd已被弃用!改为使用:--addtostart=jsp