views:

309

answers:

1

Hi,

I am currently reading about Java Server Faces. I wanted to do my first little expriment with it. I out the API and implementation JARs into the tomcat/lib folder. I created a new "Dynamic Web Project" inside Eclipse. I created a new HTML file with the following content:

    <!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"&gt;
<head>
  <title>Hello World</title>
</head>
<body>
  <h:outputText value="Hello JSF 2.0-World"/>
</body>
</html>

I edited the web.xml file. I added a tag for the Faces Servlet and mapped it to the pattern "*.html". Additionally I added the HTML file to the welcome files. This is the web.xml file:

    <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>JSF_Test</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
    <welcome-file>hello.html</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.html</url-pattern>
  </servlet-mapping>
</web-app>

Now I wanted to run and test this example. I addded the project to the Tomcat server within eclipse and started the server. When I try to request the page via browser the server just gives a stacktrace. It looks like a stackoverflow to me.

Why does this example not work and what can I do to fix it?

Thanks in advance.

Here is a part of the stacktrace:

javax.servlet.http.HttpServletRequestWrapper.getSession(HttpServletRequestWrapper.java:216)
javax.servlet.http.HttpServletRequestWrapper.getSession(HttpServletRequestWrapper.java:216)
javax.servlet.http.HttpServletRequestWrapper.getSession(HttpServletRequestWrapper.java:216)
javax.servlet.http.HttpServletRequestWrapper.getSession(HttpServletRequestWrapper.java:216)
javax.servlet.http.HttpServletRequestWrapper.getSession(HttpServletRequestWrapper.java:216)
javax.servlet.http.HttpServletRequestWrapper.getSession(HttpServletRequestWrapper.java:216)
javax.servlet.http.HttpServletRequestWrapper.getSession(HttpServletRequestWrapper.java:216)
com.sun.faces.context.ExternalContextImpl.getSession(ExternalContextImpl.java:151)
javax.faces.application.ViewHandler.calculateCharacterEncoding(ViewHandler.java:242)
javax.faces.application.ViewHandler.initView(ViewHandler.java:458)
com.sun.faces.application.view.MultiViewHandler.initView(MultiViewHandler.java:106)
com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:109)
com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
com.sun.faces.context.ExternalContextImpl.dispatch(ExternalContextImpl.java:542)
com.sun.faces.application.view.JspViewHandlingStrategy.executePageToBuildView(JspViewHandlingStrategy.java:355)
com.sun.faces.application.view.JspViewHandlingStrategy.buildView(JspViewHandlingStrategy.java:130)
com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:106)
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:313)
com.sun.faces.context.ExternalContextImpl.dispatch(ExternalContextImpl.java:542)
com.sun.faces.application.view.JspViewHandlingStrategy.executePageToBuildView(JspViewHandlingStrategy.java:355)
com.sun.faces.application.view.JspViewHandlingStrategy.buildView(JspViewHandlingStrategy.java:130)
com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:106)
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:313)
com.sun.faces.context.ExternalContextImpl.dispatch(ExternalContextImpl.java:542)
com.sun.faces.application.view.JspViewHandlingStrategy.executePageToBuildView(JspViewHandlingStrategy.java:355)
+1  A: 

Okay, the web.xml looks fine. It look like that you're incorrectly using the same .html extension for the actual files as the url-pattern the FacesServlet is mapped on. You shouldn't do that. It causes the FacesServlet to run in an infinite loop calling itself again and again.

Assuming that you're using JSF 2.0 on top of Facelets as view technology, you need to name the actual file with an .xhtml extension, not with an .html extension. The FacesServlet is since JSF 2.0 by default configured to lookup files with the .xhtml extension.

So, to get the JSF 2.0 mole to work:

  1. Name the actual file with a .xhtml extension. E.g. ProjectName/WebContent/filename.xhtml.
  2. Map the FacesServlet on an url-pattern other than .xhtml. Often .jsf is used. You can also choose .html. But this implies that every actual .html file in the webcontent will also be passed through the FacesServlet. You sometimes don't want to have that.
  3. Invoke/access/link the page on the FacesServlet's URL-pattern. E.g. http://localhost:8080/ContextName/filename.jsf.
BalusC
+1 I was writing my answer... I agree, server is confused is this index.html jsf or non-jsf request
cetnar
Thanks for answering. I renamed the hello.html file to to hello.jsf and updated this change in the web.xml. Then I mapped the Faces Servlet to *.jsf. After restarting the container the problem occures again. Seems like nothing changed.I going after this tutorial: http://jsfatwork.irian.at/semistatic/introduction.html [German]. I searched long before I found this. I think the introduction is very good. Many other tutorials are not up to date, work in progress, bad quality...
c0d3x
@c0d3x Try this tutorial from javapasion - http://www.javapassion.com/j2ee/#JavaServer_Faces_JSF_title
cetnar
Thanks. I will read it. And I fixed the problem now. I used "hello.html" and "*.html" in the turorial they used "hello.xhtml" and "*.xhtml". Now I'm using xhtml, too and it works. Why is that?
c0d3x
Oh, you're using Facelets. I didn't expect that it would pass through the JSP view handler then. At any way, the best Facelets tutorial is their own developer guide: https://facelets.dev.java.net/nonav/docs/dev/docbook.html and of course Sun's own Java EE 6 tutorial: http://java.sun.com/javaee/6/docs/tutorial/doc/bnaph.html
BalusC
OK, thanks. I think I can take the next steps on my own again. I will come back if I have another question.
c0d3x