0

I'm using gretty and running app with tomcat.

My only servlet is:

@WebServlet(name = "frontServlet", urlPatterns = arrayOf("/"))
class FrontServlet : HttpServlet() {
    override fun doGet(req: HttpServletRequest, resp: HttpServletResponse) {
        Router.doGet(req, resp)
    }
    ...
}

My static assets are under WebContent/public

BUT any request including with paths to assets is handled by FrontServlet.

Default static serving works if I set FronServlet's url pattern to specific one (but I need it to catch all requests except for requests to static files).

What should I do and is there any way to invoke server's default static file handlers from my custom servlets?

ClassyPimp
  • 715
  • 7
  • 20
  • 1
    maybe this can help you https://stackoverflow.com/questions/3125296/can-i-exclude-some-concrete-urls-from-url-pattern-inside-filter-mapping – Youcef LAIDANI Oct 31 '17 at 10:29

1 Answers1

0

Well, after struggling with such ancient technology and using some other answers, I've came to this two solutions: 1. In FrontServlet doGet method:

if (req.requestURI.startsWith("/static/") || req.requestURI.startsWith("/favicon.ico")) {
    req.session.servletContext.getNamedDispatcher("default").forward(req, resp)
} else {
    Router.doGet(req, resp)
}
  1. Write a filter:

@WebFilter(filterName = "frontFiletr", urlPatterns = arrayOf("/*"))

class FrontFilter: Filter {

    override fun doFilter(request: ServletRequest, response: ServletResponse, chain: FilterChain) {
        val path = (request as HttpServletRequest).requestURI
        if (path.startsWith("/static/")) {
            chain.doFilter(request, response)
        } else {
            request.session.servletContext.getNamedDispatcher("frontServlet").forward(request, response)
        }
    }

}

ClassyPimp
  • 715
  • 7
  • 20