permaculture playing cards
The moose likes JSF and the fly likes How can I use JSF inside a Servlet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "How can I use JSF inside a Servlet?" Watch "How can I use JSF inside a Servlet?" New topic

How can I use JSF inside a Servlet?

Matheus Viana

Joined: Jul 01, 2008
Posts: 2
Hi everyone!
I am working with JSF and servlets. I need to make a servlet that uses JSF tags to generate a web page. I cannot do it using JSP. But the instruction out.println() does not work with JSF tags. I want to do something like:

PrintWriter out = response.getWriter();
out.println( "<hutputText value=\"Some Text\" />" );

But it does not work. The resulting page does not shows anything. I think it is because the resulting page must be only HTML. How can I work with JSF inside the servlet? Is there a way I can work with faces components and then get their resulting HTML code as a String to put it in the out.println()?


There is no success with no hardness.
Darryl Nortje
Ranch Hand

Joined: Jun 11, 2002
Posts: 140
I'm sure it's possible Matheus.

I haven't done it before, but I can't see why it isn't possible. Perhaps you've got something wrong with your FacesServlet mapping in your web.xml file.

Bart Kummel
Ranch Hand

Joined: Nov 30, 2007
Posts: 81
What you want is, ehm, strange. And it's impossible too. In fact a JSF page will generate a Servlet when it's processed on the server side. What you found out is right: a servlet can only generate HTML that is send to the client (the browser) immediately, without further processing.

I suggest that you may be better off when you first read some more about Servlets, JSPs and JSF. Perhaps the chapter "The web tier" from the Sun Java EE 5 Tutorial is a good starting point.

Best regards,
Bart Kummel

SCJP 1.4 | SCJD 1.6 | Visit my website | Author of the book Apache MyFaces 1.2 Web Application Development
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17410

There already is a servlet that uses JSF tags to generate a web page. It's the JSF Servlet.

JSF isn't just a set of taglibraries, it's an entire infrastructure. Before you break the rules, you need to know how they work.

What it looks like you actually really intended to do was dynamically generate JSF page source and then apply it. That's not something I'd do lightly and I've been working with JSF for over 2 years now.

On the other hand, I've never felt the need to. JSF can do an awful lot in a fairly simple way. So simple, in fact, that one of the biggest problems a lot of people seem to have with it is that they try to build complex solutions for simple problems.

An IDE is no substitute for an Intelligent Developer.
I agree. Here's the link:
subject: How can I use JSF inside a Servlet?
It's not a secret anymore!