JSF Login Example

Thursday, July 30, 2009

I had seen many new deveopers looking for jsf login example. So I decided to write a small example.

Below are the required libraries to run this example which are easily availabel
commons-beanutils
commons-collections
commons-digester
commons-el
commons-logging
jsf-api
jsf-impl
jstl
myfaces-all
standard
el-ri
el-api

The structure of the applictaion which I am following is (Eclipse IDE)

JSFLoginExample
|---src
| |----com
| | |----raj
| | | |----SimpleLogin.java
| | | |----LoginBean.java
|---WebContent
| |---WEB-INF
| | |---lib
| | |---faces-config.xml
| | |---web.xml
| |---login.jsp
| |---resultforfail.jsp
| |---resultforSuccess.jsp

It is true that different IDE's use different structure, but at last when war is build they follow same structure.
Lets start...


SimpleLogin.java



package com.raj;

/**
* @author nagarajuv
*
*/

public class SimpleLogin{

private LoginBean loginBean;
public SimpleLogin(){}

public String CheckValidUser(){
if(loginBean.loginname.equals("chandan") && loginBean.password.equals("chand")){
System.out.println("chandan");
return "success";
}
else{
System.out.println("Going to failure page");
return "fail";
}
}

public LoginBean getLoginBean() {
return loginBean;
}

public void setLoginBean(LoginBean loginBean) {
this.loginBean = loginBean;
}

}


LoginBean.java


package com.raj;

/**
* @author nagarajuv
*
*/
public class LoginBean {
String loginname;
String password;

public LoginBean(){
}

/**
* @param loginname
* @param password
*/
public LoginBean(String loginname, String password) {
super();
this.loginname = loginname;
this.password = password;
}

public String getLoginname(){
return loginname;
}

public void setLoginname(String loginname){
this.loginname = loginname;
}

public String getPassword(){
return password;
}

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


login.jsp

<%@ page contentType="text/html"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>

<f:view>
<html>
<head><title>JSF Simple Login Example</title></head>
<body>
<center>
<h:form>
<table>
<tr>
<td><h:outputText value="Enter Login ID: " /></td>
<td><h:inputText id="loginname" value="#{SimpleLogin.loginBean.loginname}" /></td>
</tr>
<tr>
<td><h:outputText value="Enter Password: " /></td>
<td><h:inputSecret id="password" value="#{SimpleLogin.loginBean.password}" /></td>
</tr>
<tr>
<td> </td>
<td><h:commandButton value="Login" action="#{SimpleLogin.CheckValidUser}" /></td>
</tr>
</table>
</h:form>
</center>
</body>
</html>
</f:view>


resultforsuccess.jsp

Login Success.......!


resultforfail.jsp

Login Fail.......!


faces-config.xml

<?xml version="1.0"?>
<!DOCTYPE faces-config PUBLIC
"-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
"http://java.sun.com/dtd/web-facesconfig_1_1.dtd">

<faces-config>
<managed-bean>
<managed-bean-name>loginBean</managed-bean-name>
<managed-bean-class>com.raj.LoginBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<managed-bean>
<managed-bean-name>SimpleLogin</managed-bean-name>
<managed-bean-class>com.raj.SimpleLogin</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>loginBean</property-name>
<value>#{loginBean}</value>
</managed-property>
</managed-bean>

<navigation-rule>
<from-view-id>/login.jsp</from-view-id>
<navigation-case>
<from-action>#{SimpleLogin.CheckValidUser}</from-action>
<from-outcome>success</from-outcome>
<to-view-id>resultforsuccess.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-action>#{SimpleLogin.CheckValidUser}</from-action>
<from-outcome>fail</from-outcome>
<to-view-id>resultforfail.jsp</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>


web.xml

<?xml version="1.0"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application
2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>server</param-value>
</context-param>

<!-- Faces Servlet -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup> 1 </load-on-startup>
</servlet>

<!-- Faces Servlet Mapping -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>


Type this url in addressbar--> http://localhost:8080/JSFLoginExample/login.jsf
Enjoy the jsf(most popular java framework today)


Image Hosted by ImageShack.us

Image Hosted by ImageShack.us