Embedding JSP webapp inside Vaadin app

Hi,
try to move the viewer folder inside src/main/webapp.
This way the jsp should be available at url http://localhost:8080/viewer

HTH
Marco

You did it, Marco! I put it there and voilà! The page was served. In fact, I should have deduced it by myself; shame on me. I had the folder dates, name and structure as yyou can see in my previous attachment…

Anyway, now I’m facing problems that I’m almost sure are fault of the jsp app in 99% cases. It would be great if I could solve them and it would be superb if I could get rid of the /app part for calling the Vadding app.

Thanks a lot for helping, Marco :wink:

Nice work, guys :slight_smile:

It’s not a common use case, but maybe there should be a demo for this.

-Olli

Well, it seems this is a recurrent question. I found at least a similar one in Stack Overflow and, unfortunately too late, an entire old thread about this (so old that even Vaadin wasn’t called “Vaadin” then) where they already solved this problem:

https://vaadin.com/forum#!/thread/19771

Hi,
this is a hacky solution to run jsps togehter with a Vaadin app mapped to “/".
Basically we use a filter (that intercepts all requests) to find a an existing servlet mapped to "
.jsp” (there should be at least one, but it is not mandatory by specification);
if there is one we forward all jsp requests to it, otherwise we go with the filter chain.

Maybe it is not the best solution (tested only on jetty and must be improved), but it could be a starting point to resolve your problem.

HTH
Marco

@WebFilter(urlPatterns = "/*", asyncSupported = true,
                    dispatcherTypes = {DispatcherType.REQUEST, DispatcherType.FORWARD})
public static class JspFilter implements Filter {

        private String jspServletName;

        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            jspServletName = filterConfig.getServletContext()
                .getServletRegistrations().entrySet().stream()
                .filter(e -> e.getValue().getMappings().contains("*.jsp") )
                .map(Map.Entry::getKey)
                .findFirst().orElse(null);
        }

        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            HttpServletRequest req = (HttpServletRequest) request;
            String path = req.getRequestURI();
            if (jspServletName != null && path.matches("(?i).*\\.jsp$")) {
                req.getServletContext().getNamedDispatcher(jspServletName).forward(request, response);
            } else {
                chain.doFilter(request, response);
            }
        }

        @Override
        public void destroy() {

        }
}

@WebServlet(urlPatterns = {"/*"}, name = "MyUIServlet", asyncSupported = true)
@VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
public static class MyUIServlet extends VaadinServlet {
    ...
}

Thanks a lot for sharing, Marco. At this moment I’ll keep using the prior approach, as I can live for now with accessing the app with his own subpath. Also I still have to solve/configure some things in the “embedded application” in order to get it properly running, as it by default needs some paths to be in the root path and now it resides in its own directory, so it’s not time yet to mess with those hacky methods, hehe. But I’ll note in case I need it for the future.

Keep up the good work!