21.1.Role based Login Application using MVC
Friday, February 4, 2011
In this post we are going to learn, how Authentication takes place in the web browser.
So lets get started !!!
Initially create a Database and then create table called Login
with the fallowing columns.
1. username
2. password
3. userrole
After creating the table populate with sample data and give roles as admin, customer
In the Next step create the fallowing directory structure in Netbeans IDE
Then create a servlet named with LoginServlet in com.controller package and write the fallowing code in processRequest() method.Prior to that inject Persistence in the servlet by using Entity Manager option.
For this Generate Entity Class from Database and put it in com.model package.
The code for entity class is given below.
When we compile and run the above code we will get the fallowing output !!
When we use the Login page according to Login Credentials, the application will navigate to the particular role as given in the database.
That's it for this time...
meet you in the next Post !!!
So lets get started !!!
Initially create a Database and then create table called Login
with the fallowing columns.
1. username
2. password
3. userrole
After creating the table populate with sample data and give roles as admin, customer
In the Next step create the fallowing directory structure in Netbeans IDE
Then create a servlet named with LoginServlet in com.controller package and write the fallowing code in processRequest() method.Prior to that inject Persistence in the servlet by using Entity Manager option.
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { String username = request.getParameter("username"); String password = request.getParameter("password"); HttpSession session = request.getSession(); Context ctx = (Context) new InitialContext().lookup("java:comp/env"); utx.begin(); EntityManager em = (EntityManager) ctx.lookup("persistence/LogicalName"); Login login = em.find(Login.class, username); if (login == null) { request.setAttribute("notok", "Invalid Username/Password !"); RequestDispatcher view = request.getRequestDispatcher("/index.jsp"); view.forward(request, response); return;} if (login.getPassword().equals(password)) { if (login.getUserrole().equalsIgnoreCase("admin")) { RequestDispatcher view = request.getRequestDispatcher("/admin.jsp"); view.forward(request, response); } else if (login.getUserrole().equalsIgnoreCase("customer")) { session.setAttribute("login", login); RequestDispatcher view = request.getRequestDispatcher("/customer.jsp"); view.forward(request, response); }} else { request.setAttribute("notok", "Invalid Username/Password !"); RequestDispatcher view = request.getRequestDispatcher("/login.jsp"); view.forward(request, response); return; } utx.commit(); } catch (Exception e) { out.print(e); } finally { out.close(); }
For this Generate Entity Class from Database and put it in com.model package.
The code for entity class is given below.
package com.model;import java.io.*;import javax.persistence.*; @Entity @Table(name = "LOGIN") @NamedQueries({@NamedQuery(name = "Login.findAll", query = "SELECT l FROM Login l"), @NamedQuery(name = "Login.findByUsername", query = "SELECT l FROM Login l WHERE l.username = :username"), @NamedQuery(name = "Login.findByPassword", query = "SELECT l FROM Login l WHERE l.password = :password"), @NamedQuery(name = "Login.findByUserrole", query = "SELECT l FROM Login l WHERE l.userrole = :userrole")}) public class Login implements Serializable { private static final long serialVersionUID = 1L; @Id @Basic(optional = false) @Column(name = "USERNAME") private String username; @Column(name = "PASSWORD") private String password; @Column(name = "USERROLE") private String userrole; public Login() { } public Login(String username) { this.username = username; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getUserrole() { return userrole; } public void setUserrole(String userrole) { this.userrole = userrole; } @Override public int hashCode() { int hash = 0; hash += (username != null ? username.hashCode() : 0); return hash; } @Override public boolean equals(Object object) { // TODO: Warning - this method won't work in the case the id fields are not set if (!(object instanceof Login)) { return false; } Login other = (Login) object; if ((this.username == null && other.username != null) || (this.username != null && !this.username.equals(other.username))) { return false; } return true; } @Override public String toString() { return "com.model.Login[username=" + username + "]"; } }
When we compile and run the above code we will get the fallowing output !!
When we use the Login page according to Login Credentials, the application will navigate to the particular role as given in the database.
That's it for this time...
meet you in the next Post !!!
Labels:
MVC