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:com.emaxcore.emaxdata.common.web.Servlets.java

/**
 * ??Header./*from   w ww.  j av  a2 s .com*/
 *
 * @param fileName ???.
 */
public static void setFileDownloadHeader(HttpServletResponse response, String fileName) {
    try {
        // ???
        String encodedfileName = new String(fileName.getBytes(), "ISO8859-1");
        response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + encodedfileName + "\"");
    } catch (UnsupportedEncodingException e) {
        logger.warn(e + "");
        e.getMessage();
    }
}

From source file:com.innovate.cms.common.web.Servlets.java

/**
 * ??Header.//from  w  ww  . j a  v  a  2s  . co m
 * 
 * @param fileName ???.
 */
public static void setFileDownloadHeader(HttpServletResponse response, String fileName) {
    try {
        // ???
        String encodedfileName = new String(fileName.getBytes(), "ISO8859-1");
        response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + encodedfileName + "\"");
    } catch (UnsupportedEncodingException e) {
        e.getMessage();
    }
}

From source file:gov.nist.appvet.tool.synchtest.util.ReportUtil.java

/**
 * This method returns report information to the AppVet ToolAdapter as ASCII
 * text and cannot attach a file to the response.
 *///w  w w . ja  va2s .c om
public static boolean sendInHttpResponse(HttpServletResponse response, String reportText,
        ToolStatus reportStatus) {
    try {
        response.setStatus(HttpServletResponse.SC_OK); // HTTP 200
        response.setContentType("text/html");
        response.setHeader("apprisk", reportStatus.name());
        PrintWriter out = response.getWriter();
        out.println(reportText);
        out.flush();
        out.close();
        log.info("Returned report");
        return true;
    } catch (IOException e) {
        log.error("Report not sent: " + e.toString());
        return false;
    }
}

From source file:cc.sion.core.web.Servlets.java

/**
 *  Header.//from w  w w .  j  av a2s  .  c o m
 */
public static void setExpiresHeader(HttpServletResponse response, long expiresSeconds) {
    // Http 1.0 header, set a fix expires date.
    response.setDateHeader(HttpHeaders.EXPIRES, System.currentTimeMillis() + (expiresSeconds * 1000));
    // Http 1.1 header, set a time after now.
    response.setHeader(HttpHeaders.CACHE_CONTROL, "private, max-age=" + expiresSeconds);
}

From source file:com.zving.platform.SysInfo.java

public static void downloadDB(HttpServletRequest request, HttpServletResponse response) {
    try {/*from   ww  w . j  a  v a  2 s . c o  m*/
        request.setCharacterEncoding(Constant.GlobalCharset);
        response.reset();
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition",
                "attachment; filename=DB_" + DateUtil.getCurrentDate("yyyyMMddHHmmss") + ".dat");
        OutputStream os = response.getOutputStream();

        String path = Config.getContextRealPath() + "WEB-INF/data/backup/DB_"
                + DateUtil.getCurrentDate("yyyyMMddHHmmss") + ".dat";
        new DBExport().exportDB(path);

        byte[] buffer = new byte[1024];
        int read = -1;
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(path);
            while ((read = fis.read(buffer)) != -1)
                if (read > 0) {
                    byte[] chunk = (byte[]) null;
                    if (read == 1024) {
                        chunk = buffer;
                    } else {
                        chunk = new byte[read];
                        System.arraycopy(buffer, 0, chunk, 0, read);
                    }
                    os.write(chunk);
                    os.flush();
                }
        } finally {
            if (fis != null) {
                fis.close();
            }
        }
        os.flush();
        os.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.xsw.utils.Servlets.java

/**
 *  Header.//from w  ww .j a  v a2  s.com
 */
public static void setExpiresHeader(HttpServletResponse response, long expiresSeconds) {
    // Http 1.0 header, set a fix expires date.
    response.setDateHeader("Expires", System.currentTimeMillis() + expiresSeconds * 1000);
    // Http 1.1 header, set a time after now.
    response.setHeader("Cache-Control", "private, max-age=" + expiresSeconds);
}

From source file:com.groupon.odo.HttpUtilities.java

public static HttpServletResponse addHeader(HttpServletResponse response, Object[] headerPair) {
    // set header
    response.setHeader(headerPair[0].toString(), headerPair[1].toString());

    return response;
}

From source file:cn.com.qiqi.order.utils.Servlets.java

/**
 *  Header.//from  w  ww.ja  v a  2 s . co  m
 */
public static void setExpiresHeader(HttpServletResponse response, long expiresSeconds) {
    // Http 1.0 header, set a fix expires date.
    response.setDateHeader(HttpHeaders.EXPIRES, System.currentTimeMillis() + expiresSeconds * 1000);
    // Http 1.1 header, set a time after now.
    response.setHeader(HttpHeaders.CACHE_CONTROL, "private, max-age=" + expiresSeconds);
}

From source file:com.igrow.mall.common.util.Struts2Utils.java

/**
 * ./*from  w ww  . j  av a  2  s .c o  m*/
        
 * eg.
 * render("text/plain", "hello", "encoding:GBK");
 * render("text/plain", "hello", "no-cache:false");
 * render("text/plain", "hello", "encoding:GBK", "no-cache:false");
 * 
 * @param headers ??header??"encoding:""no-cache:",UTF-8true.
 */
public static void render(final String contentType, final String content, final String... headers) {
    try {
        //?headers?
        String encoding = ENCODING_DEFAULT;
        boolean noCache = NOCACHE_DEFAULT;
        for (String header : headers) {
            String headerName = StringUtils.substringBefore(header, ":");
            String headerValue = StringUtils.substringAfter(header, ":");

            if (StringUtils.equalsIgnoreCase(headerName, ENCODING_PREFIX)) {
                encoding = headerValue;
            } else if (StringUtils.equalsIgnoreCase(headerName, NOCACHE_PREFIX)) {
                noCache = Boolean.parseBoolean(headerValue);
            } else
                throw new IllegalArgumentException(headerName + "??header");
        }

        HttpServletResponse response = ServletActionContext.getResponse();

        //headers?
        String fullContentType = contentType + ";charset=" + encoding;
        response.setContentType(fullContentType);
        if (noCache) {
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
        }

        response.getWriter().write(content);
        response.getWriter().flush();

    } catch (IOException e) {
        logger.error(e.getMessage(), e);
    }
}

From source file:fiftyone.mobile.detection.webapp.JavascriptProvider.java

static void sendJavaScript(HttpServletRequest request, HttpServletResponse response, Dataset dataSet,
        StringBuilder javascript) throws IOException {

    response.reset();/*from www . ja  va2 s  .  c  o  m*/
    response.setContentType("application/x-javascript");
    response.setCharacterEncoding("UTF-8");
    response.setHeader("Vary", "User-Agent");
    response.setHeader("Cache-Control", "public");
    response.setHeader("Expires", dataSet.nextUpdate.toString());
    response.setHeader("Last-Modified", dataSet.published.toString());
    try {
        response.setHeader("ETag", eTagHash(dataSet, request));
    } catch (Exception ex) {
        // The response doesn't support eTags. Nothing we can do.
    }
    response.setBufferSize(DEFAULT_BUFFER_SIZE);
    response.setHeader("Content-Length", Integer.toString(javascript.length()));

    response.getOutputStream().println(javascript.toString());
    response.getOutputStream().flush();
    response.getOutputStream().close();
}