我正在使用Jetty构建Java Web应用程序,并且尝试使用web.xml
映射servlet,但是,这是行不通的-所有映射的servlet URL都返回Jetty 404错误。
这是我的/src/main/webapp/WEB-INF/web.xml
文件:
<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<error-page>
<error-code> 404 </error-code>
<location> /error/404 </location>
</error-page>
<servlet>
<servlet-name> MainPage </servlet-name>
<servlet-class> xx.yyy.zzz.vvvvvvvv.HelloPageHandler </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name> MainPage </servlet-name>
<url-pattern> /* </url-pattern>
</servlet-mapping>
<filter>
<filter-name> HeaderFilter </filter-name>
<filter-class> xx.yyy.zzz.vvvvvvvv.HeaderFilter </filter-class>
</filter>
<filter-mapping>
<filter-name> HeaderFilter </filter-name>
<url-pattern> /* </url-pattern>
</filter-mapping>
</web-app>
(出于明显的原因,真实的包裹名称是隐藏的)
主启动器类代码:
public static void main(String[] args) {
Logger.getGlobal().log(Level.FINE, "Starting server");
try {
Server server = new Server(10800);
ServletHandler servletHandler = new ServletHandler();
server.start();
server.join();
} catch (Exception e) {
Logger.getGlobal().log(Level.SEVERE, "Exception while starting server", e);
}
}
但是,当尝试查看一些映射的URL时:
我确实尝试过:
public static void main(String[] args) {
Logger.getGlobal().log(Level.FINE, "Starting server");
try {
Server server = new Server(10800);
ServletHandler servletHandler = new ServletHandler();
server.setHandler(servletHandler);
ServletHolder holder = new ServletHolder(new HelloPageHandler());
servletHandler.addServletWithMapping(holder, "/abcd/*");
servletHandler.addFilterWithMapping(HeaderFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
server.start();
server.join();
} catch (Exception e) {
Logger.getGlobal().log(Level.SEVERE, "Exception while starting server", e);
}
}
但是,将每个servlet单独添加到main方法中是重复的,并且看起来很愚蠢且不正确。为什么会发生这种情况的任何想法?