我使用嵌入式Jetty来启动webapp,我使用JSP页面作为视图. 使用maven-jetty-plugin可以很好地进行测试,这让我觉得我在引导服务器时缺少一些依赖项(这是问题的根本原因). 我用命令引导Jetty:
使用maven-jetty-plugin可以很好地进行测试,这让我觉得我在引导服务器时缺少一些依赖项(这是问题的根本原因).
我用命令引导Jetty:
java $JAVA_OPTS -cp target/classes:target/dependency/* xxx.Main
我已经在Maven中使用了依赖插件,以确保获得类路径上的所有deps(在打包的war文件之外):
<execution> <id>copy-dep</id> <goals> <goal>copy-dependencies</goal> </goals> </execution>
Main类看起来像这样:
Server server = new Server(8080); WebAppContext root = new WebAppContext(); root.setContextPath("/"); root.setWar("target/app.war"); server.setHandler(root); server.start(); server.join();
嵌入式Jetty服务器启动时没有错误,但是当导航到返回JSP页面的URL时,我总是会收到此错误.
堆栈跟踪:
PWC6181: File /javax/servlet/jsp/resources/web-jsptaglibrary_1_2.dtd not found Caused by: org.xml.sax.SAXException: PWC6181: File /javax/servlet/jsp/resources/web-jsptaglibrary_1_2.dtd not found at org.apache.jasper.xmlparser.MyEntityResolver.resolveEntity(ParserUtils.java:566) at com.sun.org.apache.xerces.internal.util.EntityResolverWrapper.resolveEntity(EntityResolverWrapper.java:107) at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.resolveEntityAsPerStax(XMLEntityManager.java:1018) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$DTDDriver.dispatch(XMLDocumentScannerImpl.java:1192) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$DTDDriver.next(XMLDocumentScannerImpl.java:1090) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:1003) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648) at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737) at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119) at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:235) at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:284) at org.apache.jasper.xmlparser.ParserUtils.parseXMLDocument(ParserUtils.java:296) at org.apache.jasper.xmlparser.ParserUtils.parseXMLDocument(ParserUtils.java:366) at org.apache.jasper.runtime.TldScanner.scanTld(TldScanner.java:526) at org.apache.jasper.runtime.TldScanner.scanJar(TldScanner.java:379) at org.apache.jasper.runtime.TldScanner.scanJars(TldScanner.java:607) at org.apache.jasper.runtime.TldScanner.scanTlds(TldScanner.java:282) at org.apache.jasper.runtime.TldScanner.getLocation(TldScanner.java:255)
我使用Jetty 8,我的web.xml配置为Servlet 3.0.
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">我发现缺少dtd文件的问题是我需要Jetty JSP JAR.
当它不起作用时,我正在使用这些依赖项来支持JSP:
<dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>org.glassfish.web</groupId> <artifactId>jsp-impl</artifactId> <version>2.2</version> </dependency>
Jetty-runner总是为我工作所以我查看了jar文件中的依赖项.
<dependency> <groupId>org.eclipse.jetty.orbit</groupId> <artifactId>javax.servlet.jsp</artifactId> <version>2.2.0.v201112011158</version> </dependency> <dependency> <groupId>org.eclipse.jetty.orbit</groupId> <artifactId>org.apache.jasper.glassfish</artifactId> <version>2.2.2.v201112011158</version> </dependency>
在javax.servlet.jsp依赖项中存在缺少的dtd文件,因此当我开始使用它们时问题就消失了.
所以我想问题是我需要Jetty特定的JSP依赖,而不是一般的.谁能解释为什么Jetty以这种方式实现?