Nearby lessons
10 of 34Servlet - ServletResponse (MIME Types)
- Set content type using MIME types
- Send HTML, Excel, Word, image, and video responses
- Redirect HTTP requests to another URL
- Add cookies to the response
ServletResponse provides methods to set response content type, write text and binary data, redirect requests, and add cookies. This lesson covers MIME types, response streams, and redirection with complete code examples.
Common MIME Types
MIME types represent the type of response being sent to the end user.
| MIME Type | Response |
|---|---|
text/html | HTML text |
application/pdf | PDF file |
application/msword | Word document |
application/vnd.ms-excel | Excel file |
image/jpeg | JPEG image |
video/mp4 | MP4 video |
Setting content type — 2 ways:
- By ServletResponse:
resp.setContentType("text/html"); - By HttpServletResponse:
resp.setHeader("content-type", "text/html");
Demo: HTML Response
Demo: Excel File as Response
Demo: Word File as Response
Changing the MIME type to application/msword causes the browser to open the response as a Word document instead of rendering it as HTML.
Text Stream vs Binary Stream
Text Stream (PrintWriter):
Used to send text data as response. Obtained via resp.getWriter().
Binary Stream (ServletOutputStream):
Used to send binary data (images, video, audio). Obtained via resp.getOutputStream().
Note: Within a servlet, you can get either PrintWriter or ServletOutputStream, but not both. Trying to get both throws java.lang.IllegalStateException.
BinaryStreamDemo1Servlet.java (Image)
Folder structure: advapps1G/ → sunny.jpg, WEB-INF/ → classes/ → BinaryStreamDemoServlet.class
BinaryStreamDemo1Servlet.java (Video)
Folder structure: advapps1GA/ → dhoni.mp4, WEB-INF/ → classes/ → BinaryStreamDemoServlet.class
Redirecting HTTP Request
Sometimes we need to redirect an HTTP request to another URL. Use sendRedirect() from HttpServletResponse:
public void sendRedirect(String targetPath) throws IOException
Process of Redirection:
- Browser sends a request to servlet-1
- If servlet-1 is not responsible for the response, it updates the browser to contact servlet-2 by setting status code 302 and Location header with servlet-2's URL
- By seeing 302 status code, the browser creates a new request and sends it to servlet-2
- Servlet-2 provides the required response (200) to the browser
Note: After committing the response, redirection is not allowed (throws IllegalStateException). Redirection happens at the client side (browser side).
Real-world example: When you request google.com, the request is redirected to google.co.in.
FirstServlet.java (Redirect)
SecondServlet.java (Redirect Target)
Folder structure: advapps1H/ → WEB-INF/ → classes/ → FirstServlet.class, SecondServlet.class
Adding Cookies to Response
A Cookie is a key-value pair used in session management.
Creating and adding a cookie:
Cookie c = new Cookie("durga", "java");
resp.addCookie(c);
ServletResponse Method Summary
To Set Response Headers:
- setHeader()
- addHeader()
- setIntHeader()
- addIntHeader()
- setDateHeader()
- addDateHeader()
To Set Content-Type:
resp.setContentType("text/html"); OR resp.setHeader("Content-Type", "text/html");
To Acquire Text Stream:
PrintWriter out = resp.getWriter();
To Acquire Binary Stream:
ServletOutputStream os = resp.getOutputStream();
To Redirect Request:
resp.sendRedirect("/advapps1A/test"); OR
resp.setStatus(302); resp.setHeader("Location", "/advapps1A/test");
- Common MIME types and how to set them
- PrintWriter for text, ServletOutputStream for binary
- sendRedirect() sends 302 status + Location header
- Exam-style questions at the end