Wednesday, December 20, 2006

Calling Servlets from JSF

Another point I struggled with is calling servlets from JSF. Here is a simple example.

This is the bean which is called from .jsp page
import javax.faces.context.FacesContext;

public class ServletTest {

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

This is the .jsp page

<f:view>
<h:form id="myForm">
<h:commandButton value="Do" action="#{ServletTest.doThis}" />
</h:form>
</f:view>

16 comments:

Alvaro said...

Thanks a lot, your article is very usefully. It's exactly the information that I needed. Sorry for my poor english.

From Mexico Alvaro S.

Unknown said...

hello
I am engineer computer from IRAN
please help me for ,
How call servlet JasperReport from netbeans JSF (vwp) and using Oracle connection in servlet report.
I Know How to create report.
mrakashani@gmail.com

sitaram059 said...

hi hope this post is not too old and dead. I tried everything you said and still could not call the servlet from the JSF page. I used a bean and it is giving a Methodnotfound exception when it is called from command button action. anyway u can help??

kalani Ruwanpathirana said...

@ram: Did you use the code exaclty as it is? This worked for me.

sitaram059 said...

@kalani ya its working now thanx a lot

ManMohan Vyas said...

hello , thank you for your article ...
1) i just started using jsf ... can u please help me in getting ahead by giving some startups .... i searched over internet , their are quite couple of tutotials for basic login form but cannot found more than this .. need to create a complex application on it .... what extra then JSF is needed for that ???

2) and according to my understanding do we need to have servelate ?? many of the people just put their logics inside the besn itself and view part is jsp .... so where the servlet comes into account ????

thank you
manmohan vyas

kalani Ruwanpathirana said...

@ManMohan: I think this is a good JSF tutorial.

plotemyorma said...

Hi! I want to do the opposite thing, begin an application in a servlet, store data in the faces context and jump to a jsf page or a managed bean method, it's this possible?

Ravi Krishna said...

Hi Kalani.

This blog is very helpful.

manu said...

Hi Kalani,
m Software Engineer from pune India. M using your code as its.Also i have done mapping for this bean in faces-config.xml...under .still m getting--- SEVERE: Servlet.service() for servlet Faces Servlet threw exception
java.lang.IllegalStateException: Cannot forward after response has been committed----M new to jsf..

kalani Ruwanpathirana said...

Hi manu,

Can you double check that this piece of code is included in your code, after dispatching the url?

finally{
context.responseComplete();
}

Admin said...
This comment has been removed by the author.
Moloy Bagchi said...

Hi Kalani,
I need your help. I have to display a word document (extracted by querying database tables etc.) from JSF page. I have an existing servlet doing that from JSP page. But we changed the front end from JSP to JSF (and spring framework etc) . Can you give a simple (just like the way you gave servlet/JSF example) example how to call a servlet from JSF that produces a word document? Again I need very simple example so that I can understand the concept.

Thanks again and your response is much appreciated.

Moloy Bagchi

kalani Ruwanpathirana said...

Hi Moloy,

I am not sure I understood your question well, but I guess displaying word doc is done by the servlet right? If so you just need to call that servlet from JSF (as I've posted). Servlet will do the rest (I guess you have it already). I am sorry if that is not what you asked.

Unknown said...

Hi!! I'm trying to do something like this, but i'm a bit lost, i don't get which should be my servlet's url? is the servlet-class? or the url-pattern? thanks a lot

Unknown said...




very useful info, and please keep updating........

Related Posts with Thumbnails