Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-users] Set header values for all responses


You can just have your own HandlerWrapper that you use to wrap any other handler that does the addHeader.


cheers



On 28 March 2017 at 21:47, Olaf van der Meer <o.vandermeer@xxxxxx> wrote:
Hi,


I would like to set some header values for all responses.
I already have found how to do that for the ResourceHandler:

    ResourceHandler resource_handler = new ResourceHandler()
    {
      @Override
      public void handle( String target, Request baseRequest,
HttpServletRequest request, HttpServletResponse response ) throws
IOException, ServletException
      {
        response.setHeader( "Pragma", "no-cache" );
        super.handle( target, baseRequest, request, response );
      }
    };



But I use a ServletContextHandler also. While searching I found that I could
use a filter to set the header values like this:

    ServletContextHandler context = new
ServletContextHandler(ServletContextHandler.SESSIONS);
    server.setHandler(context);

    EnumSet<DispatcherType> dispatches =
EnumSet.allOf(DispatcherType.class);
    FilterHolder holder = new FilterHolder(DemoFilter.class);
    holder.setName("filter");
    context.addFilter(holder,"/*",dispatches);


    public static class DemoFilter implements Filter
    {
        @Override
        public void doFilter(ServletRequest request, ServletResponse
response, FilterChain chain) throws IOException, ServletException
        {
            if (response instanceof HttpServletResponse)
            {
                ((HttpServletResponse)response).addHeader("Pragma ","
no-cache ");
            }
            chain.doFilter(request,response);
        }
    }

I don't like to set the header values on different ways. Is there a common
way to set the header values for all the Handlers?

Thanks.

Olaf.


_______________________________________________
jetty-users mailing list
jetty-users@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/jetty-users



--

Back to the top