0

I am new to JSF and I done a simple login application using JNDI with MySQL database and below is my coding.I am using netbeans and tomcat 7 to develop the JSF Application.If run this i am getting the target unreachable and returned null Please help me to resolve this error.

LoginFormBean.java

package com.Lawrence.Bean;

import java.sql.SQLException;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.naming.NamingException;

/**
 *
 * @author Lawrence Thanakumar
 */
@ManagedBean(name="loginBean")
@SessionScoped
public class LoginFormBean 
{
    private String userName;
    private String passWord;

    @ManagedProperty(value="#{databaseBean}")
    public DatabaseBean bean;
    /**
     * @return the userName
     */
    public String getUserName() {
        return userName;
    }

    /**
     * @param userName the userName to set
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }

    /**
     * @return the passWord
     */
    public String getPassWord() {
        return passWord;
    }

    /**
     * @param passWord the passWord to set
     */
    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }

    public String doLogin() throws NamingException, SQLException
    {
       /* if(getUserName().equals("Lawrence") && getPassWord().equals("lawrence"))
        {
            return "success";
        }
        else
        {
            return "failure";
        }*/

        boolean status = true;

        status = bean.doLogin(getUserName(), getPassWord());

        if(status)
        {
            return "success";
        }
        else
        {
            return "failure";
        }
    }

    public String logOut()
    {
        return "home";
    }

    /**
     * @param bean the bean to set
     */
    public void setBean(DatabaseBean bean) {
        this.bean = bean;
    }
}

DatabaseBean.java

package com.Lawrence.Bean;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.annotation.Resource;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
 *
 * @author Lawrence Thanakumar
 */
@ManagedBean(name="databaseBean")
@SessionScoped
public class DatabaseBean 
{
    @Resource(name="jdbc/test")
    private DataSource ds;

    public boolean doLogin(String username,String password) throws NamingException, SQLException
    {
        boolean status = true;

        Context initialContext = new InitialContext();
        if ( initialContext == null)
        {
            System.out.println("JNDI problem. Cannot get InitialContext.");
        }
        ds = (DataSource)initialContext.lookup("java:comp/env/jdbc/test");

        if(ds==null)
            throw new SQLException("Can't get data source");
        Connection con = ds.getConnection();

                if(con==null)
            throw new SQLException("Can't get database connection");

        PreparedStatement ps 
            = con.prepareStatement(
               "select username,password from login where username=? and password=?"); 
        ps.setString(1,username);
        ps.setString(2, password);

                ResultSet result =  ps.executeQuery();

        if(result.next())
        {
            status = true;
        }
        else
        {
            status = false;
                }
            return status;
    }
}

faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd"
    version="1.2">
    <application>
        <resource-bundle>
            <base-name>com.Lawrence.resourceBundle.resourceBundle</base-name>
            <var>vm</var>
        </resource-bundle>
    </application>
    <application>
        <resource-bundle>
            <base-name>com.Lawrence.resourceBundle.label</base-name>
            <var>label</var>
        </resource-bundle>
    </application>
    <navigation-rule>
        <display-name>HelloWorld</display-name>
        <from-view-id>/Login.jsp</from-view-id>
        <navigation-case>
            <from-outcome>success</from-outcome>
            <to-view-id>/Success.jsp</to-view-id>
            <redirect/>
        </navigation-case>
        <navigation-case>
            <from-outcome>failure</from-outcome>
            <to-view-id>/Failure.jsp</to-view-id>
            <redirect/>
        </navigation-case>
        <navigation-case>
            <from-outcome>home</from-outcome>
            <to-view-id>/Login.jsp</to-view-id>
            <redirect/>
        </navigation-case>
    </navigation-rule>
</faces-config>

Login.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<!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=ISO-8859-1">
<title>Login Page</title>
</head>
<body>
    <f:view>
        <center>
            <h:form>
                <h:outputText id="username" value="#{label['label.username']}" />
                <h:inputText id="userName" value="#{loginBean.userName}"
                    required="true" requiredMessage="#{vm['username.required']}" />
                <p>
                    <h:message id="errors" for="userName" style="color:red" />
                </p>
                <h:outputText id="password" value="#{label['label.password']}" />
                <h:inputSecret id="passWord" value="#{loginBean.passWord}"
                    required="true" requiredMessage="#{vm['password.required']}" />
                <p>
                    <h:message id="errors_1" for="passWord" style="color:red" />
                </p>
                <br>
                <h:commandButton id="btn" value="Login" action="#{loginBean.doLogin}" />
            </h:form>
        </center>
    </f:view>
</body>
</html>

Web.xml

<?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>JSFTutorial</display-name>
    <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>/jsf/*</url-pattern>
    </servlet-mapping>
  <welcome-file-list>
    <welcome-file>jsf/index.jsp</welcome-file>
  </welcome-file-list>
  <resource-ref>
        <description>Global Address Database</description>
        <res-ref-name>jdbc/test</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
 </resource-ref>
</web-app>

0 Answers0