gpt4 book ai didi

java - Jetty 7.6不编译JSP文件

转载 作者:行者123 更新时间:2023-12-02 10:20:52 27 4
gpt4 key购买 nike

我是 Jetty 和 JSP 的新手。我现在正在尝试使用嵌入式 Jetty 和 JSP 创建简单的服务器来生成 html。

我首先要提到的是,我受到 Jetty 版本的限制。我必须使用的版本是 Jetty 7.6.x.x。

我的需要是创建几个 servlet,我可以在其中将请求/响应分派(dispatch)到 JSP 文件。问题是 JSP 文件似乎没有被编译,它不是计算表达式,而是将整个脚本作为纯文本扔到浏览器中。让我们来看看。

public void start() throws Exception {
server = new Server();
SelectChannelConnector connector = new SelectChannelConnector();
connector.setPort(port);
server.addConnector(connector);

// Base URI to webapp, where jsp files are located
URI baseUri = getWebRootResourceUri();

// Create Servlet context
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
context.setResourceBase(baseUri.toASCIIString());

// Default Servlet (always last, always named "default")
ServletHolder holderDefault = new ServletHolder("default", DefaultServlet.class);
holderDefault.setInitParameter("resourceBase", baseUri.toASCIIString());
holderDefault.setInitParameter("dirAllowed", "true");
context.addServlet(holderDefault, "/");
server.setHandler(context);

server.start();

}

这是JSP文件

    <!DOCTYPE html>
<html>
<head>
<title>Coin Flipper</title>
</head>
<body>
<h1>Coin Flipper</h1>
<p>Flipping a coin...</p>
<% if(Math.random() < .5){ %>
<p>Heads!</p>
<% }
else{ %>
<p>Tails!</p>
<% } %>
<hr />
<p>Refresh to flip again.</p>
</body>
</html>

这就是结果:

enter image description here

我没有使用 web.xml,但如果它能解决我的问题,我不介意使用它。

这也是我的 Maven 依赖项:

<dependencies>
<!-- Embedded web server -->
<!-- https://mvnrepository.com/artifact/org.eclipse.jetty/jetty-server -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>7.6.21.v20160908</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.eclipse.jetty/jetty-servlet -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>7.6.21.v20160908</version>
</dependency>


<!-- https://mvnrepository.com/artifact/org.glassfish.web/jsp-impl -->
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jsp-impl</artifactId>
<version>2.1.3-b10</version>
</dependency>

</dependencies>

最佳答案

来自https://wiki.eclipse.org/Jetty/Howto/Configure_JSP

In versions of Jetty prior to 7.5.0, the JSP infrastructure made use of the Eclipse Java Compiler (ecj.jar) which is supplied in $JETTY_HOME/lib/jsp. For jetty-7.5.0 we upgraded the version of JSP to jsp-impl-2.1.3.b10 (from Glassfish). In this version, the JSP infrastructure ALWAYS tries to acquire a Java compiler from the JVM if the version of Java is 1.6 or above. Therefore, if you are using a JRE, JSPs are unable to compile so you must use a full JDK. Alternatively, you can precompile your JSPs (which is preferable in a production deployment in any case). The Jetty JSPC Maven Plugin is helpful for precompilation.

这听起来和你的问题完全一样。按照上面的链接的说明,使用 JDK 或预编译 JSP。

关于java - Jetty 7.6不编译JSP文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54351118/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com