Remember Me Login Application In JSF

This section gives you an application which facilitates for login. But basically this is a different type of login application in which, you will get a two text box labeled with the label like "User Name: " and another is the "Password: " these labels are denoting both text text boxes in which, one is for entering user name and another is for entering password. After entering user name and password you have to click on the "Login" button for submitting the user login form. This login form also holds an extra component i.e. the check box component which is for check to remember always on your system. If you check the check box component then the user name and password will be saved until you submit the login form after entering user name and the password without check the check box component.

You can learn by following the given example code that is given as ahead. In this example, we are using the procedure of cookies to save user name and password. When you open the login form again then the username and the password are seen as it is if you had checked the check box component. You can understand the application very efficiently by seeing the code of the example.

Here is the code of the index.jsp:

page="login.jsf" />

Here is the code of the login.jsp:

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>

<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>

<f:view>


</span><span style="color:#000000;">Remember Me Login Application</span><span style="color:#0000ff;">



align="left">
<h:form id="frm" onsubmit="return initialize();">
height="250" border="1" cellpadding="3" cellspacing="1">



valign="top">



















<h:outputText value="User Name: "
/>
<h:inputText id="username" value
=
"#{RememberMeLogin.username}" />
<h:outputText value="Password: "
/>
<h:inputSecret id="password"
value
="#{RememberMeLogin.password}" required="true" redisplay="true" />
<h:commandButton value="Login"
action="#{RememberMeLogin.CheckLogin}" onclick="initialize" />
<h:selectBooleanCheckbox id=
"remember"
value="#{RememberMeLogin.remember}" onclick="return check(this);" />
<h:outputLabel for="remember">Remember
Me
h:outputLabel>


h:form>



f:view>

Here is the code of the RememberMeLogin.java file which is the bean class:

package roseindia;

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.faces.context.*;

public class RememberMeLogin{
boolean remember;
String username;
String password;
String remember1 = "hi";

public RememberMeLogin(){
checkCookie();
}

public void setUsername(String username){
this.username = username;
}

public String getUsername(){
if(remember == false){
username = "";
return username;
}
else{
return username;
}
}

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

public String getPassword(){
if(remember == false){
password = "";
return password;
}
else{
return password;
}
}

public void setRemember(boolean remember){
this.remember = remember;
}

public boolean getRemember(){
return remember;
}

public String CheckLogin(){
if(username.equals("chandan") && password.equals("chandan")){
FacesContext facesContext = FacesContext.getCurrentInstance();

// Save the userid and password in a cookie
Cookie btuser = new Cookie("btuser", username);
Cookie btpasswd = new Cookie("btpasswd",password);
if(remember == false){
remember1 = "false";
}
else{
remember1 = "true";
}
Cookie btremember = new Cookie("btremember",remember1);
btuser.setMaxAge(3600);
btpasswd.setMaxAge(3600);

((HttpServletResponse)facesContext.getExternalContext().getResponse()).
addCookie
(btuser);
((HttpServletResponse)facesContext.getExternalContext().getResponse()).
addCookie
(btpasswd);
((HttpServletResponse)facesContext.getExternalContext().getResponse()).
addCookie
(btremember);

return "success";
}
else{
return "failure";
}
}

public void checkCookie(){
FacesContext facesContext = FacesContext.getCurrentInstance();
String cookieName = null;
Cookie cookie[] = ((HttpServletRequest)facesContext.getExternalContext().
getRequest
())
.getCookies
();
if(cookie != null && cookie.length > 0){
for(int i = 0; i){
cookieName = cookie[i].getName();
if(cookieName.equals("btuser")){
username = cookie[i].getValue();
}
else if(cookieName.equals("btpasswd")){
password = cookie[i].getValue();
}
else if(cookieName.equals("btremember")){
remember1 = cookie[i].getValue();
if(remember1.equals("false")){
remember = false;
}
else if(remember1.equals("true")){
remember = true;
}
}
}
}
else
System.out.println("Cannot find any cookie");
}
}

Here is the code of the web.xml file:

version="1.0"?> 

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_METHODparam-name>
<param-value>serverparam-value>
context-param>


<servlet>
<servlet-name>Faces Servletservlet-name>
<servlet-class>javax.faces.webapp.FacesServletservlet-class>
<load-on-startup> 1 load-on-startup>
servlet>


<servlet-mapping>
<servlet-name>Faces Servletservlet-name>
<url-pattern>*.jsfurl-pattern>
servlet-mapping>
web-app>

Here is the code of the faces-config.xml file:

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

faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces
Config 1
.0//EN" "http://java.sun.com/dtd/web-facesconfig_1_0.dtd">

<faces-config>
<managed-bean>
<managed-bean-name>RememberMeLoginmanaged-bean-name>
<managed-bean-class>roseindia.RememberMeLoginmanaged-bean-
class
>
<managed-bean-scope>requestmanaged-bean-scope>
managed-bean>

<navigation-rule>
<from-view-id>/login.jspfrom-view-id>
<navigation-case>
<from-action>#{RememberMeLogin.CheckLogin}from-action>
<from-outcome>successfrom-outcome>
<to-view-id>/success.jspto-view-id>
navigation-case>
<navigation-case>
<from-action>#{RememberMeLogin.CheckLogin}from-action>
<from-outcome>failurefrom-outcome>
<to-view-id>/failure.jspto-view-id>
navigation-case>
navigation-rule>
faces-config>

Here is the output for the whole example:

Download this complete source code of the example.

0 comments:

Post a Comment