Win a copy of Emmy in the Key of Code this week in the General Computing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

How to call a servlet from JSF without using dispatch(url)

Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am working on a task where i have to generate and display reports in the same page when the user clicks Run Report commandButton.

In a servlet i wrote all the jasper coding to generate the report.
To call sevlet from my jsf page i used a method called callMyServlet().
In callServlet method i have used context.getExternalContext().dispatch(url) to call my servlet.
In my JSF page i called callMyServlet method in action of a commandButton
This generates the report but not in the same page.

I would like to know how to call my servlet from jsf without using dispatch url

Code of servlet

Code of callMyServlet Method

JSF Code of commandButton

I have mapped my servlet in web.xml

Please help me out how to get my task done.
any sample code will also help.

Saloon Keeper
Posts: 21240
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"JSF" isn't a control program - it's a way of managing web pages. When you request a JSF page, you're requesting a document that's been designed according to JSF standards that will be converted to HTML via the JSF servlet. On the inbound end, the JSF servlet recieves data from that HTML page and uses it to dispatch the next page view, once again using the JSF rules and resources.

If you want a report to display on a JSF page, you have to use JSF resources to ensure that the page is managed correctly. Furthermore, servlets aren't really intended to be "called". The normal process to get a servlet to get another servlet to do work for it is to forward the request. You could then forward the results of that to the JSF servlet, but considering all the side aspects, you'd probably lose important things in the process. It's usually better to simply have the reporting servlet be a reporting servlet and just place it alongside the JSF stuff the same way you'd do with non-JSF stuff.

In other words, JSF isn't all-or-nothing. Your life will be simpler if your webapp only uses JSF functionality on the aspects of the app that work best on JSF. For the most part, things like servlet-generated reports should just be themselves and not try to be JSF.
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I'm sorry for "revive" this topic... but I'd like to know a example of using Jasper Reports and JSF in that way cited?

I'm having a lot of errors using "context.getExternalContext().dispatch(..." in a Managed Bean filter that call a report.

Best regards!
If you settle for what they are giving you, you deserve what you get. Fight for this tiny ad!
Java file APIs (DOC, XLS, PDF, and many more)
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!