Jsp 嵌入式Jetty 9.4在IDE内部工作,但不自动工作
我正在尝试使用嵌入式Jetty运行JSP和servlet。我有一个标准Java应用程序(不是web应用程序),它被打包为一个JAR(不是WAR),我有一个专用文件夹来存储所有与web相关的文件(“webui/”):web.xml、*.jsp、*.jspf等等。这个文件夹与JAR位于同一个文件夹中,可以从应用程序访问(已验证) 当我从NetBeans内部(版本8.1和Java8)运行我的应用程序时,它工作得非常好,但当我从shell窗口(Java-jarmyapp.jar)执行它时,它会提供以下错误Jsp 嵌入式Jetty 9.4在IDE内部工作,但不自动工作,jsp,jetty,Jsp,Jetty,我正在尝试使用嵌入式Jetty运行JSP和servlet。我有一个标准Java应用程序(不是web应用程序),它被打包为一个JAR(不是WAR),我有一个专用文件夹来存储所有与web相关的文件(“webui/”):web.xml、*.jsp、*.jspf等等。这个文件夹与JAR位于同一个文件夹中,可以从应用程序访问(已验证) 当我从NetBeans内部(版本8.1和Java8)运行我的应用程序时,它工作得非常好,但当我从shell窗口(Java-jarmyapp.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
在发布这篇文章之前,我已经阅读了(除其他外)以下帖子:
- 但我没有用玻璃鱼
- 其他帖子说把JAR放在WEB-INF/lib中。但我不能,因为我没有WEB-INF文件夹,因为我没有遵循WEB应用程序文件夹结构。我把它放在app./lib/文件夹中(和所有其他jar一起)
- 。我也使用了这篇文章所说的方法,也使用了这篇文章所指的方法(官方的Jetty示例)。在这两种情况下,我得到的错误与我的代码相同
<!--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>
非常感谢您的帮助。什么版本的Jetty?抱歉Jetty版本是9.4.3我已经找到了解决问题的方法:您可以使用WebAppContext::setResourceBase(…)将基本资源文件夹命名并放置在该基本文件夹中,只要您使用WebAppContext::setResourceBase(…),您就可以将“web.xml”命名并放置在该基本文件夹中只要使用WebAppContext::setDescriptor(…)进行设置,但在基本文件夹中,必须有一个“/WEB-INF/lib/”文件夹,并且在该文件夹中必须放置“jstl-1.2.jar”文件。Jetty的哪个版本?抱歉,Jetty的版本是9.4.3。我已经找到了解决问题的方法:您可以使用WebAppContext::setResourceBase(…)来命名和放置基本资源文件夹,只要您正确设置了它,您就可以在这个基本文件夹中命名和放置“web.xml”只要使用WebAppContext::setDescriptor(…)进行设置,但在基本文件夹中,必须有一个“/WEB-INF/lib/”文件夹,并且在此文件夹中必须放置“jstl-1.2.jar”文件。
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" );
}