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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26217143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 02:01:49  来源:igfitidea点击:

Embedded Jetty Server - NO JSP Support for /, did not find org.apache.jasper.servlet.JspServlet

javajspjettyembedded-jetty

提问by

I've have the following code to use an embedded Jetty server alongside a simple servlet and .jsp webpage. However, after compiling and running the code:

我有以下代码来使用嵌入式 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

I get an error:

我收到一个错误:

INFO:oejw.StandardDescriptorProcessor:main: NO JSP Support for /, did not find org.apache.jasper.servlet.JspServlet

And navigating to /index.jsp gives a 500 error.

导航到 /index.jsp 会出现 500 错误。

HTTP ERROR 500
Problem accessing /index.jsp. 
Reason:
JSP support not configured

I've read this postbut I don't think the solution applies here because I'm running Jetty embedded rather than using the start.jar.

我已经阅读了这篇文章,但我认为该解决方案不适用于这里,因为我运行的是嵌入式 Jetty,而不是使用 start.jar。

How can I resolve this error so that the server will run and serve .jsp pages successfully?

如何解决此错误,以便服务器能够成功运行并提供 .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();
    }
}

MyServlet.java

我的Servlet.java

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

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

This is my project structure:

这是我的项目结构:

webapp
----com
    ----test
        ----MyServlet.java
        ----ServerMain.java
        ----index.jsp
        ----web.xml
----lib
    ----jetty-all.jar
    ----servlet-api.jar  

采纳答案by msrd0

It seems like you're missing an JAR-file that includes the class org.apache.jasper.servlet.JspServlet. Download a JAR file containing it (look here) and add it to your classpath. Also, on a side note, you should replace com/test/ServerMainwith the real class name, com.test.ServerMain. You java statement should look like this:

您似乎缺少一个包含 class 的 JAR 文件org.apache.jasper.servlet.JspServlet。下载包含它的 JAR 文件(查看此处)并将其添加到您的类路径中。另外,附带说明一下,您应该替换com/test/ServerMain为真实的类名com.test.ServerMain. 您的 java 语句应如下所示:

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

回答by Joakim Erdfelt

Incidentally, there's a github project, maintained by the Jetty Project, demonstrating JSP support in Jetty Embedded.

顺便说一下,有一个由 Jetty 项目维护的 github 项目,它展示了 Jetty Embedded 中的 JSP 支持。

https://github.com/jetty-project/embedded-jetty-jsp

https://github.com/jetty-project/embedded-jetty-jsp

回答by Nux

Haven't tried for embedded Jetty but when running Jetty 9.3 as a service you need to add JSP support.

尚未尝试嵌入 Jetty,但在将 Jetty 9.3 作为服务运行时,您需要添加 JSP 支持。

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

Where JETTY_BASEis your folder where you deploy application which is separate from JETTY_HOME. So I'm guessing embedded Jetty would need similar configuration.

哪里JETTY_BASE是你的文件夹中部署应用程序,它是从不同的JETTY_HOME。所以我猜嵌入式 Jetty 需要类似的配置。