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:MyServlet.java

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

    String code = null, message = null, type = null, uri = null;
    Object codeObj, messageObj, typeObj;
    Throwable throwable;//  w ww. j a v  a 2 s. c o m

    // Retrieve the three possible error attributes, some may be null
    codeObj = req.getAttribute("javax.servlet.error.status_code");
    messageObj = req.getAttribute("javax.servlet.error.message");
    typeObj = req.getAttribute("javax.servlet.error.exception_type");
    throwable = (Throwable) req.getAttribute("javax.servlet.error.exception");
    uri = (String) req.getAttribute("javax.servlet.error.request_uri");

    if (uri == null) {
        uri = req.getRequestURI(); // in case there's no URI given
    }

    // Convert the attributes to string values
    if (codeObj != null)
        code = codeObj.toString();
    if (messageObj != null)
        message = messageObj.toString();
    if (typeObj != null)
        type = typeObj.toString();

    // The error reason is either the status code or exception type
    String reason = (code != null ? code : type);

    out.println("<HTML>");
    out.println("<HEAD><TITLE>" + reason + ": " + message + "</TITLE></HEAD>");
    out.println("<BODY>");
    out.println("<H1>" + reason + "</H1>");
    out.println("<H2>" + message + "</H2>");
    out.println("<PRE>");
    if (throwable != null) {
        throwable.printStackTrace(out);
    }
    out.println("</PRE>");
    out.println("<HR>");
    out.println("<I>Error accessing " + uri + "</I>");
    out.println("</BODY></HTML>");
}

From source file:fi.arcusys.oulu.web.AjaxView.java

@Override
protected void renderMergedOutputModel(Map<String, Object> map, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    if (map == null || map.isEmpty()) {
        JSONObject.fromObject("{}").write(response.getWriter());
        return;//from   w w  w . j a va 2 s. c o  m
    }

    JSON json = JSONSerializer.toJSON(map);
    json.write(response.getWriter());
}

From source file:net.cristcost.study.services.ServiceHttpRequestHandler.java

@Override
public void handleRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    response.setContentType("text/plain");

    PrintWriter writer = response.getWriter();

    ServiceTestUtil.testSecurity(this.getClass().getSimpleName(), request, writer, authenticationManager,
            services);/*from  w ww. j a  v  a 2s .c o  m*/
}

From source file:net.cristcost.study.services.ServiceServlet.java

@Override
public void service(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    response.setContentType("text/plain");

    PrintWriter writer = response.getWriter();

    ServiceTestUtil.testSecurity(this.getClass().getSimpleName(), request, writer, authenticationManager,
            services);/*from  www  .  j a  v  a  2s. c o m*/
}

From source file:pl.maciejwalkowiak.plist.spring.PlistView.java

@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    String result = plistSerializer.toXmlPlist(filterModel(model));

    response.getWriter().write(result);
}

From source file:arena.web.view.ErrorView.java

@SuppressWarnings("unchecked")
protected void renderMergedOutputModel(Map model, HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    if (this.message != null) {
        response.setStatus(this.errorCode);
        response.getWriter()
                .append(ServletUtils.replaceWildcards(this.message, this.allowRequestArgs, model, request));
        response.flushBuffer();/*from  ww  w.j  av a2s . c o  m*/
    } else {
        response.sendError(this.errorCode);
    }
}

From source file:com.mykarsol.appconnectivity.imageload.java

/**
 * Handles the HTTP <code>GET</code> method.
 *
 * @param request servlet request//from www  .ja va  2  s.  c o m
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    try (PrintWriter out = response.getWriter()) {
        /* TODO output your page here. You may use following sample code. */

    }

}

From source file:ErrorDisplay.java

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

    String code = null, message = null, type = null, uri = null;
    Object codeObj, messageObj, typeObj;
    Throwable throwable;//from  ww w  .j a  va  2s  .  co  m

    codeObj = req.getAttribute("javax.servlet.error.status_code");
    messageObj = req.getAttribute("javax.servlet.error.message");
    typeObj = req.getAttribute("javax.servlet.error.exception_type");
    throwable = (Throwable) req.getAttribute("javax.servlet.error.exception");
    uri = (String) req.getAttribute("javax.servlet.error.request_uri");

    if (uri == null) {
        uri = req.getRequestURI(); // in case there's no URI given
    }

    // Convert the attributes to string values
    if (codeObj != null)
        code = codeObj.toString();
    if (messageObj != null)
        message = messageObj.toString();
    if (typeObj != null)
        type = typeObj.toString();

    // The error reason is either the status code or exception type
    String reason = (code != null ? code : type);

    out.println("<HTML>");
    out.println("<HEAD><TITLE>" + reason + ": " + message + "</TITLE></HEAD>");
    out.println("<BODY>");
    out.println("<H1>" + reason + "</H1>");
    out.println("<H2>" + message + "</H2>");
    out.println("<PRE>");
    if (throwable != null) {
        throwable.printStackTrace(out);
    }
    out.println("</PRE>");
    out.println("<HR>");
    out.println("<I>Error accessing " + uri + "</I>");
    out.println("</BODY></HTML>");
}

From source file:net.nan21.dnet.core.web.security.DefaultNotAuthenticatedEntryPoint.java

@Override
public void commence(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException authException) throws IOException, ServletException {

    response.setStatus(HttpServletResponse.SC_FORBIDDEN);
    response.getWriter().write("Not authenticated");
    response.flushBuffer();//from  ww  w .  j  a  v a  2 s.  c  o m

}

From source file:edu.lafayette.metadb.web.dataman.ShowExport.java

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*//*from w w  w. j a v a 2 s .com*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    // TODO Auto-generated method stub
    PrintWriter out = response.getWriter();
    JSONObject output = new JSONObject();
    try {
        String projname = (String) request.getSession(false).getAttribute(Global.SESSION_PROJECT);
        int max = ItemsDAO.nextItemNumber(projname) - 1;
        output.put("max", max);
        output.put("projname", projname);
    } catch (Exception e) {
        MetaDbHelper.logEvent(e);
    }
    out.print(output);
    out.close();
}