<?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"> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <session-config> <session-timeout>10</session-timeout> </session-config> <welcome-file-list> <welcome-file>registeruser.jsp</welcome-file> </welcome-file-list> </web-app>
<?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"> <hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property> <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property> <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property> <property name="hibernate.connection.username">hr</property> <property name="hibernate.connection.password">hr</property> <property name="hibernate.hbm2ddl.auto">create</property> <mapping class="entities.User"/> </session-factory> </hibernate-configuration>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="default" extends="struts-default"> <action name="register" class="actions.RegisterAction"> <result name="success">registercomplete.jsp</result> <result name="fail">registerfail.jsp</result> </action> </package> </struts>
package entities; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table (name="users") public class User { @Id @GeneratedValue ( strategy = GenerationType.AUTO) private int userid; private String email; private String password; public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getUserid() { return userid; } public void setUserid(int userid) { this.userid = userid; } }
package dao; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.SessionFactory; public class HibernateUtil { private static final SessionFactory sessionFactory; static { try { sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); } catch (Throwable ex) { System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return sessionFactory; } }
package dao; import entities.User; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; public class UserDAO { public static boolean registerUser(User u) { SessionFactory sf = HibernateUtil.getSessionFactory(); Transaction t = null; try { Session s = sf.openSession(); t = s.beginTransaction(); // start a new transaction s.persist(u); t.commit(); // commit transaction return true; } catch(Exception ex) { System.err.println("Error -->" + ex.getMessage()); if ( t!=null) t.rollback(); // rollback transaction on exception return false; } } }
package actions; import dao.UserDAO; import entities.User; public class RegisterAction { private String email,pwd; public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public RegisterAction() { } public String execute() throws Exception { // create an object of User entity User u = new User(); u.setEmail(email); u.setPassword(pwd); if ( UserDAO.registerUser(u)) // Access persistence tier through DAO return "success"; else return "fail"; } }
<%@ page contentType="text/html; charset=UTF-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>User Registration </title> </head> <body> <h2>User Registration</h2> <s:form action="register"> <s:textfield name="email" label="Enter email address : "/> <s:password name="pwd" label="Enter your password : " /> <s:submit value="Register" /> </s:form> </body> </html>
<%@page contentType="text/html" pageEncoding="UTF-8"%> <html> <body> <h1>Registration Completed Successfully!</h1> </body> </html>
<%@page contentType="text/html" pageEncoding="UTF-8"%> <html> <body> <h1>Registration Failed!</h1> <a href="registeruser.jsp">Try Again </a> </body> </html>
struts2hibernate registeruser.jsp registercomplete.jsp registerfail.jsp WEB-INF web.xml classes hibernate.cfg.xml struts.xml entities User.class dao HibernateUtil.class UserDAO.class actions RegisterAction.class lib oracle driver struts2 related jar files Hibernate related jar files