Example usage for javax.servlet.http HttpServletResponse getWriter

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

Introduction

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

Prototype

public PrintWriter getWriter() throws IOException;

Source Link

Document

Returns a PrintWriter object that can send character text to the client.

Usage

From source file:me.ywork.ticket.suite.controller.DingServiceController.java

/**
 * ??/*from  www.  j  a v  a 2s  .  c  om*/
 */
private static void doSuccessResponse(DingTalkEncryptor dingTalkEncryptor, HttpServletResponse response,
        String encryptStr, String msgSignature, String timeStamp, String nonceStr) {
    try {
        Long longTime = Long.parseLong(timeStamp);
        Map<String, String> jsonMap = dingTalkEncryptor.getEncryptedMap(encryptStr, longTime, nonceStr);

        PrintWriter printWriter = response.getWriter();
        printWriter.print(JSONObject.toJSONString(jsonMap));
        printWriter.close();
        printWriter = null;
    } catch (Exception e) {
        logger.error("doSuccessResponse", e);
    }
}

From source file:ee.ria.xroad.common.conf.globalconf.ConfigurationClientMain.java

private static void setup() throws Exception {
    log.trace("setUp()");

    client = createClient();/*from  w w  w.  j ava  2  s . c om*/

    adminPort = new AdminPort(SystemProperties.getConfigurationClientAdminPort());

    adminPort.addShutdownHook(() -> {
        log.info("Configuration client shutting down...");
        try {
            shutdown();
        } catch (Exception e) {
            log.error("Error while shutting down", e);
        }
    });

    adminPort.addHandler("/execute", new AdminPort.SynchronousCallback() {
        @Override
        public void handle(HttpServletRequest request, HttpServletResponse response) {
            log.info("handler /execute");
            try {
                client.execute();
            } catch (Exception e) {
                throw translateException(e);
            }
        }
    });

    adminPort.addHandler("/status", new AdminPort.SynchronousCallback() {
        @Override
        public void handle(HttpServletRequest request, HttpServletResponse response) {
            try {
                log.info("handler /status");
                response.setCharacterEncoding("UTF8");
                JsonUtils.getSerializer().toJson(ConfigurationClientJobListener.getStatus(),
                        response.getWriter());
            } catch (Exception e) {
                log.error("Error getting conf client status", e);
            }
        }
    });
}

From source file:com.dien.upload.server.UploadServlet.java

/**
 * Writes a XML response to the client. 
 * The message must be a text which will be wrapped in an XML structure.
 * // w  w w. j a  v a  2  s  .  c  om
 * Note: if the request is a POST, the response should set the content type 
 *  to text/html or text/plain in order to be able in the client side to
 *  read the iframe body (submitCompletEvent.getResults()), otherwise the
 *  method returns null 
 * 
 * @param request
 * @param response
 * @param message
 * @param post
 *        specify whether the request is post or not.   
 * @throws IOException
 */
protected static void renderXmlResponse(HttpServletRequest request, HttpServletResponse response,
        String message, boolean post) throws IOException {

    //json????

    response.setContentType("text/html;charset=utf-8");
    PrintWriter out;
    try {
        out = response.getWriter();
        out.write("<html><body><textarea>" + message + "</textarea></body></html>");
        out.flush();
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:MyServlet.java

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    res.setContentType("text/plain");
    PrintWriter out = res.getWriter();

    res.setHeader("Refresh", "10");
    out.println(new Date().toString());
}

From source file:RedirectNewLocation.java

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    PrintWriter out = response.getWriter();

    response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
    response.setHeader("Location", "http://www.java2s.com");

    response.setContentType("text/html");
    return;/*from  w ww .jav  a 2  s.  c o  m*/
}

From source file:MyServlet.java

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    res.setContentType("text/plain");
    PrintWriter out = res.getWriter();
    count++;/*from w  w w. j  av  a  2s .  c o  m*/
    out.println("Since loading, this servlet has been accessed " + count + " times.");
}

From source file:com.berwickheights.spring.mvc.view.ResponseDataStreamView.java

@SuppressWarnings("unchecked")
@Override// w  w  w. java 2s  . c  om
public void render(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception {
    response.setContentType(this.contentType);
    response.getWriter().write((String) model.get(KEY));
}

From source file:ste.web.beanshell.jetty.BeanShellUtils.java

public static void setup(final Interpreter interpreter, final HttpServletRequest request,
        final HttpServletResponse response) throws EvalError, IOException {
    ///*from w w  w  .  ja v  a 2  s  . co m*/
    // Set attributes as script variables
    //
    String k, key;
    for (Enumeration e = request.getAttributeNames(); e.hasMoreElements();) {
        k = (String) e.nextElement();
        key = normalizeVariableName(k);
        interpreter.set(key, request.getAttribute(k));
    }

    //
    // Set request parameters as script variables. Note that parameters
    // override attributes
    //
    for (Enumeration e = request.getParameterNames(); e.hasMoreElements();) {
        k = (String) e.nextElement();
        key = normalizeVariableName(k);
        interpreter.set(key, request.getParameter(k));
    }

    interpreter.set(VAR_REQUEST, request);
    interpreter.set(VAR_RESPONSE, response);
    interpreter.set(VAR_SESSION, request.getSession(false));
    interpreter.set(VAR_OUT, response.getWriter());
    interpreter.set(VAR_LOG, log);
    if (hasJSONBody(request)) {
        interpreter.set(VAR_BODY, getJSONBody(request.getInputStream()));
    }
}

From source file:Copyright.java

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, java.io.IOException {

    java.io.PrintWriter out = response.getWriter();
    out.println("Copyright&copy; 2003-2004 Java Source and Support.");
    out.close();//from w  w  w . j  a  v a2  s  .  c  om

}

From source file:com.berrysys.ussdgw.HttpUtils.java

/**
 * Do post.//from   www.ja  va  2s . c  om
 *
 * @param req the req
 * @param resp the resp
 * @param dialogListener the dialog listener
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static void doPost(HttpServletRequest req, HttpServletResponse resp, final DialogListener dialogListener)
        throws IOException {

    boolean available = GlobalNetworkInitiatedSemaphore.getInstance().getSemaphore().tryAcquire();

    if (!available) {
        queueFullLogic(resp);
        return;
    }

    final String ussdAppUrl = req.getHeader("ussd-app-url");

    log.trace(String.format("ussd-app-url: %s", ussdAppUrl));

    final String payload = getPayload(req);

    log.trace(String.format("payload: %s", payload));

    HawtDispatchUtil.getInstance().queueExecute(new Runnable() {

        @Override
        public void run() {
            processHttpRequest(payload, ussdAppUrl, dialogListener);
        }

    });

    String response = "Request received successfully.";

    resp.setContentType("text/html");
    resp.setStatus(HttpServletResponse.SC_OK);
    resp.getWriter().println(response);

}