Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Http header using Jboss6.1

Is there any way to set HttpHeader using Jboss6.1's configuration file. These configuration is applicable to a whole project.

I want to set bellow properties in Jboss6.1 server using its configuration file.

response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
response.setDateHeader("Expires", 0);

I tried it with domain.xml but nothing works.

like image 220
nidhin Avatar asked Oct 16 '15 10:10

nidhin


1 Answers

Don't complicate yourself. If you want every response has this header configuration, create your own filter to do this. This way you won't be coupled to JBoss and get what you want.

Here you have a filter sample:

package your.package;

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.HttpServletResponse;

public class NoCacheFilter implements Filter {

          @Override
          public void destroy() {
          }

          @Override
          public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
                  HttpServletResponse hsr = (HttpServletResponse) res;
                  hsr.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
                  hsr.setHeader("Pragma", "no-cache");
                  hsr.setDateHeader("Expires", 0);
                  chain.doFilter(req, res);
          }

          @Override
          public void init(FilterConfig arg0) throws ServletException {
          }
}

then you only have to configure it into your web.xml more or less this way:

<filter>
    <filter-name>noCacheFilter</filter-name>
    <filter-class>your.package.NoCacheFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>noCacheFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

This mapping is valid for all request, but you can adapt it.

Hope it helps!

like image 77
malaguna Avatar answered Nov 17 '22 01:11

malaguna