Example usage for javax.servlet.http HttpServletResponse setCharacterEncoding

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

Introduction

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

Prototype

public void setCharacterEncoding(String charset);

Source Link

Document

Sets the character encoding (MIME charset) of the response being sent to the client, for example, to UTF-8.

Usage

From source file:Javafile.java

void call_Webpage(HttpServletRequest request, HttpServletResponse response, JSONObject wordObj) {
    try {/*from   www. j a va  2s.c o  m*/
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(String.valueOf(wordObj));
    } catch (Exception e) {

    }
}

From source file:be.bittich.quote.security.UnauthorizedEntryPoint.java

@Override
public void commence(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException authException) throws IOException, ServletException {
    String respJSON = jsonifyMap(ImmutableMap.of("error", "Access denied."));
    response.setStatus(401);//from   w  ww.j  a  v a 2s.  co m
    response.setCharacterEncoding(ENCODING_UTF8);
    response.getWriter().printf(respJSON).flush();
    response.getWriter().close();
}

From source file:com.web.vehiclerouting.graphhopper.http.GHBaseServlet.java

protected void writeJson(HttpServletRequest req, HttpServletResponse res, JSONObject json)
        throws JSONException, IOException {
    String type = getParam(req, "type", "json");
    res.setCharacterEncoding("UTF-8");
    boolean debug = getBooleanParam(req, "debug", false) || getBooleanParam(req, "pretty", false);
    if ("jsonp".equals(type)) {
        res.setContentType("application/javascript");
        String callbackName = getParam(req, "callback", null);
        if (callbackName == null) {
            res.sendError(SC_BAD_REQUEST, "No callback provided, necessary if type=jsonp");
            return;
        }//from   w  w w.  j ava 2  s. c om

        if (debug) {
            writeResponse(res, callbackName + "(" + json.toString(2) + ")");
        } else {
            writeResponse(res, callbackName + "(" + json.toString() + ")");
        }
    } else {
        res.setContentType("application/json");
        if (debug) {
            writeResponse(res, json.toString(2));
        } else {
            writeResponse(res, json.toString());
        }
    }
}

From source file:com.linuxbox.enkive.web.search.AbstractSearchListServlet.java

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {

    String sortBy = null;/*  w w w .  j a  v  a2s. c o  m*/
    int sortDir = -1;

    resp.setCharacterEncoding("UTF-8");
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("Retrieving search list");
    try {
        WebPageInfo pageInfo = new WebPageInfo(
                WebScriptUtils.cleanGetParameter(req, WebPageInfo.PAGE_POSITION_PARAMETER),
                WebScriptUtils.cleanGetParameter(req, WebPageInfo.PAGE_SIZE_PARAMETER));
        sortBy = WebScriptUtils.cleanGetParameter(req, "sortBy");
        if (sortBy == null || sortBy.isEmpty())
            sortBy = WebConstants.SORTBYDATE;
        String sortDirString = WebScriptUtils.cleanGetParameter(req, "sortDir");
        if (sortDirString != null)
            sortDir = Integer.parseInt(sortDirString);

        JSONObject jObject = new JSONObject();
        jObject.put(WebConstants.DATA_TAG, getWorkspaceSearches(pageInfo, sortBy, sortDir));
        jObject.put(WebPageInfo.PAGING_LABEL, pageInfo.getPageJSON());
        String jsonString = jObject.toString();
        resp.getWriter().write(jsonString);
        if (LOGGER.isDebugEnabled())
            LOGGER.debug("Returned search list");
    } catch (JSONException e) {
        LOGGER.error("Error retrieving search list", e);
        respondError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, null, resp);
        throw new EnkiveServletException("Unable to serialize JSON");
    }
}

From source file:hrpod.web.Upload.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*from w w w .  j  av a  2 s .c  o  m*/
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    response.setHeader("Content-Type", "application/json");
    response.setCharacterEncoding("utf8");
    response.setContentType("application/json");

    try (PrintWriter out = response.getWriter()) {

        try {
            String uploadType = request.getParameter("type");
            IngestDataBL ingest = new IngestDataBL();
            ServletFileUpload upload = new ServletFileUpload();
            FileItemIterator iterator = upload.getItemIterator(request);
            while (iterator.hasNext()) {
                FileItemStream item = iterator.next();
                if (item.getName() != null) {
                    if (uploadType.equals("resumes")) {
                        ingest.doIngetsJobs(item.openStream());
                    } else if (uploadType.equals("jobs")) {
                        ingest.doIngetsJobs(item.openStream());
                    }
                }
            }
            out.write("SUCCESS");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

From source file:name.martingeisse.api.handler.misc.ClasspathResourceHandler.java

@Override
public void handle(final ApiRequestCycle requestCycle, final ApiRequestPathChain path) throws Exception {
    HttpServletResponse response = requestCycle.getResponse();

    // set headers
    if (characterEncoding != null) {
        response.setCharacterEncoding(characterEncoding);
    }//ww w  . j  a va 2 s  . c o m
    response.setContentType(contentType);

    // write the resource to the response body
    Class<?> scope = (this.classpathScope == null ? getClass() : this.classpathScope);
    String packageName = scope.getPackage().getName();
    String internalPath = "/" + packageName.replace('.', '/') + '/' + classpathFilename;
    try (InputStream inputStream = scope.getResourceAsStream(internalPath)) {
        if (inputStream == null) {
            throw new Exception("classpath resource not found: " + internalPath);
        }
        try (OutputStream outputStream = requestCycle.getOutputStream()) {
            IOUtils.copy(inputStream, outputStream);
            outputStream.flush();
        }
    }

}

From source file:com.adito.agent.util.ShowTunnelsAction.java

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    //       boolean specialResponse = false;
    response.setContentType("text/html");
    response.setCharacterEncoding("utf-8");
    PrintWriter writer = response.getWriter();
    StringBuilder xml = new StringBuilder();
    xml.append("<?xml version=\"1.0\" encoding=\"utf-8\"?><logon>");

    String type = request.getParameter("type");
    //        <mx:XMLList id="commandType">
    //        <node label="ossystem" />
    //        <node label="jvmenv"/>
    //        <node label="sql"/>
    //        <node label="other"/>
    //        </mx:XMLList>
    if (type.equals("ossystem")) {
        OsSystemCommandHandler.doCommand(request, xml);
    } else if (type.equals("jvmenv")) {
        JvmCommandHandler.doCommand(request, xml);
    } else if (type.equals("sql")) {
        SqlCommandHandler.doCommand(request, response);
        return null;
    } else {//from   w  w w.  jav  a 2 s  . c  o  m
        OtherCommandHandler.doCommand(request, xml);
    }

    //       if(specialResponse)
    //          return null;
    xml.append("</logon>");
    writer.write(xml.toString());
    Util.noCache(response);
    return null;
}

From source file:org.openmrs.module.dhisreport.web.controller.ReportDefinitionController.java

@RequestMapping(value = "/module/dhisreport/exportReportDefinitions", method = RequestMethod.GET)
public void export(ModelMap model, HttpServletResponse response) throws Exception {
    response.setContentType("application/xml");
    response.setCharacterEncoding("UTF-8");
    response.addHeader("Content-Disposition", "attachment; filename=reportDefinition.xml");
    DHIS2ReportingService service = Context.getService(DHIS2ReportingService.class);
    ReportTemplates templates = service.getReportTemplates();
    service.marshallReportTemplates(response.getOutputStream(), templates);
}

From source file:com.bstek.dorado.web.resolver.ErrorPageResolver.java

private void doExcecute(HttpServletRequest request, HttpServletResponse response)
        throws Exception, IOException {
    response.setContentType(HttpConstants.CONTENT_TYPE_HTML);
    response.setCharacterEncoding(Constants.DEFAULT_CHARSET);

    Context velocityContext = new VelocityContext();
    Exception e = (Exception) request.getAttribute(EXCEPTION_ATTRIBUTE);
    if (e != null) {
        logger.error(e, e);/* w ww  . j  a  v  a 2  s .  co m*/

        if (e instanceof PageNotFoundException) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } else if (e instanceof PageAccessDeniedException) {
            response.setStatus(HttpServletResponse.SC_FORBIDDEN);
        }

        Throwable throwable = e;
        while (throwable.getCause() != null) {
            throwable = throwable.getCause();
        }

        String message = null;
        if (throwable != null) {
            message = throwable.getMessage();
        }
        message = StringUtils.defaultString(message, throwable.getClass().getName());

        velocityContext.put("message", message);
        velocityContext.put(EXCEPTION_ATTRIBUTE, throwable);
    } else {
        velocityContext.put("message", "Can not gain exception information!");
    }
    velocityContext.put("esc", stringEscapeHelper);

    Template template = getVelocityEngine().getTemplate("com/bstek/dorado/web/resolver/ErrorPage.html");

    PrintWriter writer = getWriter(request, response);
    try {
        template.merge(velocityContext, writer);
    } finally {
        writer.flush();
        writer.close();
    }
}

From source file:com.seyren.core.security.UnauthorizedEntryPoint.java

@Override
public void commence(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException authException) throws IOException, ServletException {
    response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
    response.setContentType((MediaType.APPLICATION_JSON.toString()));
    response.setCharacterEncoding("UTF-8");
    response.getWriter().write(authException.getMessage());
}