How can I improve -> Simple JSF Login example
Hi everyone,
I've successfully implemented a simple Login using JSF, now I need to know how can I improve my code. All comments will be appreciated.
index.jsp
Code:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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>Index</title>
</head>
<body>
<%
response.sendRedirect("login-input.jsf");
%>
</body>
</html>
login-input.xhtml
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Login input</title>
</h:head>
<h:body>
<fieldset>
<legend>Enter you login data</legend>
<h:form>
<table>
<tr align="left">
<th>Username</th>
<th><h:inputText value="#{loginForm.userLogin}" /></th>
</tr>
<tr align="left">
<th>Password</th>
<th><h:inputSecret value="#{loginForm.userPassword}" /></th>
</tr>
<tr align="left">
<th><h:commandButton value="Submit" action="#{loginForm.doLogin}" />
</th>
</tr>
</table>
</h:form>
</fieldset>
</h:body>
</html>
login-ok.xhtml
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Login OK</title>
</h:head>
<h:body>
<h2>Welcome #{loginForm.userName}, you have a successful Login.</h2>
<ul>
<li>User ID: #{loginForm.userId}</li>
<li>User login: #{loginForm.userLogin}</li>
<li>User password: #{loginForm.userPassword}</li>
</ul>
<h3>
Return to <a href="index.jsp">Login page</a>.
</h3>
</h:body>
</html>
login-missing-data.xhtml
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Login error - missing data</title>
</h:head>
<h:body>
<h2>Login error - missing data</h2>
<p>You must insert User login details.</p>
<h3>
Back to <a href="index.jsp">Login page</a>.
</h3>
</h:body>
</html>
login-error.xhtml
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Login error</title>
</h:head>
<h:body>
<h2>Login error</h2>
<ul>
<li>User login: #{loginForm.userLogin}</li>
<li>Password: #{loginForm.userPassword}</li>
</ul>
<h3>
Invalid login, back to <a href="index.jsp">Login page</a>.
</h3>
</h:body>
</html>
faces-config.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xi="http://www.w3.org/2001/XInclude"
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_2_0.xsd">
<managed-bean>
<managed-bean-name>loginForm</managed-bean-name>
<managed-bean-class>login.LoginForm</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<navigation-rule>
<from-view-id>/login-input.xhtml</from-view-id>
<navigation-case>
<from-outcome>error</from-outcome>
<to-view-id>/login-error.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>ok</from-outcome>
<to-view-id>/login-ok.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>missing</from-outcome>
<to-view-id>/login-missing-data.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
web.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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-app_3_0.xsd">
<display-name>SimpleLogin</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>
</web-app>
UserPOJO.java
Code:
package login;
public class UserPOJO {
private int userId;
private String userName;
private String userLogin;
private String userPassword;
public UserPOJO() {
}
public UserPOJO(int userId, String userName, String userLogin,
String userPassword) {
super();
this.userId = userId;
this.userName = userName;
this.userLogin = userLogin;
this.userPassword = userPassword;
}
@Override
public String toString() {
return "User [userId=" + userId + ", userName=" + userName
+ ", userLogin=" + userLogin + ", userPassword=" + userPassword
+ "]";
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserLogin() {
return userLogin;
}
public void setUserLogin(String userLogin) {
this.userLogin = userLogin;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
}
UserUtils.java
Code:
package login;
public class UserUtils {
/**
* Define all available Users, static so we just have one instance
*/
private static UserPOJO[] availableUsers = {
new UserPOJO(0, "Antonio Godinho", "log001", "pass001"),
new UserPOJO(1, "Maria Alberta", "log002", "pass002"),
new UserPOJO(2, "Joao Farias", "log003", "pass003") };
/**
*
* @return all available Users
*/
public static UserPOJO[] getAvailableUsers() {
return availableUsers;
}
/**
*
* @param userPassword
* @return User if Password exists
*/
public static UserPOJO findUser(String userLogin, String userPassword) {
for (UserPOJO user : availableUsers) {
if (user.getUserLogin().equals(userLogin)
&& user.getUserPassword().equals(userPassword)) {
return user;
}
}
return null;
}
/**
* Non instantiatable class
*/
private UserUtils() {
}
}
LoginForm.java
Code:
package login;
import javax.annotation.ManagedBean;
@ManagedBean
public class LoginForm {
// Bean Properties
private UserPOJO user;
private int userId;
private String userName;
private String userLogin;
private String userPassword;
public UserPOJO getUser() {
return user;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserLogin() {
return userLogin;
}
public void setUserLogin(String userLogin) {
this.userLogin = userLogin;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
// Action
public String doLogin() {
if (userLogin.isEmpty() || userPassword.isEmpty()) {
return "missing";
} else {
user = UserUtils.findUser(userLogin, userPassword);
if (user == null) {
return "error";
} else {
userId = user.getUserId();
userName = user.getUserName();
return "ok";
}
}
}
}
Thank you for your time