Nearby lessons

10 of 34

Servlet - ServletResponse (MIME Types)

📌 What You Will Learn
  • 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 TypeResponse
text/htmlHTML text
application/pdfPDF file
application/mswordWord document
application/vnd.ms-excelExcel file
image/jpegJPEG image
video/mp4MP4 video

Setting content type — 2 ways:

  • By ServletResponse: resp.setContentType("text/html");
  • By HttpServletResponse: resp.setHeader("content-type", "text/html");

Demo: HTML Response

Example02
Loading editor…

Demo: Excel File as Response

Example03
Loading editor…

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.

Example04
Loading editor…

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

Example06
Loading editor…

BinaryStreamDemo1Servlet.java (Video)

Folder structure: advapps1GA/ → dhoni.mp4, WEB-INF/ → classes/ → BinaryStreamDemoServlet.class

Example07
Loading editor…

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:

  1. Browser sends a request to servlet-1
  2. 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
  3. By seeing 302 status code, the browser creates a new request and sends it to servlet-2
  4. 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)

Example09
Loading editor…

SecondServlet.java (Redirect Target)

Folder structure: advapps1H/ → WEB-INF/ → classes/ → FirstServlet.class, SecondServlet.class

Example10
Loading editor…

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");

Example12
Loading editor…
📝 Key Takeaways
  • 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

🧠 Test Your Knowledge

3 Questions
Progress: 0 / 3

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.