Latest Tutorials| Questions and Answers|Ask Questions?|Site Map

Home Java Itext 'Hello World' file from a servlet (PDF, HTML or RTF).

Related Tutorials


Share on Google+Share on Google+

'Hello World' file from a servlet (PDF, HTML or RTF).

In this section, you will see an example that print hello world file from a servlet(PDF, HTML or RTF).

 'Hello World' file from a servlet (PDF, HTML or RTF).


In this program we are going to tell you how we can create three file rtf,pdf and html files.And we are printing here Hello Word on PDF, HTML, And RTF formate.

To make a program over this, firstly we need to import some packages. Remember to make this program the first and foremost thing to remember is to place the iText.jar in WEB-INF/lib of your web application. Without this .jar the application will not run. The packages we were talking about are* for input output, com.lowagie.text.pdf.*,import java.awt.*;import javax.servlet.*;import javax.servlet.http.*,import com.lowagie.text.*,import com.lowagie.text.rtf.*,import com.lowagie.text.html.*;

Now create a file named helloServletPDF which extends Httpservlet. Remember the name of the file should be such that the reader can easily  understand what the program is going to perform. Inside the class declare a main method inside which we are going to write the logic of our program.

In doGet(HttpServletRequest req,HttpServletResponse res) create Document object  and getParameter  passed by user. Then if parameter passed is equals to "rtf" then  set res.setContentType("text/rtf") and  writer  file on Clint side as with RtfWriter2.getInstance (document,res. getOutputStream()) or if "html" then  set res.setContentType("text/html") and  writer  file on Clint side as with HtmlWriter.getInstance (document,res. getOutputStream()) or if "pdf" then  set res.setContentType("application/pdf") and  writer  file on Clint side as with PdfWriter.getInstance (document,res. getOutputStream()). Finaly close the document.  

The code of the program is given below:

import java.awt.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
import com.lowagie.text.rtf.*;
import com.lowagie.text.html.*;

public class helloServletPDF extends HttpServlet
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {
String str=req.getParameter("Text1");

Document document=new Document();

document.add(new Paragraph("Hello World"));
catch(DocumentException de) 
System.err.println("document: " + de.getMessage());

The code of the program is given below:Index.Html
Example of servlet to display "Hello Word "
on PDF,RTF and HTML format using iText
<center><b>Enter Data Type Format (Only rtf,html or pdf)</b><br>
<form action="../helloServletPDF" method="GET" name="upform">
<input type="text" name="Text1" ><br>
<input type="submit" name="Submit" value="Submit" >


The output of the program is given below:

Download this example.

Download this index.html.


If you enjoyed this post then why not add us on Google+? Add us to your Circles

Liked it!  Share this Tutorial

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 17, 2007

Related Tutorials

Discuss: 'Hello World' file from a servlet (PDF, HTML or RTF).   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
May 28, 2013
how to show output on pdf

how to run servlet in netbeans with this example
March 12, 2013

Wow super explination
February 21, 2012

August 16, 2012

good one