0

At first, I want to let you know that I have been browsing through the same issues here on SO for a long time but nothing helped at all. I have project where I want to have AuthBean to handle authentication, however I keep getting HTTP 500 error with the known error message

javax.servlet.ServletException: /login.xhtml @14,45 value="#{authBean.login}": Target Unreachable, identifier 'authBean' resolved to null

I am using GlassFish 4 server as Java EE runtime environment.

So far, I have tried

  • Restarting GlassFish server, removing the application from the server and redeploy.
  • Switching between javax.faces.bean.SessionScoped and javax.enterprise.context.SessionScoped.
  • Checked whether AuthBean.class is in class folder.
  • Explicitly naming my bean (as you can see below)

Here are my files. Any help is appreciated.

AuthBean.java

package pis.back;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import pis.data.Person;
import pis.service.PersonManager;

@ManagedBean(name = "authBean")
@SessionScoped
public class AuthBean {
    private boolean loggedIn;
    private String login;
    private String password;
    @EJB
    private PersonManager personMgr;

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    /*public boolean isLoggedIn() {
        return this.loggedIn;
    }*/

    public String performLogin() {
        if (loggedIn) {
            return "error";
        }

        Person person = personMgr.findByLogin(this.login);
        if (person == null || !this.password.equals(person.getPassword())) {
            return "error";
        }

        loggedIn = true;
        return "ok";
    }

    public String performLogout() {
        if (!loggedIn) {
            return "error";
        }

        loggedIn = false;
        return "ok";
    }
}

faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<faces-config
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
    version="2.2">

</faces-config>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>pis</display-name>
  <welcome-file-list>
    <welcome-file>index.xhtml</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.xhtml</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <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>*.xhtml</url-pattern>
  </servlet-mapping>
  <filter>
    <filter-name>AuthFilter</filter-name>
    <filter-class>pis.back.AuthFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>AuthFilter</filter-name>
    <url-pattern>*.xhtml</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
  </filter-mapping>
</web-app>

glassfish-web.xml

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

<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<glassfish-web-app>
    <context-root>/pis</context-root>
</glassfish-web-app>

login.xhtml

<?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"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core">

<ui:composition template="template.xhtml">
    <ui:define name="title">Login</ui:define>
    <ui:define name="content">
        <h:form>
            <h:panelGrid columns="2">
                <h:outputLabel value="Login: "/>
                <h:inputText value="#{authBean.login}"/>
                <h:outputLabel value="Password: "/>
                <h:inputSecret value="#{authBean.password}"/>
                <h:commandButton action="#{authBean.performLogin}" value="Login"/>
            </h:panelGrid>
        </h:form>
    </ui:define>
</ui:composition>
</html>

template.xhtml

<?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"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">
<h:head>
    <title><ui:insert name="title"/></title>
</h:head>

<h:body>
    <div id="header">
        Here comes header
    </div>
    <div id="menu">
        Here comes menu
    </div>
    <div id="content">
        <h2><ui:insert name="title"/></h2>
        <ui:insert name="content"/>
    </div>
    <div id="footer">
        Here comes footer
    </div>
</h:body>
</html>
Marek Milkovič
  • 382
  • 1
  • 5
  • 18
  • When you build/export the WAR, does it look all fine? See also point 1c of http://stackoverflow.com/q/30128395 – BalusC Apr 21 '16 at 18:51
  • I have seen this ticket and checked the content of WAR. `AuthBean.class` resides in `WEB-INF/classes/pis/back` and I guess it should be that way. – Marek Milkovič Apr 21 '16 at 18:57
  • And `WEB-INF/lib` is free of server-provided libraries? (Java EE, JSF, etc) – BalusC Apr 21 '16 at 18:57
  • `WEB-INF/lib` contains just `primefaces-5.3.jar`. I can try it without this library. I haven't even used it yet. – Marek Milkovič Apr 21 '16 at 18:59
  • PrimeFaces isn't provided by GF, so shouldn't be a problem. Which GF4 version exactly is it? First 4.0 release is full of childhood diseases and you'd better pick the current 4.1.1 (or even Payara). Have you tried using `@Named` instead of `@ManagedBean`? – BalusC Apr 21 '16 at 19:00
  • It is GF 4.1 and the reason I use JSF is because it is university project and it is stated in specification that we have to use it. So I have not tried `@Named` and I would better not. There must be some way how to make this work even with JSF. – Marek Milkovič Apr 21 '16 at 19:04
  • Problem is not visible in the information provided so far. Any oddities in server logs? Have you tried a clean GF? – BalusC Apr 21 '16 at 19:05
  • There are some warnings -- `Warning: Ignoring WEB-INF/ because the containing archive PATH_TO_GLASSFISH\glassfish-4.1\glassfish\domains\domain3\applications\pis recorded it as a pre-existing stale file`. I'll try clean GF server and let you know. – Marek Milkovič Apr 21 '16 at 19:08
  • 1
    Ah GF stale files. Certainly clean its work folders. If necessary throw away it and unzip afresh. – BalusC Apr 21 '16 at 19:09
  • It's magical. I removed glassfish and unpacked it from ZIP again and it works. I don't understand it. Thanks man. – Marek Milkovič Apr 21 '16 at 19:36
  • 1
    Well, that was covered by *"Make sure that you've properly performed a full clean, rebuild, redeploy and restart of the project and server"* phrase in 1c. I guess you was using a poor IDE server plugin (GF Eclipse plugin is known to have issues with properly cleaning the GF work folders) – BalusC Apr 21 '16 at 20:09

1 Answers1

0

As BalusC have advised I completely reinstalled GlassFish server and it started to work again.

Marek Milkovič
  • 382
  • 1
  • 5
  • 18