JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy
  • Contact Us

How To Set PDF File Display In Servlet

February 7, 2014 by Krishna Srinivasan Leave a Comment

  • Java EE Tutorials
  • Servlets Tutorials
  • Servlets Interview Questions

In this article we will write a simple program which will write content to a PDF file. Example of writing data into PDF using Servlet. Create a Servlet called PDFDemo.

[code lang=”java”]
package javabeat.net.servlets;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;

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

public class PDFDemo extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
PrintWriter out = response.getWriter();
response.setContentType("application/pdf");
String filepath = "/home/jsp.pdf";
response.setHeader("Content-Disposition", "inline; filename=’jsp.pdf’");
FileOutputStream fileout = new FileOutputStream(filepath);
fileout.close();
out.close();
}

}
[/code]

Explanation:

  • Content-Disposition in response header contains inline disposition type and file name attributes.
  • inline is disposition type. If it is marked “inline” then it should be automatically displayed when the message is displayed.
  • filename is name of the file used when creating the file.
  • ContentType is used to display MIME (Multipurpose Internet Mail Extensions) type. MIME type describes contents of various files.
  • PrintWriter object is used to display the result.
  • FileOutputStream is an output stream which is used to write data to file or file descriptor.

Execute the above program, right mouse click on the class PDFDemo and select Run>Run As, a pdf file with the name jsp.pdf would be created at the specified path in our case it is /home/jsp.pdf.

Previous Tutorial : How To Initialize Variables In Servlet?  || Next Tutorial : 

Filed Under: Java EE Tagged With: Servlets Tutorials

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved