Link to home
Start Free TrialLog in
Avatar of maXXXeE
maXXXeE

asked on

Error : JavaServerFaces : Cannot find FacesContext

I get the following error when deploying web application with java server faces

exception :
javax.servlet.ServletException: Cannot find FacesContext
      org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:830)
      org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:763)
      org.apache.jsp.index1_jsp._jspService(index1_jsp.java:93)
      org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:105)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:860)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:336)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:251)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:860)
      sun.reflect.GeneratedMethodAccessor64.invoke(Unknown Source)
      sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      java.lang.reflect.Method.invoke(Method.java:324)
      org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:249)
      java.security.AccessController.doPrivileged(Native Method)
      javax.security.auth.Subject.doAsPrivileged(Subject.java:500)
      org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:282)
      org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:165)
      java.security.AccessController.doPrivileged(Native Method)
      java.security.AccessController.doPrivileged(Native Method)
      org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:675)
      org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:642)
      org.apache.jsp.index_jsp._jspService(index_jsp.java:46)
      org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:105)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:860)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:336)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:251)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:860)
      sun.reflect.GeneratedMethodAccessor64.invoke(Unknown Source)
      sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      java.lang.reflect.Method.invoke(Method.java:324)
      org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:249)
      java.security.AccessController.doPrivileged(Native Method)
      javax.security.auth.Subject.doAsPrivileged(Subject.java:500)
      org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:282)
      org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:165)

root cause
javax.servlet.jsp.JspException: Cannot find FacesContext
      javax.faces.webapp.UIComponentTag.doStartTag(UIComponentTag.java:405)
      com.sun.faces.taglib.jsf_core.ViewTag.doStartTag(ViewTag.java:105)
      org.apache.jsp.index1_jsp._jspx_meth_cr_view_0(index1_jsp.java:108)
      org.apache.jsp.index1_jsp._jspService(index1_jsp.java:83)
      org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:105)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:860)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:336)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:251)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:860)
      sun.reflect.GeneratedMethodAccessor64.invoke(Unknown Source)
      sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      java.lang.reflect.Method.invoke(Method.java:324)
      org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:249)
      java.security.AccessController.doPrivileged(Native Method)
      javax.security.auth.Subject.doAsPrivileged(Subject.java:500)
      org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:282)
      org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:165)
      java.security.AccessController.doPrivileged(Native Method)
      java.security.AccessController.doPrivileged(Native Method)
      org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:675)
      org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:642)
      org.apache.jsp.index_jsp._jspService(index_jsp.java:46)
      org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:105)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:860)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:336)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:251)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:860)
      sun.reflect.GeneratedMethodAccessor64.invoke(Unknown Source)
      sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      java.lang.reflect.Method.invoke(Method.java:324)
      org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:249)
      java.security.AccessController.doPrivileged(Native Method)
      javax.security.auth.Subject.doAsPrivileged(Subject.java:500)
      org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:282)
      org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:165)
====================================================================
----------------------------------------------------------------------------
                                     faces-config.xml
----------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE faces-config PUBLIC '-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN' ''>
<faces-config>
<navigation-rule>
    <from-view-id>index1.jsp</from-view-id>
    <navigation-case>
        <from-outcome>success</from-outcome>
        <to-view-id>index1.jsp</to-view-id>
    </navigation-case>
</navigation-rule>
<managed-bean>
    <managed-bean-name>Bean</managed-bean-name>
    <managed-bean-class>Package.BeanClass</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
</managed-bean>    
</faces-config>
====================================================================
--------------------------------------------------------------------------------
                             index1.jsp
--------------------------------------------------------------------------------
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<!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>
        <%@taglib uri="http://java.sun.com/jsf/html" prefix="ht"%>
        <%@taglib uri="http://java.sun.com/jsf/core" prefix="cr"%>
    </head>
    <body>

<cr:view>
    <ht:form id="frm">
        <ht:inputText id="in" value="#{Bean.setMethod}" />
        <ht:commandButton value="Retrieve" action="success"/>
    </ht:form>
</cr:view>
    </body>
</html>
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

Are you trying to view index.jsp directly?

Like:

    http://localhost:8080/index1.jsp

?

You can't do that...  You need to do:

    http://localhost:8080/index1.faces

or whatever you have decalerd index1.jsp to be known as...
ASKER CERTIFIED SOLUTION
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of maXXXeE
maXXXeE

ASKER

Hi TimYates
i am not using directly. i do a jsp forward from index.jsp to index1.jsp

i am checking out the link
>>  i do a jsp forward from index.jsp to index1.jsp

Try forwarding to index1.faces instead of index1.jsp (assuming your faces servlet is set up to use the ".faces" extension)

Tim
Avatar of maXXXeE

ASKER

Going by info on ur link, i did this
  <servlet-mapping>
    <servlet-name>FacesServlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
  </servlet-mapping>

but, any method by which i can keep the extension as jsp itself?

 <url-pattern>*</url-pattern> does not work, gives same exception as above
>> but, any method by which i can keep the extension as jsp itself?

Not really...  JSP pages have to go through the JSP servlet (which is defined in tomcat/config/web.xml), wheras jsf pages have to go through the FacesServlet (as defined in your WEB-INF/web.xml)

ACTUALLY!  Thinking about it, you may be able to do:

 <servlet-mapping>
    <servlet-name>FacesServlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
  </servlet-mapping>
 <servlet-mapping>
    <servlet-name>FacesServlet</servlet-name>
    <url-pattern>*.jsp</url-pattern>
  </servlet-mapping>

so that both jsp and jsf files go through the FacesServlet...

However I am not 100% sure this will work...

Hope it does :-)

Tim
Apparently though, Struts won't work with duplicate mappings...

I have no idea if JSF will fail also :-(
Avatar of maXXXeE

ASKER

<servlet-mapping>
    <servlet-name>FacesServlet</servlet-name>
    <url-pattern>*.jsp</url-pattern>
  </servlet-mapping>

tried this already
does not work
Avatar of maXXXeE

ASKER

Thanx for the really fast help

However, if u find a way to the jsp extension problem, please do post it
I think you're going to have to have

  index.jsp forwarding to index1.jsf

then, have index1.jsp as it is now, and

 <servlet-mapping>
    <servlet-name>FacesServlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
  </servlet-mapping>

in WEB-INF/web.xml

I reckon the file has to be a jsp, so that it can get parsed by the JSPServlet, but you have to reference it as .jsf so that the JSFServlet can do its magic first (before it then goes off to the jspservlet)

Tim
:-)

Good luck with it all :-)

I will bookmark this Q incase I ever spot anything, but I reckon the way you have it now is the way it has to be done :-/

Good luck again!

Tim
I was porting an application from jsf/struts/jsf.  I was going out of my mind trying to get the answer, until finally, I tried prefixing my servlets with /faces like so:

    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>faces/index.jsp</welcome-file>
    </welcome-file-list>

For whatever its worth, hope it helps.

Take care.