Example usage for javax.servlet.http HttpServletResponse setContentType

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

Introduction

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

Prototype


public void setContentType(String type);

Source Link

Document

Sets the content type of the response being sent to the client, if the response has not been committed yet.

Usage

From source file:org.openmrs.module.hospitalcore.web.controller.patientsearch.PatientSearchController.java

@RequestMapping(value = "/module/hospitalcore/getPatientResultCount.form", method = RequestMethod.POST)
public void getPatientResultCount(@RequestParam("query") String query, HttpServletResponse response)
        throws IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    BigInteger count = Context.getService(HospitalCoreService.class).getPatientSearchResultCount(query);
    out.print(count);//  w w w .  ja v  a2s. c  o  m
}

From source file:CompileServlet.java

/**
 * Main servlet entry point. The servlet reads a stylesheet from the
 * URI specified by the "sheet" parameter. The compiled Java class
 * ends up in the CWD of the web server (a better solution would be
 * to have an environment variable point to a translet directory).
 *///from   w w  w  .j  av  a 2  s .co  m
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    String stylesheetName = request.getParameter("sheet");

    out.println("<html><head>");
    out.println("<title>Servlet Stylesheet Compilation</title>");
    out.println("</head><body>");

    if (stylesheetName == null) {
        out.println("<h1>Compilation error</h1>");
        out.println("The parameter <b><tt>sheet</tt></b> " + "must be specified");
    } else {
        XSLTC xsltc = new XSLTC();

        xsltc.init();
        xsltc.compile(new URL(stylesheetName));
        out.println("<h1>Compilation successful</h1>");
        out.println("The stylesheet was compiled into the translet " + "class " + xsltc.getClassName()
                + " and is now " + "available for transformations on this server.");
    }
    out.println("</body></html>");
}

From source file:FormServlet.java

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

    response.setContentType("text/html");
    java.io.PrintWriter out = response.getWriter();
    out.println("<html><head>");

    RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/validate.js");

    dispatcher.include(request, response);

    out.println("<title>Help Page</title></head><body>");
    out.println("<h2>Please submit your information</h2>");

    out.println("<form action =\"" + request.getContextPath()
            + "/displayHeaders.jsp\" onSubmit=\" return validate(this)\">");

    out.println("<table border=\"0\"><tr><td valign=\"top\">");
    out.println("Your name: </td>  <td valign=\"top\">");
    out.println("<input type=\"text\" name=\"username\" size=\"20\">");
    out.println("</td></tr><tr><td valign=\"top\">");
    out.println("Your email: </td>  <td valign=\"top\">");
    out.println("<input type=\"text\" name=\"email\" size=\"20\">");
    out.println("</td></tr><tr><td valign=\"top\">");

    out.println("<input type=\"submit\" value=\"Submit Info\"></td></tr>");
    out.println("</table></form>");
    out.println("</body></html>");
    out.close();//  w  w w  .  j a v  a 2s. c  o  m
}

From source file:com.netease.channel.controller.BaseController.java

protected void successRes(HttpServletResponse respose) {
    respose.setContentType(JSON_CONTENT_TYPE);
    try {/*www  .j a v  a  2  s.  co  m*/
        respose.getWriter().write(SUC_RES);
        respose.getWriter().close();
    } catch (IOException e) {
        LOG.info("Controller response error");
    }
}

From source file:com.netease.channel.controller.BaseController.java

protected void errorRes(HttpServletResponse respose) {
    respose.setContentType(JSON_CONTENT_TYPE);
    try {/*from w ww .ja v a2 s . c  om*/
        respose.getWriter().write(FAIL_RES);
        respose.getWriter().close();
    } catch (IOException e) {
        LOG.info("Controller response error");
    }
}

From source file:com.android.get.AndroidJSONMethod.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {/*from w ww .j  a  v a  2s. c  o  m*/
        String fn = request.getParameter("un");
        String pw = request.getParameter("pw");
        JSONObject jo = new JSONObject();
        if (fn.equals("abc") && pw.equals("123")) {

            jo.put("fname", "Lakmal");
            jo.put("lname", "navarathne");

        }
        out.write(jo.toJSONString());
    } finally {
        out.close();
    }
}

From source file:com.jfinal.weixin.sdk.jfinal.MsgInterceptor.java

public void writeText(HttpServletResponse response, String text) {
    response.setContentType("text/x-json;charset=utf-8");
    response.setHeader("Cache-Control", "no-store, max-age=0, no-cache, must-revalidate");
    response.addHeader("Cache-Control", "post-check=0, pre-check=0");
    response.setHeader("Pragma", "no-cache");
    try {/*  w  w  w .  ja v  a2s.c  om*/
        PrintWriter out = response.getWriter();
        out.write(text);
        out.close();
    } catch (IOException e) {
        logger.error("error write json data", e);
    }
}

From source file:MyServlet.java

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

    ServletOutputStream out = res.getOutputStream();
    res.setContentType("text/plain");

    String file = req.getPathInfo();
    if (file == null) {
        out.println("Extra path info was null; should be a resource to view");
        return;//w ww.ja v  a2  s. com
    }

    URL url = getServletContext().getResource(file);
    if (url == null) {
        out.println("Resource " + file + " not found");
        return;
    }

    URLConnection con = null;
    try {
        con = url.openConnection();
        con.connect();
    } catch (IOException e) {
        out.println("Resource " + file + " could not be read: " + e.getMessage());
        return;
    }
}

From source file:CounterRewrite.java

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    HttpSession session = req.getSession(true);
    resp.setContentType("text/html");
    PrintWriter out = resp.getWriter();
    int count = 1;
    Integer i = (Integer) session.getAttribute(COUNTER_KEY);
    if (i != null) {
        count = i.intValue() + 1;/*ww w  .j a v a2 s  .com*/
    }
    session.setAttribute(COUNTER_KEY, new Integer(count));
    out.println("<html>");
    out.println("<head>");
    out.println("<title>Session Counter</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("Your session ID is <b>" + session.getId());
    out.println("</b> and you have hit this page <b>" + count + "</b> time(s) during this browser session");

    String url = req.getRequestURI();
    out.println("<form method=GET action=\"" + resp.encodeURL(url) + "\">");
    out.println("<input type=submit " + "value=\"Hit page again\">");
    out.println("</form>");
    out.println("</body>");
    out.println("</html>");
    out.flush();
}

From source file:DateServlet.java

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

    HttpSession hs = request.getSession(true);

    response.setContentType("text/html");
    PrintWriter pw = response.getWriter();
    pw.print("<B>");

    Date date = (Date) hs.getAttribute("date");
    if (date != null) {
        pw.print("Last access: " + date + "<br>");
    }//from   w ww  .ja va  2s  .c o  m

    date = new Date();
    hs.setAttribute("date", date);
    pw.println("Current date: " + date);
}