In this post we will see how to create a login page using spring 3 and Hibernate 3. Below is
the screen shot of the directory structure.
As shown in the below screens create "LoginTab" table. For this example I am using Oracle as data base.
After creating the table, enter username, password and access_mode of your choice.
Next step is to create a bean for the table "LoginTab". Create a class with as shown below.
We need to mark the class as "@Entity" to state the class as entity.
File: com.ram.form.LoginTab
package com.ram.form; import javax.persistence.Entity; import javax.persistence.Id; @Entity public class LoginTab { @Id private String uname; private String password; private String access_mode; public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getAccess_mode() { return access_mode; } public void setAccess_mode(String access_mode) { this.access_mode = access_mode; } }
Create a web.xml file as shown below. Note that the welcome-file is given as "callLogin.html".
This is name is checked in the controller and the "LoginPage.jsp" will be displayed.
File: WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>Spring Login Page Example</display-name> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>callLogin.html</welcome-file> </welcome-file-list> </web-app>
File: com.ram.controller.LoginController.java
package com.ram.controller; import java.util.Iterator; import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import com.ram.dao.LoginDaoImpl; import com.ram.form.LoginTab; @Controller public class LoginController { @Autowired private LoginDaoImpl loginDaoImpl; @RequestMapping(value="/callLogin", method = RequestMethod.GET) public String callLogin(Map model){ LoginTab LoginPage = new LoginTab(); model.put("LoginPage", LoginPage); return "LoginPage"; } @RequestMapping("/login") public String loginValidate(@ModelAttribute("loginTab") LoginTab loginTab, BindingResult result){ boolean flag = false; List<LoginTab> users = loginDaoImpl.validateUser(loginTab); for(Iterator<LoginTab> it = users.iterator(); it.hasNext();){ LoginTab login = it.next(); if(login.getUname().equals(loginTab.getUname())){ if(login.getPassword().equals(loginTab.getPassword())){ flag = true; } } } if(flag==true){ return "Success"; }else{ return "Error"; } } }
Lets create a dao class. In this class using the SessionFactory we will establish the connection, use
the HQL to get the LoginTab table details.
File: com.ram.dao.LoginDaoImpl.java
package com.ram.dao; import java.util.List; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import com.ram.form.LoginTab; @Repository public class LoginDaoImpl { @Autowired private SessionFactory sessionFactory; public List<LoginTab> validateUser(LoginTab loginTab){ String hql = "from LoginTab where uname = '"+loginTab.getUname()+"'"; return getSessionFactory().openSession().createQuery(hql).list(); } public SessionFactory getSessionFactory() { return sessionFactory; } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } }
File: WEB-INF/dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <context:annotation-config/> <context:component-scan base-package="com.ram"></context:component-scan> <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/> <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"/> <property name="username" value="hr"/> <property name="password" value="hr"/> <property name="initialSize" value="2"/> <property name="maxActive" value="5"/> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="packagesToScan" value="com.ram.form"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> </bean> </beans>
File: WEB-INF/jsp/LoginPage.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Spring Login Page</title> </head> <body> <center> <h3>Login Page</h3> <form:form action="login.html" commandName="LoginPage"> <table> <tr> <td>User Name: <FONT color="red"><form:errors path="uname" /></FONT></td> <td><form:input path="uname" /></td> </tr> <tr> <td>Password: <FONT color="red"><form:errors path="password" /></FONT></td> <td><form:password path="password" /></td> </tr> </table> <p> <input type="submit" value="Submit" /> </p> </form:form> </center> </body> </html>
File: WEB-INF/jsp/Success.jsp
<%@ 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> <title>Spring Success Page</title> </head> <body> <center> <h4>Welcome to the application.</h4> <p> <a href="callLogin.html">Back</a> </p> </center> </body> </html>
File: WEB-INF/jsp/Error.jsp
<%@ 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> <title>Spring Error Page</title> </head> <body> <center> <h4 style="color: red">Error page, please go back to login page and enter correct credentials</h4> <p> <a href="callLogin.html">Back</a> </p> </center> </body> </html>
Right click on the "Spring_Hibernate_Login" directory structure and select "Run As -> Run on Server".
Once the login page appears, enter the credentials to successfully login. If incorrect credentials are
given, then error page will be thrown.
No comments:
Post a Comment