About HttpServletResponse
You use the Response to send data back to the client.
The most common methods you’ll call on the response object
(HttpServletResponse) are setContentType() and getWriter().
Be careful—many developers assume the method is getPrintWriter(), but
it’s getWriter().
The getWriter() method lets you do character I/O to write HTML (or
something else) to the stream.
You can also use the response to set headers, send errors, and add
cookies.
In the real world, you’ll probably use a JSP to send most HTML
responses, but you may still use a response stream to send binary data
(like a JAR fi le, perhaps) to the client.
The method you call on your response for getting a binary stream is
getOutputStream().
ServletResponse interface
(javax.servlet.ServletResponse)
<<interface>>
ServletResponse
getBufferSize()
setContentType()
getOutputStream()
getWriter()
setContentLength()
// MANY more methods...
The setContentType() method tells the browser how to handle the
data coming in with the response. Typical content types are “text/html”,
“application/pdf”, and “image/jpeg”.
You don’t have to memorize content types (also known as MIME types).
You can set response headers using addHeader() or setHeader().
The difference depends on whether the header is already part of the
response. If it is, setHeader() will replace the value, but addHeader will
add an additional value to the existing response. If the header is not
already part of the response, then setHeader() and addHeader() behave
in exactly the same way.
If you don’t want to respond to a request, you can redirect the request to a
different URL. The browser takes care of sending the new request to the
URL you provide.
To redirect a request, call sendRedirect(aStringURL) on the response.
You cannot call sendRedirect() after the response is committed! In other
words, if you’ve already written something to the stream, it’s too late to do
a redirect.
A request redirect is different from a request dispatch. A request dispatch
(covered more in another chapter) happens on the server, while a redirect
happens on the client. A request dispatch hands the request to another
component on the server, usually within the same web app. A request
redirect simply tells the browser to go a different URL.
The most common methods you’ll call on the response object
(HttpServletResponse) are setContentType() and getWriter().
Be careful—many developers assume the method is getPrintWriter(), but
it’s getWriter().
The getWriter() method lets you do character I/O to write HTML (or
something else) to the stream.
You can also use the response to set headers, send errors, and add
cookies.
In the real world, you’ll probably use a JSP to send most HTML
responses, but you may still use a response stream to send binary data
(like a JAR fi le, perhaps) to the client.
The method you call on your response for getting a binary stream is
getOutputStream().
ServletResponse interface
(javax.servlet.ServletResponse)
<<interface>>
ServletResponse
getBufferSize()
setContentType()
getOutputStream()
getWriter()
setContentLength()
// MANY more methods...
The setContentType() method tells the browser how to handle the
data coming in with the response. Typical content types are “text/html”,
“application/pdf”, and “image/jpeg”.
You don’t have to memorize content types (also known as MIME types).
You can set response headers using addHeader() or setHeader().
The difference depends on whether the header is already part of the
response. If it is, setHeader() will replace the value, but addHeader will
add an additional value to the existing response. If the header is not
already part of the response, then setHeader() and addHeader() behave
in exactly the same way.
If you don’t want to respond to a request, you can redirect the request to a
different URL. The browser takes care of sending the new request to the
URL you provide.
To redirect a request, call sendRedirect(aStringURL) on the response.
You cannot call sendRedirect() after the response is committed! In other
words, if you’ve already written something to the stream, it’s too late to do
a redirect.
A request redirect is different from a request dispatch. A request dispatch
(covered more in another chapter) happens on the server, while a redirect
happens on the client. A request dispatch hands the request to another
component on the server, usually within the same web app. A request
redirect simply tells the browser to go a different URL.