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


 

Ads

Tutorials   
JSF Tutorials: Easy steps to learn JSF Java Server Faces (JSF) Tutorials What is JSF Mojarra? JSF Life Cycle JSF Examples JSF Tutorial For Beginners Roseindia JSF Tutorial JSF core Tag Reference JSF internationalization example Tomahawk selectOneCountry tag RCFaces About Hibernate, Spring and JSF Integration Tutorial Creating Login Page In JSF using NetBeans JSF Hello World Declaring the Bean class in the faces-config.xml file Creating a Web Application with the JSF Framework Binding the Form Elements With the Bean Properties Adding Navigation in JSF Application Binding Component Instance to a Bean Property Tomahawk validateEqual tag Tomahawk validateEmail tag Tomahawk validateCreditCard tag Tomahawk selectOneListbox tag Tomahawk selectManyListbox tag Tomahawk selectManyCheckbox tag Tomahawk selectBooleanCheckbox tag Tomahawk saveState tag Tomahawk popup tag Tomahawk inputCalendar tag Tomahawk htmlTag tag Tomahawk graphicImage tag Tomahawk documentHead tag Tomahawk documentBody tag Tomahawk document tag Tomahawk column tag DataBase Connectivity with MySql in Visual Web JSF Application Using Net Beans IDE Retrieve Image in JSF From MySQL Table Using NetBeans IDE JSF Training Java Sever Faces 1.2 - JSF 1.2 Display Data from Database in JSF Application Business Objects of Business Logic tier Setup MySQL Database Working with Database through JDBC Working with JavaScript & CSS Referencing a Localized Message Referencing Resource Bundle from a Page Binding Component Value to an Implicit Object Phase Listener Value Change Event Listener Action Event Listener
Ads
<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" %>

<f:view>
<html>
	<head>
		<title>Remember Me Login Application</title>
	</head>

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

<?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>
</web-app>

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

<faces-config>
	<managed-bean>
		<managed-bean-name>RememberMeLogin</managed-bean-name>
		<managed-bean-class>roseindia.RememberMeLogin
</managed-bean-
class>
		<managed-bean-scope>request</managed-bean-scope>
	</managed-bean>

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

Here is the output for the whole example:

Download this complete source code of the example.

Advertisements

Share on Google+Share on Google+

Remember Me Login Application In JSF

Posted on: March 18, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Remember Me Login Application In JSF   View All Comments

Post your Comment


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

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

Ads

 

Ads