List of usage examples for javax.servlet.http HttpServletResponse getWriter
public PrintWriter getWriter() throws IOException;
PrintWriter
object that can send character text to the client. From source file:co.edu.UNal.ArquitecturaDeSoftware.Bienestar.Vista.App.Admin.CUDEventos.java
protected static void actualizarConvocatoria(HttpServletRequest request, HttpServletResponse response) throws IOException { ArrayList r = CtrlAdmin.actualizarConvocatoria(Integer.parseInt(request.getParameter("0")), // id request.getParameter("1"), // nombre request.getParameter("2"), // descripcin request.getParameter("4"), // fin Integer.parseInt(request.getParameter("6")) // cupos );//from www . j a v a2s .c o m response.setContentType("application/json;charset=UTF-8"); PrintWriter out = response.getWriter(); if (r.get(0) == "error") { JSONObject obj = new JSONObject(); obj.put("isError", true); obj.put("errorDescrip", r.get(1)); out.print(obj); } else if (r.get(0) == "isExitoso") { JSONObject obj = new JSONObject(); obj.put("Exitoso", true); out.print(obj); } else Util.errordeRespuesta(r, out); }
From source file:com.adnature.framework.util.Struts2Utils.java
/** * .//from w w w. j av a 2 s . c o m * * eg. <br/> * render("text/plain", "hello", "encoding:GBK"); <br/> * render("text/plain", "hello", "no-cache:false"); <br/> * 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:org.apache.hadoop.yarn.server.webproxy.WebAppProxyServlet.java
/** * Warn the user that the link may not be safe! * @param resp the http response/*from w w w . ja va 2s .com*/ * @param link the link to point to * @param user the user that owns the link. * @throws IOException on any error. */ private static void warnUserPage(HttpServletResponse resp, String link, String user, ApplicationId id) throws IOException { //Set the cookie when we warn which overrides the query parameter //This is so that if a user passes in the approved query parameter without //having first visited this page then this page will still be displayed resp.addCookie(makeCheckCookie(id, false)); resp.setContentType(MimeType.HTML); Page p = new Page(resp.getWriter()); p.html().h1("WARNING: The following page may not be safe!").h3()._("click ").a(link, "here") ._(" to continue to an Application Master web interface owned by ", user)._()._(); }
From source file:com.xqdev.jam.MLJAM.java
private static void sendStringResponse(HttpServletResponse res, String s) throws IOException { res.setContentType("text/plain; charset=UTF-8"); Writer w = res.getWriter(); w.write(s);/* w w w.j a va2 s. com*/ w.flush(); }
From source file:com.erudika.para.rest.RestUtils.java
/** * A generic JSON response handler//from w ww . ja va 2s . co m * @param response the response to write to * @param status status code * @param message error message */ public static void returnStatusResponse(HttpServletResponse response, int status, String message) { if (response == null) { return; } PrintWriter out = null; try { response.setStatus(status); response.setContentType(MediaType.APPLICATION_JSON); out = response.getWriter(); ParaObjectUtils.getJsonWriter().writeValue(out, getStatusResponse(Response.Status.fromStatusCode(status), message).getEntity()); } catch (Exception ex) { logger.error(null, ex); } finally { if (out != null) { out.close(); } } }
From source file:com.erudika.para.rest.RestUtils.java
/** * A generic JSON response returning an object. Status code is always {@code 200}. * @param response the response to write to * @param obj an object/*from ww w . ja va 2s .com*/ */ public static void returnObjectResponse(HttpServletResponse response, Object obj) { if (response == null) { return; } PrintWriter out = null; try { response.setStatus(HttpServletResponse.SC_OK); response.setContentType(MediaType.APPLICATION_JSON); out = response.getWriter(); ParaObjectUtils.getJsonWriter().writeValue(out, obj); } catch (Exception ex) { logger.error(null, ex); } finally { if (out != null) { out.close(); } } }
From source file:myproject.MyServer.java
public static void Add(HttpServletRequest request, HttpServletResponse response) throws IOException { try {//from w w w .j a v a 2 s . c o m String jsonString = IOUtils.toString(request.getInputStream()); JSONObject json = (JSONObject) JSONValue.parse(jsonString); String student_name = (String) json.get("student_name"); Long regno = (Long) json.get("regno"); Double cgpa = (Double) json.get("cgpa"); String query = String.format( "INSERT INTO student " + "(student_name, regno, cgpa) " + "VALUES('%s',%d,%f)", JSONValue.escape(student_name), regno, cgpa); database.runUpdate(query); String result = database.getStudent(regno); response.getWriter().write(result); } catch (Exception ex) { JSONObject output = new JSONObject(); output.put("error", "Connection failed: " + ex.getMessage()); response.getWriter().write(JSONValue.toJSONString(output)); } }
From source file:de.mpg.escidoc.services.fledgeddata.webservice.oaiServlet.java
/** * Get a response Writer depending on acceptable encodings * @param request the servlet's request information * @param response the servlet's response information * @exception IOException an I/O error occurred *//* w w w . j a v a 2 s .c o m*/ public static Writer getWriter(HttpServletRequest request, HttpServletResponse response) throws IOException { Writer out; String encodings = request.getHeader("Accept-Encoding"); if (encodings != null && encodings.indexOf("gzip") != -1) { response.setHeader("Content-Encoding", "gzip"); out = new OutputStreamWriter(new GZIPOutputStream(response.getOutputStream()), "UTF-8"); } else if (encodings != null && encodings.indexOf("deflate") != -1) { response.setHeader("Content-Encoding", "deflate"); out = new OutputStreamWriter(new DeflaterOutputStream(response.getOutputStream()), "UTF-8"); } else { out = response.getWriter(); } return out; }
From source file:org.artifactory.util.HttpUtils.java
public static void sendErrorResponse(HttpServletResponse response, int statusCode, String message) throws IOException { response.setContentType(MediaType.APPLICATION_JSON_VALUE); response.setStatus(statusCode);//from w ww . j a v a 2s . c o m ObjectMapper mapper = new ObjectMapper(); mapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true); ErrorResponse errorResponse = new ErrorResponse(statusCode, message); response.getWriter().write(mapper.writeValueAsString(errorResponse)); }
From source file:gov.nih.nci.evs.browser.servlet.ValueSetServlet.java
public static void create_vs_subtree(HttpServletRequest request, HttpServletResponse response, int view, String dictionary, String version, String vsd_uri) { request.getSession().removeAttribute("b"); request.getSession().removeAttribute("m"); response.setContentType("text/html"); PrintWriter out = null;//from w w w . java2s . c om //String checked_vocabularies = HTTPUtils.cleanXSS((String) request.getParameter("checked_vocabularies")); //String partial_checked_vocabularies = HTTPUtils.cleanXSS((String) request.getParameter("partial_checked_vocabularies")); //System.out.println("partial_checked_vocabularies: " + partial_checked_vocabularies); try { out = response.getWriter(); } catch (Exception ex) { ex.printStackTrace(); return; } //String message = (String) request.getSession().getAttribute("message"); try { out.close(); } catch (Exception ex) { ex.printStackTrace(); return; } }