Results 1 to 3 of 3
- 07-01-2011, 03:59 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 36
- Rep Power
- 0
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
login-input.xhtmlJava 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-ok.xhtmlJava 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-missing-data.xhtmlJava 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-error.xhtmlJava 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>
faces-config.xmlJava 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>
web.xmlJava 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>
UserPOJO.javaJava 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>
UserUtils.javaJava 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; } }
LoginForm.javaJava 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() { } }
Thank you for your timeJava 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"; } } } }There is not knowledge that it is not power!
buyapentiumjerk.blogspot.com
- 07-01-2011, 03:28 PM #2
Member
- Join Date
- Jul 2011
- Posts
- 3
- Rep Power
- 0
Looks great, so as a tip you can avoid
instead usingJava Code:if (userLogin.isEmpty() || userPassword.isEmpty()) { return "missing"; }As any way is greatJava Code:<h:inputText value="#{loginForm.userLogin}" required="true"/>
- 07-06-2011, 11:42 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 36
- Rep Power
- 0
Hi hugomx,
thank you for your answer, I didn't knew about that.
I'm now using PrimeFaces and added a bit more of complexity, I've created a new Class Company for all Users and I'm trying to use Hibernate with all this, let me see what I can come up with. One question, normally I'd create a List<UserPOJO> companyUsers; in class Company to get all Users allocated to that Company, but I've been reading some tutorials and some say to use HashMap instead of List but this seems to be the prefered way with JSF 1.x?There is not knowledge that it is not power!
buyapentiumjerk.blogspot.com
Similar Threads
-
What can i do to improve this? Explanation please
By biggerthanblue in forum New To JavaReplies: 1Last Post: 02-19-2011, 06:55 AM -
Improve my GUI!
By AJArmstron@aol.com in forum New To JavaReplies: 8Last Post: 04-27-2010, 09:17 PM -
how to improve my security?
By anthrax in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 03-13-2009, 09:08 AM -
cannot open database requested in login. Login fails
By banduskank in forum JDBCReplies: 0Last Post: 06-25-2008, 12:41 PM -
how to improve the performance of JWS?
By dinesh kaushik in forum Java AppletsReplies: 0Last Post: 11-21-2007, 08:46 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks