Example usage for javax.servlet.http HttpServletResponse reset

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

Introduction

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

Prototype

public void reset();

Source Link

Document

Clears any data that exists in the buffer as well as the status code, headers.

Usage

From source file:fll.web.api.CategoryScheduleMappingServlet.java

@Override
protected final void doGet(final HttpServletRequest request, final HttpServletResponse response)
        throws IOException, ServletException {
    final ServletContext application = getServletContext();

    final DataSource datasource = ApplicationAttributes.getDataSource(application);
    Connection connection = null;
    try {/* w w  w.  j  a v  a2 s .c o m*/
        connection = datasource.getConnection();

        final ObjectMapper jsonMapper = new ObjectMapper();

        response.reset();
        response.setContentType("application/json");
        final PrintWriter writer = response.getWriter();

        final int currentTournament = Queries.getCurrentTournament(connection);

        final Collection<CategoryColumnMapping> mappings = CategoryColumnMapping.load(connection,
                currentTournament);

        jsonMapper.writeValue(writer, mappings);
    } catch (final SQLException e) {
        LOGGER.fatal("Database Exception", e);
        throw new RuntimeException(e);
    } finally {
        SQLFunctions.close(connection);
    }

}

From source file:fll.web.api.ScheduleServlet.java

@Override
protected final void doGet(final HttpServletRequest request, final HttpServletResponse response)
        throws IOException, ServletException {
    final ServletContext application = getServletContext();

    final DataSource datasource = ApplicationAttributes.getDataSource(application);
    Connection connection = null;
    try {//from w w w .  ja v a 2  s .  co m
        connection = datasource.getConnection();

        final ObjectMapper jsonMapper = new ObjectMapper();

        response.reset();
        response.setContentType("application/json");
        final PrintWriter writer = response.getWriter();

        final int currentTournament = Queries.getCurrentTournament(connection);
        if (TournamentSchedule.scheduleExistsInDatabase(connection, currentTournament)) {
            final TournamentSchedule schedule = new TournamentSchedule(connection, currentTournament);

            jsonMapper.writeValue(writer, schedule);
        } else {
            jsonMapper.writeValue(writer, "");
        }

    } catch (final SQLException e) {
        throw new RuntimeException(e);
    } finally {
        SQLFunctions.close(connection);
    }

}

From source file:com.node.action.UserAction.java

/**
 * //from  w ww  .  j  a  va 2  s  .c om
 * ??excel
 * 
 * @param request
 * @param response
 * @version: 1.0
 * @author: liuwu
 * @version: 201642 ?5:53:39
 */
@RequestMapping("/exportExcel")
public void exportExcel(HttpServletRequest request, HttpServletResponse response) {
    response.reset();
    try {
        request.setCharacterEncoding("UTF-8");
    } catch (UnsupportedEncodingException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    String hf = request.getParameter("hfs");
    String type = request.getParameter("type");
    String exportname = "grid";
    try {
        if (type.equals("excel")) {
            exportname += ".xls";
            response.setHeader("Content-disposition",
                    "attachment; filename=" + java.net.URLEncoder.encode(exportname, "UTF-8") + "");
            response.setContentType("application/msexcel;charset=utf-8");
        } else if (type.equals("word")) {
            exportname += ".doc";
            response.setHeader("Content-disposition",
                    "attachment; filename=" + java.net.URLEncoder.encode(exportname, "UTF-8") + "");
            response.setContentType("application/ms-word;charset=UTF-8");
        }
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    PrintWriter out;
    try {
        out = response.getWriter();
        out.println(hf);
        out.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:org.fenixedu.academic.ui.struts.action.candidate.degree.DegreeCandidacyManagementDispatchAction.java

public ActionForward showSummaryFile(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request,
        HttpServletResponse response) {
    CandidacySummaryFile file = getCandidacy(request).getSummaryFile();

    response.reset();
    try {//from   w  w w  . jav a2 s .co  m
        response.getOutputStream().write(file.getContent());
        response.setContentLength(file.getContent().length);
        response.setContentType("application/pdf");
        response.flushBuffer();
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
    }

    return null;
}

From source file:cn.orignzmn.shopkepper.common.utils.excel.ExportExcel.java

/**
 * /*from  www.  ja  v a2  s . co  m*/
 * @param fileName ??
 */
public ExportExcel write(HttpServletResponse response, String fileName) throws IOException {
    response.reset();
    response.setContentType("application/octet-stream; charset=utf-8");
    response.setHeader("Content-Disposition", "attachment; filename=" + Encodes.urlEncode(fileName));
    write(response.getOutputStream());
    return this;
}

From source file:com.delmar.core.web.filter.ExportDelegate.java

/**
 * Actually writes exported data. Extracts content from the Map stored in request with the
 * <code>TableTag.FILTER_CONTENT_OVERRIDE_BODY</code> key.
 * @param wrapper BufferedResponseWrapper implementation
 * @param response HttpServletResponse/* w  w w.  j  a v a2 s . c  o m*/
 * @param request ServletRequest
 * @throws java.io.IOException exception thrown by response writer/outputStream
 */
public static void writeExport(HttpServletResponse response, ServletRequest request,
        BufferedResponseWrapper wrapper) throws IOException {

    if (wrapper.isOutRequested()) {
        // data already written
        log.debug("Filter operating in unbuffered mode. Everything done, exiting");
        return;
    }

    // if you reach this point the PARAMETER_EXPORTING has been found, but the special header has never been set in
    // response (this is the signal from table tag that it is going to write exported data)
    log.debug("Filter operating in buffered mode. ");

    Map bean = (Map) request.getAttribute(TableTag.FILTER_CONTENT_OVERRIDE_BODY);

    if (log.isDebugEnabled()) {
        log.debug(bean);
    }

    Object pageContent = bean.get(TableTagParameters.BEAN_BODY);

    if (pageContent == null) {
        if (log.isDebugEnabled()) {
            log.debug("Filter is enabled but exported content has not been found. Maybe an error occurred?");
        }

        response.setContentType(wrapper.getContentType());
        PrintWriter out = response.getWriter();

        out.write(wrapper.getContentAsString());
        out.flush();
        return;
    }

    // clear headers
    if (!response.isCommitted()) {
        response.reset();
    }

    String filename = (String) bean.get(TableTagParameters.BEAN_FILENAME);
    String contentType = (String) bean.get(TableTagParameters.BEAN_CONTENTTYPE);

    if (StringUtils.isNotBlank(filename)) {
        response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
    }

    String characterEncoding = wrapper.getCharacterEncoding();
    String wrappedContentType = wrapper.getContentType();

    if (wrappedContentType != null && wrappedContentType.indexOf("charset") > -1) {
        // charset is already specified (see #921811)
        characterEncoding = StringUtils.substringAfter(wrappedContentType, "charset=");
    }

    if (characterEncoding != null && contentType.indexOf("charset") == -1) //$NON-NLS-1$
    {
        contentType += "; charset=" + characterEncoding; //$NON-NLS-1$
    }

    response.setContentType(contentType);

    if (pageContent instanceof String) {
        // text content
        if (characterEncoding != null) {
            response.setContentLength(((String) pageContent).getBytes(characterEncoding).length);
        } else {
            //FIXME  Reliance on default encoding
            // Found a call to a method which will perform a byte to String (or String to byte) conversion,
            // and will assume that the default platform encoding is suitable.
            // This will cause the application behaviour to vary between platforms.
            // Use an alternative API and specify a charset name or Charset object explicitly.
            response.setContentLength(((String) pageContent).getBytes().length);
        }

        PrintWriter out = response.getWriter();
        out.write((String) pageContent);
        out.flush();
    } else {
        // dealing with binary content
        byte[] content = (byte[]) pageContent;
        response.setContentLength(content.length);
        OutputStream out = response.getOutputStream();
        out.write(content);
        out.flush();
    }
}

From source file:org.lazulite.boot.autoconfigure.core.utils.excel.ExportExcel.java

/**
 * //ww w.  j  a v  a 2 s .  c  o  m
 *
 * @param fileName ??
 */
public ExportExcel write(HttpServletResponse response, String fileName) throws IOException {
    response.reset();
    response.setContentType("application/octet-stream; charset=utf-8");
    response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
    write(response.getOutputStream());
    return this;
}

From source file:fll.web.api.TournamentsServlet.java

@Override
protected final void doGet(final HttpServletRequest request, final HttpServletResponse response)
        throws IOException, ServletException {
    final ServletContext application = getServletContext();

    final DataSource datasource = ApplicationAttributes.getDataSource(application);
    Connection connection = null;
    try {// w  ww  . j av  a 2s.  c om
        connection = datasource.getConnection();

        final ObjectMapper jsonMapper = new ObjectMapper();

        response.reset();
        response.setContentType("application/json");
        final PrintWriter writer = response.getWriter();

        final String pathInfo = request.getPathInfo();
        if (null != pathInfo && pathInfo.length() > 1) {
            final String tournamentStr = pathInfo.substring(1);

            int id;
            if ("current".equals(tournamentStr)) {
                id = Queries.getCurrentTournament(connection);
            } else {
                try {
                    id = Integer.parseInt(tournamentStr);
                } catch (final NumberFormatException e) {
                    throw new RuntimeException("Error parsing tournament id " + tournamentStr, e);
                }
            }

            final Tournament tournament = Tournament.findTournamentByID(connection, id);
            if (null != tournament) {
                jsonMapper.writeValue(writer, tournament);
                return;
            } else {
                throw new RuntimeException("No tournament found with id " + id);
            }

        }

        final Collection<Tournament> tournaments = Tournament.getTournaments(connection);

        jsonMapper.writeValue(writer, tournaments);
    } catch (final SQLException e) {
        throw new RuntimeException(e);
    } finally {
        SQLFunctions.close(connection);
    }

}

From source file:com.feilong.controller.DownloadController.java

public void downloadLocal(HttpServletResponse response) throws FileNotFoundException {
    // //from w w  w.j  ava2 s. c  o m
    String fileName = "Operator.doc".toString(); // ???
    // ?
    InputStream inStream = new FileInputStream("c:/Operator.doc");// 
    // ?
    response.reset();
    response.setContentType("bin");
    response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
    // ???
    byte[] b = new byte[100];
    int len;
    try {
        while ((len = inStream.read(b)) > 0)
            response.getOutputStream().write(b, 0, len);
        inStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}