This discussion is archived
1 2 Previous Next 15 Replies Latest reply: Apr 22, 2012 11:17 AM by 308675 RSS

servlet response not displaying facelet template

308675 Newbie
Currently Being Moderated
Calling a servlet from a facelet template. The text response from the servlet is displaying, but not the facelet template. It seems the facelet template in the servlet response is not being processed. It seems to be ignored.

Is there some particular form or tags needed for facelet template to display in a servlet response?

.xhtml file with template making the servlet call:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"
xmlns:f="http://java.sun.com/jsf/core" 
xmlns:h="http://java.sun.com/jsf/html"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:ejstreegrid="https://unfccc.int/nais/ejstreegrid"
xmlns:grid="http://java.sun.com/jsf/composite/gridcomp"
xmlns:nais="http://java.sun.com/jsf/composite/naiscomp">
<body>
<ui:composition template="/templateForm.xhtml">
<ui:define name="title">Some title</ui:define>
<ui:param name="currentPage" value="somepage.xhtml" />
<ui:define name="body">
<h2>the name to be added</h2><br/><br/>
<form action="someServlet" method="post">
<input type="text" name="someName" />
<input type="submit" />
</form>
</ui:define>
</ui:composition>
</body> 
</html>
the servlet called:
package net.gui.servlet;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SomeServlet extends HttpServlet {
     
private static final long serialVersionUID = 1L;

public SomeServlet() {
  super();
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   processRequest(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  processRequest(request, response);
}

private void processRequest(HttpServletRequest request, HttpServletResponse response) throws IOException {

try {

  logger.debug("start...Other Please Specify two servlet ");
  response.getWriter().write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");     
  response.getWriter().write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
                   "Transitional//EN\" " +               
                   "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
  response.getWriter().write("<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\" " + 
                    "xmlns:f=\"http://java.sun.com/jsf/core\" " + 
                    "xmlns:h=\"http://java.sun.com/jsf/html\" " +
                    "xmlns:c=\"http://java.sun.com/jsp/jstl/core\" " +
                    "xmlns:ui=\"http://java.sun.com/jsf/facelets\" " +
                    "xmlns:ejstreegrid=\"https://unfccc.int/nais/ejstreegrid\" " +
                   "xmlns:grid=\"http://java.sun.com/jsf/composite/gridcomp\" " +
                   "xmlns:some=\"http://java.sun.com/jsf/composite/somecomp\"> ");
  response.getWriter().write("<body> " + 
                             "<ui:composition template=\"/templateForm.xhtml\"> " +
                            "<ui:define name=\"title\">Some Name</ui:define> " +
                 "<ui:param name=\"currentPage\" value=\"somePage.xhtml\" /> " +
                 "<ui:define name=\"body\"> "+
                 "<h2>reply</h2><br/><br/> ");                    
   response.getWriter().write("</ui:define>" +
                                         "</ui:composition> " + 
                                         "</body>" + 
                                         "</html>");               
               
   logger.debug("completed");

   } catch (Exception e) {
     logger.error(e.getMessage());
     e.printStackTrace();
     response.getWriter().println("Error: " + e.getMessage());
  }}}
  • 1. Re: servlet response not displaying facelet template
    gimbal2 Guru
    Currently Being Moderated
    XHTML pages are not processed by JSF just like that; you have to manually configure the Faces servlet in the web.xml to pickup those resources. The Faces servlet will then trigger the view renderer (facelets in this case) to process the page data.

    Your servlet is likely not routed through the Faces servlet, so it won't be processed as a facelets page.
  • 2. Re: servlet response not displaying facelet template
    308675 Newbie
    Currently Being Moderated
    How do I route my servlet through the Faces servlet?

    I have the Faces servlet declared in web.xml:
    <servlet>
         <servlet-name>Faces Servlet</servlet-name>
         <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
         <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
         <servlet-name>Faces Servlet</servlet-name>
         <url-pattern>*.jsf</url-pattern>
     </servlet-mapping>
    also the servlet I am calling:
    <servlet>
        <servlet-name>SomeServlet</servlet-name>
        <servlet-class>net.gui.servlet.SomeServlet</servlet-class>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>SomeServlet</servlet-name>
        <url-pattern>/someServlet</url-pattern>
    </servlet-mapping>
    They are both in web.xml
  • 3. Re: servlet response not displaying facelet template
    gimbal2 Guru
    Currently Being Moderated
    Atlas77 wrote:
    How do I route my servlet through the Faces servlet?
    I don't think that you can really. Witness your web.xml as it is now; all resources ending with .jsf are processed by the faces servlet. And you have another resource (/someServlet) that maps to your own servlet. You could adapt /someServlet to map to the Faces Servlet, but then your own servlet wouldn't be invoked anymore.

    I have a question of my own now that I've had a moment to think about it: why oh why do you want to use a servlet to generate a facelets page in stead of just generating the HTML output directly? The way you want to set it up now is like this:

    - SomeServlet generates XHTML facelets template data (and somehow makes the Faces Servlet pick that up)
    - Faces Servlet invokes facelets
    - facelets generates the final (X)HTML content based on the XHTML JSF/facelets data
    - final (X)HTML content is streamed to the browser

    If you must generate content using a servlet, then don't use JSF. Just generate the content you want to send to the browser directly.
  • 4. Re: servlet response not displaying facelet template
    308675 Newbie
    Currently Being Moderated
    why oh why do you want to use a servlet to generate a facelets page in stead of just generating the HTML output directly?
    How would I generate it directly?
    The way you want to set it up now is like this:

    - SomeServlet generates XHTML facelets template data (and somehow makes the Faces Servlet pick that up)
    - Faces Servlet invokes facelets
    - facelets generates the final (X)HTML content based on the XHTML JSF/facelets data
    - final (X)HTML content is streamed to the browser
    I am using facelets because it simplifies page creation. It is very easy to reuse page headers and footers. I don't have to recreate all the html for every page all over again.
    If you must generate content using a servlet, then don't use JSF. Just generate the content you want to send to the browser directly.
  • 5. Re: servlet response not displaying facelet template
    gimbal2 Guru
    Currently Being Moderated
    Atlas77 wrote:
    How would I generate it directly?
    Like you're already doing :s Right now you generate facelets stuff, don't do that. Just generate whatever you want to display in the browser.
    I am using facelets because it simplifies page creation. It is very easy to reuse page headers and footers.
    Right, so more likely using a servlet is not what you want; why do you need a servlet in stead of just another JSF facelets page? They're already templated and allow dynamic content through the JSF tags.
  • 6. Re: servlet response not displaying facelet template
    308675 Newbie
    Currently Being Moderated
    Like you're already doing :s Right now you generate facelets stuff, don't do that. Just generate whatever you want to display in the browser.
    But I lose the benefit of the backing bean and the easy reuse of page footers and headers.

    >
    I am using facelets because it simplifies page creation. It is very easy to reuse page headers and footers.
    Right, so more likely using a servlet is not what you want; why do you need a servlet in stead of just another JSF facelets page? They're already templated and allow dynamic content through the JSF tags.
    How would I call another jsf facelets page instead of a servlet? How would I set up the new jsf facelets page so I don't need the servlet anymore?

    Edited by: Atlas77 on Apr 17, 2012 7:08 AM
  • 7. Re: servlet response not displaying facelet template
    gimbal2 Guru
    Currently Being Moderated
    Atlas77 wrote:
    How would I call another jsf facelets page instead of a servlet? How would I set up the new jsf facelets page so I don't need the servlet anymore?
    Yes now we come to the root of the problem.

    Answer: by reading a good book about JSF and actually learning how to properly use it. This stuff isn't easy, you're not going to learn it by peppering forums with questions and trying to piece it together from stuff you find on the net.
  • 8. Re: servlet response not displaying facelet template
    308675 Newbie
    Currently Being Moderated
    gimbal2 wrote:
    Atlas77 wrote:
    How would I call another jsf facelets page instead of a servlet? How would I set up the new jsf facelets page so I don't need the servlet anymore?
    Yes now we come to the root of the problem.

    Answer: by reading a good book about JSF and actually learning how to properly use it. This stuff isn't easy, you're not going to learn it by peppering forums with questions and trying to piece it together from stuff you find on the net.
    I've two years experience working with jsf. I have read several books on the subject, but this is my first experience with facelets. I find it to have significantly unique properties that differentiates it from working with 'simple' jsf.

    Can you at least give me a clue or hint how I can set up a new jsf facelets page that would replace a servlet?
  • 9. Re: servlet response not displaying facelet template
    EJP Guru
    Currently Being Moderated
    But I lose the benefit of the backing bean and the easy reuse of page footers and headers.
    But you don't have the benefit of the backing bean in the first place. You aren't talking to the JSF/Facelets servlet, you are talking to your own servlet. Your servlet writes directly to the browser.
  • 10. Re: servlet response not displaying facelet template
    308675 Newbie
    Currently Being Moderated
    EJP wrote:
    But I lose the benefit of the backing bean and the easy reuse of page footers and headers.
    But you don't have the benefit of the backing bean in the first place. You aren't talking to the JSF/Facelets servlet, you are talking to your own servlet. Your servlet writes directly to the browser.
    I see. That explains the behavior I am seeing. So the use of Servlets and Facelets are not compatible.

    Edited by: Atlas77 on Apr 18, 2012 4:14 AM
  • 11. Re: servlet response not displaying facelet template
    EJP Guru
    Currently Being Moderated
    So the use of Servlets and Facelets are not compatible.
    That's putting it much too strongly. Writing Facelets code directly to the output stream in a servlet isn't 'compatible' with the browser, as the browser doesn't understand Facelets code itself. It certainly isn't JSF's fault, or Facelets', that you are bypassing it completely, and it isn't any kind of an incompatibility involving them.
  • 12. Re: servlet response not displaying facelet template
    gimbal2 Guru
    Currently Being Moderated
    EJP wrote:
    So the use of Servlets and Facelets are not compatible.
    That's putting it much too strongly. Writing Facelets code directly to the output stream in a servlet isn't 'compatible' with the browser, as the browser doesn't understand Facelets code itself. It certainly isn't JSF's fault, or Facelets', that you are bypassing it completely, and it isn't any kind of an incompatibility involving them.
    Summarized: you are doing it wrong. Don't blame the technology.
  • 13. Re: servlet response not displaying facelet template
    308675 Newbie
    Currently Being Moderated
    :-) I'm not blaming the technology. I'm just better understanding the relationship between facelets and servlets.
  • 14. Re: servlet response not displaying facelet template
    gimbal2 Guru
    Currently Being Moderated
    Well lets start with a simple question then - do you even know what facelets is?

    I already know that the answer is no or you wouldn't be asking these questions. It is more important that you yourself start to see that, and then perhaps you start to ask yourself: why am I doing things without understanding them? And you don't understand it, or you would yourself realize that sending Facelets data to the browser in a servlet is not going to do what you need it to be doing. You wouldn't have needed to get an explanation that there is such a thing as a faces servlet.
1 2 Previous Next

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points