Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7018175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:00:38+00:00 2026-05-27T23:00:38+00:00

I’m trying to display a PDF report on a webApp, I’ve been following this

  • 0

I’m trying to display a PDF report on a webApp, I’ve been following this tutorial here
and it creates the pdf file fine, but I’m having trouble trying to display it in the browser. In my xhtml I have a button, once that button is clicked, a function calling the servlet is called. it goes into the servlet and created a pdf document fine. but I can’t seem to figure out how to display it on the screen. is there a way to show the document on a new browser window, or new tab? or even the same one.

I’m working with Java Server faces 2.0 in Eclipse. and have a Tomcat 7.0 server.

on my webxml I added the following code specified in the example:

 <servlet>
    <servlet-name>PdfServlet</servlet-name>
    <servlet-class>com.bravo.servlets.PdfServlet</servlet-class>
</servlet>    
<servlet-mapping>
    <servlet-name>PdfServlet</servlet-name>
    <url-pattern>/PdfServlet</url-pattern>
</servlet-mapping>

and my servlet looks like this ( pretty much the same as the example):

@WebServlet("/PdfServlet")
public class PdfServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private Font font = new Font(Font.FontFamily.TIMES_ROMAN, 12,
        Font.NORMAL, BaseColor.RED);



/**
 * Default constructor. 
 */
public PdfServlet() {
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request, response);
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    invokePDFViewer(response);
    Document document = new Document();

    try{
            PdfWriter.getInstance(document, response.getOutputStream()); 
        document.open();

        addContent(document);

        document.close(); 
    }catch(DocumentException e){
        e.printStackTrace();
    }

}

private void invokePDFViewer(HttpServletResponse response){
    response.setContentType("application/pdf"); 
}

private void addContent(Document document)throws DocumentException {
    PdfPTable table = new PdfPTable(2);


    Paragraph paragraph = new Paragraph ("Este es un parrafo en celda 1", font);



    table.addCell(paragraph);
    table.addCell("2");
    table.addCell("3");
    table.addCell("4");
    table.addCell("5");
    table.addCell("6");     

    document.add(table);
}

}

the xhtml I’m calling the servlet from looks like this:

....
function callPdfServlet(){

            $.ajax({
                type:   'POST',
                cache:  'false',
                data:   'codeType=notUsed',
                url:    '/miloWeb/PdfServlet',
                async:  false,
                success: function(data){    
                },
                error: function (xhr, ajaxOptions, thrownError){
                    alert(ajaxOptions);
                }   
            });

        }

.....
<h:commandButton id="reportButton" action=" " styleClass="button" value="get Report" onclick="callPdfServlet();"></h:commandButton>

So at the end, all it does right now is I go into the xhtml in my browser, click on the button, and it hits the servlet, goes through the code, and then thats it. my browser just reloads the screen and nothing else happens. so i need to show the pdf I just created. Thanks in advance for your help!

//************************************************************************************
EDIT 01/02/12:

after reading this and this

I can see that the action in the commandButton will take me to the “response”.xhtml with “response” being a string that I either hardcode in, or is returned by an action in a Managed Bean. that response ( if not put in my faces-config file ) will take me to the page IF it’s in the same folder as my current page.

so I believe that when I put “miloWeb/PdfServlet” as a response for the action, It looks for the page in the same folder ( which it’s not) and since it doesn’t find anything just reloads the page. And since I have a break point In the servlet, I am 100% sure it’s not hitting it.

so my question is: How do I redirect my page to miloWeb/PdfServlet??
to clarify, it works fine if I put the name of another xhtml in the same folder. so it is working that way.

//This is what I tried just for reference:
Instead of going through the ajax call I’ve changed the button to

<h:commandButton id="reportButton" action="/miloWeb/PdfServlet" styleClass="button" value="get Report"></h:commandButton>

but it Just reloads the page and doesn’t take me to the Servlet.

so another thing I tried was tried to go thought the action of the button calling a Managed Bean:

    public String actionPdf(){
    return "/miloWeb/PdfServlet";
}

again,same thing, the function returns the string, but it still doesn’t take me to the servlet.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:00:38+00:00Added an answer on May 27, 2026 at 11:00 pm

    in the action of the commandButton, I had to type this in:

    public String doThis(){   
    String url = "url of your servlet";   
     FacesContext context = FacesContext.getCurrentInstance();   
     try {   
      context.getExternalContext().dispatch(url);   
      }catch (Exception e) {   
           e.printStackTrace();   
    }   
    finally{   
       context.responseComplete();
    
    return "";   
       } 
    

    So with this, I get the context root and redirect it there. url being /PdfServlet

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i want to parse a xhtml file and display in UITableView. what is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.