Reading:  

Introduction to Servlet technology


Server Response

Some of the following method of HTTP Response header are: 

Method & Description

String encodeRedirectURL(String url)


Encodes specified URL for redirect

String encodeURL(String url)

Encodes specified URL by including session ID in it.

boolean containsHeader(String name)


Returns a boolean indicating whether named response header has already been set.

boolean isCommitted()


Return a boolean if response has been committed.

void addCookie(Cookie cookie)


Add cookie specified to response.

void addDateHeader(String name, long date)


Add a response header with given name and date-value.

void addHeader(String name, String value)


Adds a response header with given name and value.

void addIntHeader(String name, int value)


Adds a response header with given name and integer value.

void flushBuffer()


Forces any content in buffer to be written to client.

void reset()


Clears any data that exists in buffer along with status code and headers.

void resetBuffer()


Clears content of underlying buffer in response without clearing headers or status code.

void sendError(int sc)


Sends an error response to client using specified status code and clearing buffer.

void sendError(int sc, String msg)


Sends an error response to client using specified status.

void sendRedirect(String location)


Sends a temporary redirect response to client using specified redirect location URL.

void setBufferSize(int size)


Sets the preferred buffer size for body of response.

void setCharacterEncoding(String charset)


Sets character encoding (MIME charset) of response being sent to client

void setContentLength(int len)


Sets length of content body in response 

void setContentType(String type)


Sets content type of response being sent to client

void setDateHeader(String name, long date)


Sets a response header with given name and date-value.

void setHeader(String name, String value)


Sets a response header with given name and value.

void setIntHeader(String name, int value)


Sets a response header with given name and integer value.

void setLocale(Locale loc)


Sets locale of response.

void setStatus(int sc)


Sets status code for this response.

More information can be found on tomcat's site. Here is the link

HTTP Header Response Example

The below is the HTTP header response example, which will return current time every 15 seconds with auto refresh.

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RefreshServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		refreshTask(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {
		refreshTask(request, response);
	}

	private void refreshTask(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {
		response.setContentType("text/html");
		response.addHeader("Refresh", "10");
		PrintWriter out = response.getWriter();
		out.println(new Date());
	}

}
 

Result we get is a date shown in a browser window, browser refreshes ever every 10 seconds thus the date shown will be updated too.

 In next part of this guide we will dive into HTTP status codes.

Description

This guide introduces Servlet technology and we will cover below topics

  1. What is a Servlet?
  2. Initial Setup
  3. Life Cycle
  4. Various Examples
  5. Client Request
  6. Server Response
  7. Http Status Codes
  8. Writing Filters
  9. Exception Handling
  10. Cookies Handling
  11. Session Tracking
  12. Database Access
  13. Packaging
  14. Internationalization

This is to the point introduction to the topic to get you started

 



Prerequisites

Working knowledge of Core Java is essential

Audience

Beginner to Java Servlet technology or students looking to brush up their skills quickly

Learning Objectives

Learn Java Servlets

Author: Subject Coach
Added on: 2nd May 2015

You must be logged in as Student to ask a Question.

None just yet!