Struts 2.0 Login Example Using Hibernate 3.0
Previous | Home | Next |
Hibernate 3.0 Login Example using Struts 2.0 the following tool are required for run this example
- JDK 1.5
- MyEclipse IDE
- Server Tomcat 6.0
- Struts 2.0 jar file
- Ojdbc14.jar
- Oracle 10G Database Server
- Hibernate 3.0 jar file
Directory Structure of Login Example in Struts 2.0 and Hibernate3.0 Using MyEclipse IDE
index.jsp<html> <head> <title>Login</title> </head> <body><center> <table cellpadding="0" cellspacing="0" align="center" style="border:1px #CCC double"> <tr><td align="center" valign="top"> <table cellpadding="0" cellspacing="2" align="center" style="border:1px #CCC double;background-image: url('images/03.GIF');"> <tr> <td height="40" width="125" style="background-image: url('images/003.jpg');" align="center"><a href="index.jsp?page=default" style="color: lime;text-decoration: none;">Home</a></td> <td height="40" width="125" style="background-image: url('images/003.jpg');" align="center"><a href="index.jsp?page=login" style="color: lime;text-decoration: none;">Login</a></td> <td height="40" width="125" style="background-image: url('images/003.jpg');" align="center"><a href="index.jsp?page=registation" style="color: lime;text-decoration: none;">Registation</a></td> <td height="40" width="125" style="background-image: url('images/003.jpg');" align="center"><a href="index.jsp?page=default" style="color: lime;text-decoration: none;">LogOut</a></td> </tr> </table></td> </tr> <tr><td height="400" width="500" align="center" valign="middle"> <table cellpadding="0" cellspacing="0" align="center" style="border:1px #CCC double"> <tr> <td height="400" width="500" align="center" valign="middle" style="background-image:url('images/22.JPG'); "> <% String url=request.getParameter("page"); String furl=url+".jsp"; if(url==null){ %> <jsp:include page="default.jsp"/> <% }else{ %> <jsp:include page="<%= furl %>"/> <%} %> </td></tr></table> </td></tr></table> </center> </body> </html>web.xml<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" 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_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping></web-app>struts.xml<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="demo" extends="struts-default"> <action name="login" class="mypack.LoginAction"> <result name="success">/index.jsp?page=successlogin</result> <result name="input">/index.jsp?page=login</result> <result name="error">/index.jsp?page=login</result> </action> </package> </struts>
hibernate.cfg.xml<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <!-- Generated by MyEclipse Hibernate Tools.--> <hibernate-configuration> <session-factory> <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property> <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property> <property name="connection.username">system</property> <property name="connection.password">system</property> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <mapping resource="login.hbm.xml"/> </session-factory> </hibernate-configuration>
login.hbm.xml<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated by MyEclipse Hibernate Tools.--> <hibernate-mapping> <class name="mypack.LoginHibernate"> <id name="id" type="int"> <generator class="increment"/> </id> <property name="name"/> <property name="password"/> </class> </hibernate-mapping>LoginHibernate.javapackage mypack; public class LoginHibernate { int id; String name; String password; public LoginHibernate() { super(); // TODO Auto-generated constructor stub } public LoginHibernate(String name, String password) { super(); this.name = name; this.password = password; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }LoginAction.javapackage mypack; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport{ private static final long serialVersionUID = 1L; private String name=null; private String password=null; @SuppressWarnings("unused") private LoginHibernate lh=new LoginHibernate(); public void validate(){ if(name.length()==0) this.addFieldError("name", "Name is required.."); if(password.length()==0) this.addFieldError("password", "Password is required.."); } public String execute(){ DAO dao=new DAO(); if(dao.find(getName(),getPassword())) return "success"; else this.addActionError("Invalid UserName and Password.."); return "error"; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }DAO.javapackage mypack; import java.util.Iterator; import org.hibernate.*; public class DAO { @SuppressWarnings("unchecked") public boolean find(String name,String password){ Session session=ConnectionProvider.getSession(); String SQL_QUERY =" from LoginHibernate ih where ih.name='" + name + "' and ih.password='" + password + "'"; Query query = session.createQuery(SQL_QUERY); Iterator<LoginHibernate> it=query.iterate(); while(it.hasNext()) { session.close(); return true; } session.close(); return false; } }ConnectionProvider.javapackage mypack; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class ConnectionProvider { static SessionFactory sessionfactory; static{ Configuration cfg=new Configuration().configure(); sessionfactory=cfg.buildSessionFactory(); } public static Session getSession(){ return sessionfactory.openSession(); } }default.jsp<img src="images/welcome.jpg" width="500" height="400"/>login.jsp<%@ taglib uri="/struts-tags" prefix="s"%> <table cellpadding="0" cellspacing="0"> <tr> <td height="300" width="300" style="background-image: url('images/2.JPG');color: yellow;" align="center"> <h1 style="color: yellow;">Login Page</h1> <s:actionerror/> <s:form action="login" cssStyle="color:yellow;"> <s:textfield name="name" label="Name"/> <s:password name="password" label="Password"/> <s:submit value="Login"/> </s:form> </td> </tr> </table>registation.jsp<%@ taglib uri="/struts-tags" prefix="s"%> <table cellpadding="0" cellspacing="0"> <tr><td height="300" width="300" style="background-image: url('images/2.JPG');color: yellow;" align="center"> <h1 style="color: yellow;">Registation Page</h1> <s:actionerror/> <s:form action="registation" cssStyle="color:yellow;"> <s:textfield name="name" label="Name"/> <s:password name="password" label="Password"/> <s:textfield name="email" label="Email"/> <s:select list="{'Male','Female'}" label="Gender" headerValue="Select Gender" headerKey="" name="sex"/> <s:textfield name="mobile" label="Mobile"/> <s:submit value="Register"/> </s:form></td></tr> </table>successlogin.jsp<%@ taglib uri="/struts-tags" prefix="s"%> <table cellpadding="0" cellspacing="0"> <tr><td height="300" width="300" style="background-image: url('images/2.JPG');"> <center><h1 style="color: yellow;">Welcome </h1><br/> <br/><h3 style="color: lime;"><s:property value="name"/></h3></center> </td></tr></table>Output
Previous | Home | Next |