JSF Newbie:Cannot find FacesContext

I just start learning JSF and having problem with the installation part.

I try to run the JSF example which I downloaded from http://java.sun.com/j2ee/javaserverfaces/download.html. I copy the jsf-guessnumber.war to /webapps directory under my tomcat.

when I try to access http://localhost:81/jsf-guessNumber/index.jsp, I got the following error.

org.apache.jasper.JasperException: Cannot find FacesContext

org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:512)

org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)

org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)

org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)

javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

what is the problem? Do I have to configure my tomcat server?

[900 byte] By [monkeya] at [2007-11-27 3:31:03]
# 1
You need to invoke the FacesServlet to activate the FacesContext.Look in the servlet-mapping of the FacesServlet in the web.xml. If the url-pattern of the FacesServlet is something like *.jsf, then you have to invoke the page as /index.jsf instead of index.jsp.
BalusCa at 2007-7-12 8:34:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

I've changed servlet-mapping of my web.xml to

<servlet-mapping>

<servlet-name>Faces Servlet</servlet-name>

<url-pattern>/index.jsf</url-pattern>

</servlet-mapping>

and this is the content of my index.jsp( as a testing page)

<%@page contentType="text/html"%>

<%@page pageEncoding="UTF-8"%>

<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>

<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>JSP Page</title>

</head>

<body>

<body>

<f:view>

<h1><h:outputText value="JavaServer Faces" /></h1>

</f:view>

</body>

</body>

</html>

And I get the following error

org.apache.jasper.JasperException: Exception in JSP: /index.jsp:17

14:</head>

15:<body>

16: <body>

17: <f:view>

18: <h1><h:outputText value="JavaServer Faces" /></h1>

19: </f:view>

20:</body>

Stacktrace:

org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:506)

org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)

org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)

org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)

javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

root cause

javax.servlet.ServletException: Cannot find FacesContext

org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:843)

org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:776)

org.apache.jsp.index_jsp._jspService(index_jsp.java:83)

org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)

javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:334)

org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)

org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)

javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

root cause

javax.servlet.jsp.JspException: Cannot find FacesContext

what is the problem?

Thank You,

monkeya at 2007-7-12 8:34:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Likely you don't understand the servlet-mapping. I recommend you to read thoroughout the Java EE tutorial here http://java.sun.com/javaee/5/docs/tutorial/doc/ to get more insights in the material. Servlets starts at chapter 3.

You need to specify an URL match -not a full page- in the url-pattern. For example<url-pattern>*.jsf</url-pattern>

Then every page which is called with the *.jsf extension will be passed through the FacesServlet, which on it's turn creates the FacesContext.

BalusCa at 2007-7-12 8:34:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

(when using Sun JSF 1.1 implementation)

<context-param>

<param-name>javax.faces.CONFIG_FILES</param-name>

<param-value>/WEB-INF/my-faces-config.xml, /WEB-INF/your-faces-config.xml</param-value>

</context-param>

<context-param>

<param-name>com.sun.faces.verifyObjects</param-name>

<param-value>false</param-value>

</context-param>

<context-param>

<param-name>com.sun.faces.validateXml</param-name>

<param-value>true</param-value>

</context-param>

<context-param>

<param-name>javax.faces.STATE_SAVING_METHOD</param-name>

<param-value>server</param-value>

</context-param>

<servlet>

<servlet-name>Faces Servlet</servlet-name>

<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>

<init-param>

<param-name>config</param-name>

<param-value>/WEB-INF/faces-config.xml</param-value>

</init-param>

<init-param>

<param-name>debug</param-name>

<param-value>3</param-value>

</init-param>

<init-param>

<param-name>detail</param-name>

<param-value>3</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>Faces Servlet</servlet-name>

<url-pattern>*.jsf</url-pattern>

</servlet-mapping>

well to my knowledge you need to configure the discussed parameters too which might be critical @ times..

RahulSharnaa at 2007-7-12 8:34:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Critical? Many of those aren't required. Here is the full web.xml which is sufficient to run a basic implementation of JSF.<?xml version="1.0" encoding="UTF-8" ?>

<web-app

xmlns="http://java.sun.com/xml/ns/j2ee" version="2.4"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<servlet>

<servlet-name>FacesServlet</servlet-name>

<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>FacesServlet</servlet-name>

<url-pattern>*.jsf</url-pattern>

</servlet-mapping>

</web-app>

BalusCa at 2007-7-12 8:34:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

I dont know what is the problem,

but I think, something wrong with my servlet-mapping, both of the following not working for me..

<servlet-mapping>

<servlet-name>Faces Servlet</servlet-name>

<url-pattern>*.jsf</url-pattern>

</servlet-mapping>

and

<servlet-mapping>

<servlet-name>Faces Servlet</servlet-name>

<url-pattern>/index.jsf</url-pattern>

</servlet-mapping>

I use tomcat 5 and jsf1.1.

Do I need to copy any thing to my tomcat directory?

eg. jar file?

monkeya at 2007-7-12 8:34:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

<?xml version="1.0" encoding="UTF-8" ?>

<web-app

xmlns="http://java.sun.com/xml/ns/j2ee" version="2.4"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<servlet>

<servlet-name>FacesServlet</servlet-name>

<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>FacesServlet</servlet-name>

<url-pattern>*.jsf</url-pattern>

</servlet-mapping>

</web-app>

I've try to to copy the coding above and paste it to my web.xml but I still get the same error..

Thanks,

monkeya at 2007-7-12 8:34:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8

I've already solved the problem, the problem was because of the tomcat server that I use, previously I use Apache Tomcat 5(MSI package),

I try to download jakarta-tomcat.zip and unzip it to my C:\ drive, and I try to run jsf page and it's working.

Thanks to all of your replies.

:-)

monkeya at 2007-7-12 8:34:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...