我正在尝试使用嵌入式Jetty运行JSP和Servlet。我有一个标准的Java应用程序(不是Web应用程序),它打包为JAR(而不是WAR),我有一个专用文件夹来存储所有与Web相关的文件(“webui /”):web.xml,* .jsp ,* .jspf等。此文件夹与JAR位于同一文件夹中,可从应用程序访问(已证实)。
当我从NetBeans(版本8.1和Java 8)中运行它时,我的应用程序运行正常,但是当我从shell窗口(java -jar myapp.jar)执行它时,它提供了以下错误
org.apache.jasper.JasperException: /menu.jsp (line: 6, column: 0) The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application
在发布此内容之前,我已经阅读了以下帖子(以及其他许多内容):
Jetty 9 The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved 但我不是在使用Glassfish
其他帖子说将JAR放在WEB-INF / lib中。但我不能因为我没有WEB-INF文件夹,因为我没有遵循WEB app文件夹结构。我把它放在app ./lib/文件夹里(和所有其他的罐子一样)。
Embedded Jetty Error The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved。我也使用了这篇文章所说的方法以及这篇文章所指的方法(官方Jetty示例)。在这两种情况下,我都会获得与我的代码相同的错误。
这是我的POM
<!--Jetty dependencies start here -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jettyVersion}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>${jettyVersion}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>${jettyVersion}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-annotations</artifactId>
<version>${jettyVersion}</version>
</dependency>
<!--Jetty Apache JSP -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>apache-jsp</artifactId>
<version>${jettyVersion}</version>
</dependency>
<!-- Jetty JSTL -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
这是我的代码
private static Server serverWeb = null;
//----------------------------------------------------------------------------//
public static void Main( String[] args )
{
JettyExampleStack instance = new JettyExampleStack();
instance.initServerWeb();
}
//----------------------------------------------------------------------------//
private void initServerWeb()
{
WebAppContext ctx = new WebAppContext();
ctx.setContextPath( "/" );
serverWeb = new Server( 8080 );
serverWeb.setHandler( ctx );
serverWeb.setStopAtShutdown( true );
serverWeb.setStopTimeout( UtilConvert.SECOND * 5 );
try
{
initServlets( ctx );
initJSPs( serverWeb, ctx );
serverWeb.start();
}
catch( Exception exc )
{
UtilDebug.log( exc );
stopdown();
}
}
private void initServlets( WebAppContext ctx ) throws Exception
{
ctx.addServlet( GetFile.class, "/getFile" );
}
private void initJSPs( Server server, WebAppContext ctx ) throws Exception
{
ctx.setResourceBase( "webui" );
ctx.setDescriptor( "web.xml" );
// ----------------------------------------------------------------
// TAKEN FROM HERE:
// https://github.com/jetty-project/embedded-jetty-jsp
// ----------------------------------------------------------------
// By default, the JSP implementation will use an internal eclipse JDT compiler.
// This will use the JDK JavaC built-in compiler.
System.setProperty( "org.apache.jasper.compiler.disablejsr199", "false" );
// Set a Servlet Temp Directory
File fTmp = new File( System.getProperty( "java.io.tmpdir" ), "jetty-jsp-tmp" );
if( ! UtilIO.createFolder( fTmp ) )
{
throw new IOException( "Can not create folder '"+ fTmp.getAbsolutePath() +"'" );
}
ctx.setAttribute("javax.servlet.context.tempdir", fTmp );
// Set Classloader of Context to be sane (needed for JSTL)
// JSP requires a non-System classloader, this simply wraps the
// embedded System classloader in a way that makes it suitable
// for JSP to use
ClassLoader jspClassLoader = new URLClassLoader( new URL[0], WebServer.class.getClassLoader() );
ctx.setClassLoader( jspClassLoader );
// The JspServlet must be named "jsp" (per JSP spec).
// Add JSP Servlet (must be named "jsp")
ServletHolder holderJsp = new ServletHolder( "jsp", JspServlet.class );
holderJsp.setInitOrder( 0 );
// Including the JSTL jars for the webapp.*/
ctx.setAttribute( "org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", ".*/[^/]*jstl.*\\.jar$" );
// Enabling the Annotation based configuration
Configuration.ClassList classlist = Configuration.ClassList.setServerDefault( server );
classlist.addBefore( "org.eclipse.jetty.webapp.JettyWebXmlConfiguration",
"org.eclipse.jetty.annotations.AnnotationConfiguration" );
classlist.addAfter( "org.eclipse.jetty.webapp.FragmentConfiguration",
"org.eclipse.jetty.plus.webapp.EnvConfiguration",
"org.eclipse.jetty.plus.webapp.PlusConfiguration" );
}
非常感谢您的帮助。