【发布时间】:2019-05-31 21:23:08
【问题描述】:
我必须使用我的 html 文件中的自定义页面更改 404 错误页面。 我编写了这段代码,但它不起作用,和/或我不知道如何使用它。
public static void main(String[] args) throws Exception {
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/rac");
Server jettyServer = new Server(8080);
context.setWelcomeFiles(new String[] { "./src/main/resources/index.html" });
ErrorPageErrorHandler errorHandler = new ErrorPageErrorHandler();
errorHandler.addErrorPage(HttpStatus.NOT_FOUND_404, "./src/main/resources/error.html");
context.setErrorHandler(errorHandler);
// HTML
DefaultServlet defaultServlet = new DefaultServlet();
ServletHolder holderPwd = new ServletHolder("default", defaultServlet);
holderPwd.setInitParameter("resourceBase", "./src/main/resources/index.html");
context.addServlet(holderPwd, "/*");
// SERVICES
ServletHolder jerseyServlet = context.addServlet(org.glassfish.jersey.servlet.ServletContainer.class,
"/ajax/*");
jerseyServlet.setInitOrder(0);
jerseyServlet.setInitParameter("jersey.config.server.provider.classnames",
DiskServiceWS.class.getCanonicalName() + "," + RamServiceWS.class.getCanonicalName() + ","
+ CpuServiceWS.class.getCanonicalName());
jettyServer.setHandler(context);
try {
jettyServer.start();
jettyServer.join();
} finally {
jettyServer.destroy();
}
}
【问题讨论】:
-
哪个404错误?从码头?来自您的 ServletContextHandler?还是从你的 Jersey 层?
标签: java html servlets server jetty