Home Jsf Remember Me Login Application In JSF


Share on Google+Share on Google+

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
<jsp:forward 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" %>

		<title>Remember Me Login Application</title>

	<div align="left">
	<h:form id="frm" onsubmit="return initialize();">
	<table height="250" border="1" cellpadding="3" cellspacing="1">
				<td valign="top"><br/><br/>
		<td><h:outputText value="User Name: "
		<td><h:inputText id="username" value
="#{RememberMeLogin.username}" /></td>
				<td><h:outputText value="Password: "
				<td><h:inputSecret id="password" 
required="true" redisplay="true" /></td>
				<td><h:commandButton value="Login"
 action="#{RememberMeLogin.CheckLogin}" onclick="initialize" /></td>
				<td><h:selectBooleanCheckbox id=
"remember" value="#{RememberMeLogin.remember}" 
onclick="return check(this);" />
				<h:outputLabel for="remember">Remember

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(){

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

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

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

  public String getPassword(){
  if(remember == false){
  password = "";
  return password;
  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";
  remember1 = "true";
  Cookie btremember = new Cookie("btremember",remember1);

  return "success";
  return "failure";

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

Here is the code of the web.xml file:

<?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">


    <!-- Faces Servlet -->
        <servlet-name>Faces Servlet</servlet-name>
        <load-on-startup> 1 </load-on-startup>

	<!-- Faces Servlet Mapping -->
        <servlet-name>Faces Servlet</servlet-name>

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

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



Here is the output for the whole example:

Download this complete source code of the example.


If you enjoyed this post then why not add us on Google+? Add us to your Circles

Liked it!  Share this Tutorial

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: March 18, 2008

Related Tutorials

Ask Questions?    Discuss: Remember Me Login Application In JSF   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
December 5, 2011
Security of your tip

Thanks for the tip, but storing login/password in a cookie isn't very secure, is it?