Programming Tutorials

The Response Object in JSP

By: Sathya Narayana in JSP Tutorials on 2010-10-24  

The response object in JSP represents the HTTP response that is sent back to the client browser by the web server. It is used to set response headers, status codes, and send content to the client.

Some of the commonly used methods of the response object in JSP are:

  1. setContentType(String type): Sets the MIME type of the content being sent to the client.

  2. getWriter(): Returns a PrintWriter object that can be used to send character data to the client.

  3. sendRedirect(String location): Redirects the client to a new URL.

  4. setStatus(int sc): Sets the status code of the response.

  5. addHeader(String name, String value): Adds a new header to the response.

Example:

<%@ page contentType="text/html; charset=UTF-8" %>
<%
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<head>");
    out.println("<title>Hello World</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<h1>Hello World</h1>");
    out.println("</body>");
    out.println("</html>");
%>

In this example, the response object is used to set the content type of the response to "text/html" and to send HTML content to the client using a PrintWriter object obtained from the response.






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in JSP )

Latest Articles (in JSP)