Responses
import javax.servlet.http.*;
import java.io.*;
// Servlets that shows the current time
public class TimeServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
response.setContentType("text/html; charset=UTF-8"); // required: HTML in UTF-8 (more)
response.setCharacterEncoding("UTF-8"); // recommended: use UTF-8 (more)
PrintWriter writer = response.getWriter(); // writer for sending text (more)
writer.print("<html><head><title>Time</title></head><body>");
writer.print("The current time is: ");
writer.print((new java.util.Date()).toString());
writer.print("</body></html>");
}
}
import java.io.*;
import java.util.*;
import javax.servlet.http.*;
// Servlet that sends one of two PNG images, depending on time of day
public class AmPmFileServlet extends HttpServlet {
// simple helper: copy data from InputStream to OutputStream
private void copy(InputStream in, OutputStream out)
throws IOException {
byte buffer[] = new byte[4096];
while (true) {
int r = in.read(buffer);
if (r < 0) // end of stream?
break;
out.write(buffer, 0, r);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
// before or after noon? determine path to the right image in the WAR
boolean isPM = new GregorianCalendar().get(Calendar.AM_PM) == Calendar.PM;
String resName = "/WEB-INF/images/time" + (isPM ? "PM" : "AM") + ".png";
response.setContentType("image/png"); // required: content type PNG
OutputStream out = response.getOutputStream(); // stream for binary files (more)
InputStream in = getServletContext().getResourceAsStream(resName);
copy(in, out); // copy file to client
}
}
Buffering and Committing
As long as you set the Content Type and other headers before writing the actual data, your servlet will always work fine. However, the Servlet API allows you a bit more flexibility when you understand its buffering mechanism:
- The web container buffers all data that you write to the client
- Data will be sent to the client when ...
- the buffer's capacity is exceeded, or
- you call flushBuffer, or
- the amount of bytes specified with setContentLength has been written, or
- the OutputStream or PrintWriter has been closed, or
- the service method (doGet, doPost etc) has finished.
- Use getBufferSize to get the buffer size.
- You may set the buffer size with setBufferSize, but only before getting the stream/writer.
- The response is committed as soon as data has been sent to the client.
- Find out whether the response is committed using isCommitted.
- The following things are only allowed before the response is committed:
- setting headers (more)
- clearing the buffer with resetBuffer (more)
- clearing buffer and headers with reset (more)
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
// send status code 403 (more):
response.sendError(HttpServletResponse.SC_FORBIDDEN, "Access not allowed");
// Important: return from service method immediately (more)
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
response.sendRedirect("http://jarfiller.com/");
// Important: return from service method immediately (more)
}

