Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用不带web.xml的JSPServlet jsp文件_Java_Jetty_Embedded Jetty_Servlet 3.0 - Fatal编程技术网

Java 使用不带web.xml的JSPServlet jsp文件

Java 使用不带web.xml的JSPServlet jsp文件,java,jetty,embedded-jetty,servlet-3.0,Java,Jetty,Embedded Jetty,Servlet 3.0,我正试图从使用带有web.xml的Jetty转变为使用嵌入式Jetty。我已经设法使用HttpServletDispatchers移动了我的REST端点,但是我很难移动JSP servlet 目前,在web.xml中,我有 <servlet-mapping> <servlet-name>home</servlet-name> <url-pattern>/*</url-pattern> </servlet-mappi

我正试图从使用带有web.xml的Jetty转变为使用嵌入式Jetty。我已经设法使用HttpServletDispatchers移动了我的REST端点,但是我很难移动JSP servlet

目前,在web.xml中,我有

<servlet-mapping>
    <servlet-name>home</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>
<servlet>
    <servlet-name>home</servlet-name>
    <jsp-file>/index.jsp</jsp-file>
</servlet>
但我无法确定在哪里添加与

<jsp-file>/index.jsp</jsp-file>
/index.jsp
进入爪哇

索引文件实际上只是一个普通的html页面,因此如果有一种更简单的方法来实现这一点,而不是JSP文件,那么它也可以。但是,我不能将此文件作为欢迎文件,因为我需要所有URL映射到此文件(除非我也可以这样做,而且我遗漏了一些内容)

首先,使用,而不是
JspServlet

您使用的
JettyJspServlet
将仅配置为识别各种jsp扩展(例如:url模式
*.jsp
),而不是直接附加到
/index.jsp
(其他servlet映射将是您实际的jsp文件和引用)

对于与您的web.xml段等效的代码


家
/*
家
/index.jsp
那将是

ServletHolder homeHolder=新的ServletHolder();
房屋持有人。房屋名称(“房屋”);
homeHolder.setForcedPath(“/index.jsp”);//相当于
addServlet(homeHolder,“/*”);
。。。但是这种url模式对于一个“全是html”的JSP文件来说毫无意义

此模式意味着100%的请求将转到您的
/index.jsp

请看这个示例项目,因为在嵌入式jetty中设置JSP非常棘手!在JSP正常工作之前,许多事情必须以正确的方式准备就绪

您可能希望使用欢迎文件行为,该行为将针对请求的URL解析(注意这个词!)如果请求的URL将返回404,则欢迎文件列表将一个接一个地尝试(使用类似于
URI.resolve(String)
的技术,直到返回其他文件,然后返回404

在标准servlet描述符
WEB-INF/WEB.xml
中,将在以下代码段中表示


index.jsp
/index.jsp
index.html
或者在代码中

context.setWelcomeFiles(新字符串[]{“index.jsp”、“/index.jsp”、“index.html”});

根据@joakim erdfelt的回答,我最终为默认index.html文件使用了一个欢迎列表,然后创建了一个自定义的ErrorHandler,它根据404错误将请求转发给/,然后返回索引文件

对localhost:8080的任何请求都会正确返回welcomeFiles中列出的index.html页面,但localhost:8080/foo只会返回404错误

RequestDispatcher dispatcher = request.getRequestDispatcher(redirectRoute);
if (dispatcher != null && response.getStatus() == HttpServletResponse.SC_NOT_FOUND) {
    try {
        response.reset();
        dispatcher.forward(request, response);
    } catch (ServletException e) {
        super.handle(target, baseRequest, request, response);
    }
} else if (response.getStatus() == HttpServletResponse.SC_NOT_FOUND) {
    logger.info("Can not redirect to /, no dispatcher found. Will show system 404 page");
} else {
    super.handle(target, baseRequest, request, response);
}

但是这种url模式对于一个“全是html”的JSP文件来说毫无意义“-我如何才能将所有未映射到找到的资源的请求都映射回index.html?因此favicon等应该返回正确的内容,但/foo应该仍然显示索引页。这是我使其在web.xml中工作的唯一方法,所以这就是它的工作方式。谢谢!这似乎是wh的正确方向。”但是,在我需要的时候,如果没有名为“foo”的文件,这似乎无法将“/foo”解析为“index.html”,但您是说应该这样做?因此,如果您使用
“index.jsp”,可能我还有一个配置问题
然后对
localhost:8080/foo
的请求将导致对
localhost:8080/foo/index.jsp
的内部请求。记住,想想解析是如何工作的。也许你是想使用
“/index.jsp”
?我的欢迎文件列表包括“index.html”和“/index.html”以及“/foo”仍然没有查看DefaultServlet的源代码,如果它在检查欢迎列表之前检查文件是否存在,那么我看不出它应该如何工作。在doGet函数中,它首先检查“//Not found?”,然后继续检查“sendWelcome”(如果找到)
RequestDispatcher dispatcher = request.getRequestDispatcher(redirectRoute);
if (dispatcher != null && response.getStatus() == HttpServletResponse.SC_NOT_FOUND) {
    try {
        response.reset();
        dispatcher.forward(request, response);
    } catch (ServletException e) {
        super.handle(target, baseRequest, request, response);
    }
} else if (response.getStatus() == HttpServletResponse.SC_NOT_FOUND) {
    logger.info("Can not redirect to /, no dispatcher found. Will show system 404 page");
} else {
    super.handle(target, baseRequest, request, response);
}