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

Hi,

 

I have tried to use a HandlerWrapper. This will give the output I want. But there is one thing I don’t understand.

In the next code I have got a resourceHandler for static pages/images/css and a contextHandler for dynamic pages.

I have created a wrapperResourceHandler and called setHandler for the resource_handler.

The output shows that the wrapperResourceHandler also has effect on the contextHandler. Can anyone explain this?

 

 

ServletContextHandler contextHandler = new ServletContextHandler( ServletContextHandler.SESSIONS );

contextHandler.addServlet( new ServletHolder( new HelloServlet() ), "/hello/*" );

 

ResourceHandler resource_handler = new ResourceHandler();

 

HandlerWrapper wrapperResourceHandler = new HandlerWrapper()

{

  @Override

  public void handle( String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response ) throws IOException, ServletException

  {

    response.setHeader( "Pragma", "no-cache" );

  }

};

wrapperResourceHandler.setHandler( resource_handler );

 

HandlerList handlers = new HandlerList();

handlers.setHandlers( new Handler[] { wrapperResourceHandler, contextHandler } );

_JettyServer.setHandler( handlers );

        

 

Thanks.

 


From: jetty-users-bounces@xxxxxxxxxxx [mailto:jetty-users-bounces@xxxxxxxxxxx] On Behalf Of Greg Wilkins
Sent: woensdag 29 maart 2017 12:02
To: JETTY user mailing list
Subject: 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