Reputation: 106
I am trying to move from using Jetty with a web.xml to using embedded Jetty. I have managed to move my REST endpoints using HttpServletDispatchers however I am struggling to get the JSP servlet moved
Currently, in web.xml I have
<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>
The Java code I have is something like
final JspServlet indexServlet = new JspServlet();
final ServletHolder indexServletHolder = new ServletHolder("jsp", indexServlet);
indexServletHolder.setInitParameter("fork", "false");
indexServletHolder.setInitParameter("xpoweredBy", "false");
indexServletHolder.setInitOrder(1);
context.addServlet(indexServletHolder, "/*");
But I cannot work out where I would add the bit that related to
<jsp-file>/index.jsp</jsp-file>
into Java
The index file is actually just a plain html page, so if there is an easier way to do this as not a JSP file, then that works too. I cannot put this file as a welcome file however, as I need all URL's to map to this one file (unless I can do that too and I am missing something)
Upvotes: 2
Views: 970
Reputation: 106
Based on the answer by @joakim-erdfelt, I ended up using a welcome-list for the default index.html file, and then creating a custom ErrorHandler that on 404 errors which forwards the requests to /, which then, in turn, returns the index file.
Any request to localhost:8080 would correctly return the index.html page listed in welcomeFiles, but localhost:8080/foo would just return a 404 error
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);
}
Upvotes: 0
Reputation: 49555
First, use JettyJspServlet
, not JspServlet
.
See https://github.com/jetty-project/embedded-jetty-jsp
Your usage of JettyJspServlet
will only be configured to recognize various jsp extensions (eg: a url-pattern of *.jsp
), not directly attached to the /index.jsp
(other servlet mappings will be your actual jsp files and references)
For a code equivalent of your web.xml segment of ...
<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>
it would be ...
ServletHolder homeHolder = new ServletHolder();
homeHolder.setName("home");
homeHolder.setForcedPath("/index.jsp"); // equiv to <jsp-file>
context.addServlet(homeHolder, "/*");
... but this url-pattern is nonsense for a JSP file that's "all html".
This pattern means 100% of requests will go to your /index.jsp
.
Please look at the example project, as setting up JSP in embedded-jetty is quite tricky! Lots of things have to be in place juuust the right way before JSP will work properly.
You might be wanting to use the welcome-file behaviors, which will resolve (pay attention to this word!) against the requested URL if that requested URL would return a 404, the list of welcome files are attempted one after each other (using technique similar to URI.resolve(String)
until one returns something other then a 404.
In the standard servlet descriptor WEB-INF/web.xml
that would be represented in the following snippet ...
<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"
metadata-complete="false"
version="3.1">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file> <!-- relative ref -->
<welcome-file>/index.jsp</welcome-file> <!-- absolute ref -->
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
or in code ...
context.setWelcomeFiles(new String[]{"index.jsp", "/index.jsp", "index.html"});
Upvotes: 1