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
andjavax.enterprise.context.SessionScoped
. - Checked whether
AuthBean.class
is inclass
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>