Loading...
Important Notice - Forums is archived

To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Product icon
TUTORIAL

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.

Hosting files when Vaadin application is running in root context

bob block
5 years ago Dec 23, 2016 7:57pm
Marco Collovati
5 years ago Dec 23, 2016 8:42pm
bob block
5 years ago Dec 23, 2016 10:25pm
Marco Collovati
5 years ago Dec 23, 2016 10:58pm

Hi,
RequestHandler works fine, but you must add it directly in VaadinService not on VaadinSession

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

        @Override
        protected VaadinServletService createServletService(DeploymentConfiguration deploymentConfiguration) throws ServiceException {
            VaadinServletService service = new VaadinServletService(this, deploymentConfiguration) {
                @Override
                protected List<RequestHandler> createRequestHandlers() throws ServiceException {
                    List<RequestHandler> h = super.createRequestHandlers();
                    h.add((session, request, response) -> {
                        if ("/rhexample".equals(request.getPathInfo())) {
                            // Generate a plain text document
                            response.setContentType("text/plain");
                            response.getWriter().append(
                                "Here's some dynamically generated content from request handler.\n");
                            response.getWriter().format(Locale.ENGLISH,
                                "Time: %Tc\n", new Date());
                            return true; // We wrote a response
                        }
                        return false; //
                    });
                    return h;
                }
            };
            service.init();
            return service;
        }
    }

For the filter  map it only on the needed path; for example you can serve http://localhost/other with something like this

@WebFilter(urlPatterns = "/other")
    public static class MyFilter implements Filter {

        // other methods omitted

        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            response.setContentType("text/plain");
            response.getWriter().append(
                "Here's some dynamically generated content from filter.\n");
            response.getWriter().format(Locale.ENGLISH, "Time: %Tc\n", new Date());
        }

    }

HTH
Marco

bob block
5 years ago Dec 24, 2016 11:19am