Note that there are some explanatory texts on larger screens.

plurals
  1. POServing static resources, via default servlet, in Jetty
    primarykey
    data
    text
    <p>My Servlet (running on Jetty) should also deploy static content that is in the static directory.</p> <p>Directory structure:</p> <pre><code>/ - static/ - [static files] - WEB-INF/ - [my servlet .class files and the web.xml] </code></pre> <p>My web.xml</p> <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt; &lt;web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"&gt; &lt;filter&gt; &lt;filter-name&gt;filter&lt;/filter-name&gt; &lt;filter-class&gt;com.example.StaticFilter&lt;/filter-class&gt; &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;filter&lt;/filter-name&gt; &lt;url-pattern&gt;/*&lt;/url-pattern&gt; &lt;/filter-mapping&gt; &lt;/web-app&gt; </code></pre> <p>My main Servlet file:</p> <pre><code>@WebServlet("/*") public class SampleClass extends HttpServlet { //Code } </code></pre> <p>And my filter:</p> <pre><code>import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; public class StaticFilter implements Filter { @Override public void init(FilterConfig fc) throws ServletException { } @Override public void destroy() { } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; String path = req.getRequestURI().substring(req.getContextPath().length()); if (path.startsWith("/static")) { request.getRequestDispatcher(path).forward(request, response); } else { chain.doFilter(request, response); } } } </code></pre> <p>Now, if I call /static/style.css, I want to get the file from /static and not be redirected to the servlet.</p> <p>Server: Jetty 9.1.0 RC1</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload