我正在学习Java EE 7 Servlet,并尝试使用嵌入式Jetty(v 9.3.7)从Java EE 7教程中部署hello2
示例,但收效甚微。hello2
由两个servlet和一个图像文件组成。该配置带有注释,并且该项目没有任何web.xml。
遵循WebAppContext
嵌入式Jetty示例的一部分,我创建了这个主类来初始化我的嵌入式服务器:
public class MyServer {
public static void main(String[] args) throws Exception {
Server server = new Server(8080);
String webappPath = new File(MyServer.class.getProtectionDomain().getCodeSource().getLocation().getFile())
.getParentFile().getParentFile().getAbsolutePath();
WebAppContext webapp = new WebAppContext(webappPath, "");
webapp.setConfigurations(new Configuration[]{
new AnnotationConfiguration()});
server.setHandler(webapp);
server.start();
server.join();
}
}
据我了解,由于Jetty是Java EE Web容器,它应该能够按原样提供Serlvet项目示例,而我只需要指向war文件夹结构即可。以下是项目的结构:
-- hello2
\-- src
\-- main
+-- java
│ +-- MyServer.java
│ \-- javaeetutorial
│ \-- hello2
│ +-- GreetingServlet.java
│ \-- ResponseServlet.java
\-- webapp
+-- WEB-INF
│ \-- classes
│ +-- MyServer.class
│ \-- javaeetutorial
│ \-- hello2
│ +-- GreetingServlet.class
│ \-- ResponseServlet.class
+-- index.html
\-- resources
\-- images
\-- duke.waving.gif
该hello2
示例代码可以发现在这里。这是GreetingServlet
@WebServlet("/greeting")
public class GreetingServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
....
和 ResponseServlet
@WebServlet("/response")
public class ResponseServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
....
编译文件以hello2/webapp/classes/
使该webapp
文件夹成为分解后的WAR。我添加index.html只是为了测试Jetty是否接好它。结果是当我访问localhost:8080,localhost:8080 / greeting或localhost:8080 / response时收到错误404
如果添加WebXmlConfiguration
,webapp.setConfigurations()
然后将资源库设置为,那么webapp.setResourceBase(webappPath)
我将设法进入Jetty的静态文件服务器。这是因为Jetty然后使用默认值web.xml
,该默认值将其自己的servlet用于文件服务目的添加到服务器。但是即使那样,我的带注释的servlet也不会被使用。
我让Jetty读取带注释的servlet配置的方法是通过WEB-INF
使用WebAppContext.getMetadata().setWebInfClassesDirs()
以下命令显式设置目录:
webapp.getMetaData().setWebInfClassesDirs(
Arrays.asList(Resource.newResource(
MyServer.class.getProtectionDomain().getCodeSource().getLocation())));
然后,Servlet将按预期方式响应,但这不会为我index.html
的文件或映像文件提供服务。我还将资源库设置为无用。因此,我想要的是Jetty在不使用的情况下为我的Web应用程序提供服务web.xml
,只需将其指向展开的WAR目录即可。显然我缺少了一些东西。
使用 ...
webapp.setConfigurations(new Configuration[]{
new AnnotationConfiguration()});
将撤消所有现有的重要配置,并且仅AnnotationConfiguration
启用该配置。
难怪它对您不起作用,使用该设置,WEB-INF/web.xml
缺少加载的配置,WEB-INF/lib
缺少使用的配置,等等。
您必须适当地修改现有的配置列表,并且有很多示例向您展示。
由于您没有指定是否有使用JNDI的批注,是否存在于Web片段中,或者来自Webapp上下文之外(例如容器本身),因此很难指定所需的确切配置。
有关执行此操作的完整项目,请参见https://github.com/jetty-project/embedded-servlet-3.1项目。
context.setConfigurations(new Configuration[]
{
new AnnotationConfiguration(),
new WebInfConfiguration(),
new WebXmlConfiguration(),
new MetaInfConfiguration(),
new FragmentConfiguration(),
new EnvConfiguration(),
new PlusConfiguration(),
new JettyWebXmlConfiguration()
});
这是最常见的常规设置,但可能会使您面临不必要的额外配置。
即使您不想要其他组件,也仍然需要将它们留给发现的Web应用程序使用。否则,你有100%的Web应用手册,你专门致电.addServlet()
和.addFilter()
对等。
您可能应该改用此语法。
private void enableAnnotationScanning(Server server)
{
Configuration.ClassList classlist = Configuration.ClassList.setServerDefault(server);
classlist.addBefore("org.eclipse.jetty.webapp.JettyWebXmlConfiguration",
"org.eclipse.jetty.annotations.AnnotationConfiguration");
}
因为这将修改现有的配置列表,仅添加 AnnotationConfiguration
如果要查看此格式的其他示例,请查看以下示例项目:
AnnotationConfiguration
根据需要使用。ServletContextHandler
如上所述的和手动设置。本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句