• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How can I use JSF inside a Servlet?

 
Matheus Viana
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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()?

Thanks!
 
Darryl Nortje
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

cheers
Darryl
 
Bart Kummel
author
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18212
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic