Example usage for javax.servlet.http HttpServletResponse setHeader

List of usage examples for javax.servlet.http HttpServletResponse setHeader

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletResponse setHeader.

Prototype

public void setHeader(String name, String value);

Source Link

Document

Sets a response header with the given name and value.

Usage

From source file:org.gwtwidgets.server.spring.ServletUtils.java

/**
 * Adjusts HTTP headers so that browsers won't cache response.
 * @param response//  www . ja  v  a  2s .co  m
 * For more background see <a href="http://www.onjava.com/pub/a/onjava/excerpt/jebp_3/index2.html">this</a>.
 */
public static void disableResponseCaching(HttpServletResponse response) {
    response.setHeader("Expires", "Sat, 1 January 2000 12:00:00 GMT");
    response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
    response.addHeader("Cache-Control", "post-check=0, pre-check=0");
    response.setHeader("Pragma", "no-cache");
}

From source file:Main.java

/**
 * Add headers to prevent browsers and proxies from caching this reply.
 * @param resp The response to add headers to
 *///from  w w w  .j a  v  a  2 s . c o  m
public static void addNoCacheHeaders(HttpServletResponse resp) {
    // Set standard HTTP/1.1 no-cache headers.
    resp.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");

    // Set IE extended HTTP/1.1 no-cache headers (use addHeader).
    resp.addHeader("Cache-Control", "post-check=0, pre-check=0");

    // Set standard HTTP/1.0 no-cache header.
    resp.setHeader("Pragma", "no-cache");

    // Set to expire far in the past. Prevents caching at the proxy server
    resp.setHeader("Expires", "Sat, 6 May 1995 12:00:00 GMT");
}

From source file:cz.cas.lib.proarc.authentication.utils.AuthUtils.java

/**
 * Writes the authentication required status to the HTTP response.
 * @param response response//  w w  w  .j ava2 s  .c o  m
 * @throws IOException failure
 * @see #HEADER_AUTHENTICATE_TYPE
 * @see <a href='http://www.smartclient.com/smartgwt/javadoc/com/smartgwt/client/docs/Relogin.html'>SmartGWT Relogin</a>
 */
public static void setLoginRequiredResponse(HttpServletResponse response) throws IOException {
    response.setHeader(HEADER_AUTHENTICATE_TYPE, Authenticators.getInstance().getLoginType());
    response.setStatus(HttpServletResponse.SC_FORBIDDEN);
    response.setContentType(MediaType.TEXT_HTML);
    InputStream res = ProarcAuthFilter.class.getResourceAsStream("loginRequiredMarker.html");
    try {
        IOUtils.copy(res, response.getOutputStream());
        res.close();
    } finally {
        IOUtils.closeQuietly(res);
    }
}

From source file:org.myjerry.util.ResponseUtil.java

public static final void setNoCache(HttpServletResponse response, boolean isHttp11) {
    if (isHttp11) {
        response.setHeader("Cache-Control", "no-cache");
    } else {/*from   ww w  .  ja v  a 2 s .com*/
        response.setHeader("Pragma", "no-cache");
        response.addDateHeader("Expires", 0L);
        response.setHeader("Cache-Control", "no-store");
    }
}

From source file:br.org.ipti.guigoh.util.DownloadService.java

public static synchronized void downloadFile(File file, String mimeType) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext context = facesContext.getExternalContext();
    HttpServletResponse response = (HttpServletResponse) context.getResponse();
    response.setHeader("Content-Disposition", "attachment;filename=\"" + file.getName() + "\"");
    response.setContentLength((int) file.length());
    response.setContentType(mimeType);/*from   w  w  w  .  jav  a 2s.c o  m*/
    try {
        OutputStream out;
        try (FileInputStream in = new FileInputStream(file)) {
            out = response.getOutputStream();
            byte[] buf = new byte[(int) file.length()];
            int count;
            while ((count = in.read(buf)) >= 0) {
                out.write(buf, 0, count);
            }
        }
        out.flush();
        out.close();
        facesContext.responseComplete();
    } catch (IOException ex) {
        System.out.println("Error in downloadFile: " + ex.getMessage());
    }
}

From source file:org.lightadmin.core.web.util.ResponseUtils.java

public static void addImageResourceHeaders(HttpServletResponse response, HttpHeaders httpHeaders) {
    for (String httpHeaderKey : httpHeaders.keySet()) {
        response.setHeader(httpHeaderKey, httpHeaders.get(httpHeaderKey).get(0));
    }//  www .ja v a2 s  .c  o  m
}

From source file:de.itsvs.cwtrpc.core.CwtRpcUtils.java

public static void addNoCacheResponseHeaders(HttpServletRequest request, HttpServletResponse response) {
    response.setHeader("Cache-Control", "no-cache, no-store, no-transform");
    response.setDateHeader("Expires", -1L);
    response.setHeader("Pragma", "no-cache");
}

From source file:com.kwoksys.framework.util.HttpUtils.java

public static void setDownloadResponseHeaders(HttpServletResponse response, String fileName) {
    // To work around file download problems on IE
    response.setHeader("Pragma", "public"); // For HTTP/1.0 backward compatibility.
    response.setHeader("Cache-Control", "public"); // For HTTP/1.1.

    // Set filename
    response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
}

From source file:dk.dma.msinm.common.util.WebUtils.java

/**
 * Add headers to the response to ensure no caching takes place
 * @param response the response//from  ww  w  .j a v  a 2s.  com
 * @return the response
 */
public static HttpServletResponse nocache(HttpServletResponse response) {
    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("Cache-Control", "no-store");
    response.setHeader("Pragma", "no-cache");
    response.setDateHeader("Expires", 0);
    return response;
}

From source file:net.hj2eplatform.core.component.CustomAuthenticationSuccessHandler.java

public static void sendJsonResponse(HttpServletResponse response, String key, String message) {
    response.setContentType("application/json;charset=UTF-8");
    response.setHeader("Cache-Control", "no-cache");
    try {/*from w  w w. j  a v  a2  s. co  m*/
        response.getWriter().write(String.format(JSON_VALUE, key, message));
    } catch (IOException e) {
        LOGGER.error("error writing json to response", e);
    }
}