Skip to main content

Example for Registration using hibernate integration with struts2 and spring



First create a Pojo class for the Registeration form:

    1)User.java
                 

  package com.gklife.pojo;

import com.gklife.RegisterDao.RegisterDao;

public class User {
private int id;
private String name;
private String Email;
private String mobilenumber;
private String 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 getEmail() {
return Email;
}
public void setEmail(String email) {
Email = email;
}
public String getMobilenumber() {
return mobilenumber;
}
public void setMobilenumber(String mobilenumber) {
this.mobilenumber = mobilenumber;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

public String execute(){
RegisterDao dao=new RegisterDao();
dao.saveUser(this);
return "success";


}
}

     
2) Register.dao



package com.gklife.RegisterDao;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;



import com.gklife.pojo.User;
import com.gklife.pojoclass.HibernatePlug;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class RegisterDao {
SessionFactory factory = HibernatePlug.getFactory();
    Session session = factory.openSession();

    public  int saveUser(User u){
        Transaction t=session.beginTransaction();
int i=(Integer)session.save(u);
t.commit();
session.close();
return i;
  }
@SuppressWarnings("unchecked")
public boolean find(String Email, String password) {
String SQL_QUERY = " from User u  where u.Email='" + Email + "' and u.password='" + password + "'";
System.out.println(SQL_QUERY);
        Query query = session.createQuery(SQL_QUERY);
         Iterator<User> it = query.iterate();
         List<User> list = query.list();
         System.out.println(list);
        if (list.size() > 0) {
            session.close();
            return true;
        }
        session.close();
        return false;
    }

@SuppressWarnings("unchecked")
public List<User> ret() {
List<User> l = new ArrayList<User>();
/*SessionFactory sf=new AnnotationConfiguration().configure().buildSessionFactory();*/
System.out.println("hghjhbjhbj");
Query q =session.createQuery("select u.name from User u");

l=q.list();

System.out.println("successs..................");

return l;
}

}



3)HibernatePlug.java



package com.gklife.pojoclass;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernatePlug {
private static SessionFactory factory = getSessionFactory();

    public static synchronized SessionFactory getSessionFactory()
    {
            try {                     

                                Configuration cfg = new Configuration();
                                cfg.configure("hibernate.cfg.xml");

                                SessionFactory sessionFactory = cfg.buildSessionFactory();
                                System.out.println(" ----------   Factory Object Created  ------------");
                                return sessionFactory;

                  }             catch (Throwable ex) {
                                 System.err.println("Initial SessionFactory creation failed." + ex);
                                 throw new ExceptionInInitializerError(ex);
                  }
      }

    public static SessionFactory getFactory() {
        return factory;
    }
}


4)Retriveuser.java


package com.gklife.pojoclass;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.gklife.RegisterDao.RegisterDao;
import com.gklife.pojo.User;

public class RetriveUser extends ActionSupport {
List<User> imgList = new ArrayList<User>();
public List<User> getImgList() {
return imgList;
}
public void setImgList(List<User> imgList) {
this.imgList = imgList;
}
@Override
public String execute() throws Exception {
System.out.println("kjshsjhsjjh");
RegisterDao cdao =new RegisterDao();
imgList=cdao.ret();
return "success";
}

}


5)SendMailAPI.java

package com.gklife.pojoclass;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendMailAPI {
public void sendMsg(String to, String subject, String body) {
  
  
  final String from ="kclinkmail@gmail.com";
  final String pass="kclink@123";
  String  html="<a href='http://localhost:8080/RegistrationExample1/head.jsp'>click here to register</a>";
  //Get the session object  
  Properties props = new Properties();  
  props.put("mail.smtp.host", "smtp.gmail.com");  
  props.put("mail.smtp.socketFactory.port", "465");  
  props.put("mail.smtp.socketFactory.class",  
            "javax.net.ssl.SSLSocketFactory");  
  props.put("mail.smtp.auth", "true");  
  props.put("mail.smtp.port", "465");  
   
  Session session = Session.getDefaultInstance(props,  
   new javax.mail.Authenticator() {  
   protected PasswordAuthentication getPasswordAuthentication() {  
   return new PasswordAuthentication(from,pass);//change accordingly  
   }  
  });  
   
  //compose message  
  try {  
   MimeMessage message = new MimeMessage(session);  
   message.setFrom(new InternetAddress(from));//change accordingly  
   message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));  
   message.setSubject(subject);  
   message.setText(body); 
   message.setContent(html+"<br>"+body, "text/html; charset=utf-8");
     
   //send message  
   Transport.send(message);  
  
   System.out.println("message sent successfully");  
   
  } catch (MessagingException e) {throw new RuntimeException(e);}  
 

}


}


6)UserLogin.java

package com.gklife.pojoclass;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendMailAPI {
public void sendMsg(String to, String subject, String body) {
  
  
  final String from ="kclinkmail@gmail.com";
  final String pass="kclink@123";
  String  html="<a href='http://localhost:8080/RegistrationExample1/head.jsp'>click here to register</a>";
  //Get the session object  
  Properties props = new Properties();  
  props.put("mail.smtp.host", "smtp.gmail.com");  
  props.put("mail.smtp.socketFactory.port", "465");  
  props.put("mail.smtp.socketFactory.class",  
            "javax.net.ssl.SSLSocketFactory");  
  props.put("mail.smtp.auth", "true");  
  props.put("mail.smtp.port", "465");  
   
  Session session = Session.getDefaultInstance(props,  
   new javax.mail.Authenticator() {  
   protected PasswordAuthentication getPasswordAuthentication() {  
   return new PasswordAuthentication(from,pass);//change accordingly  
   }  
  });  
   
  //compose message  
  try {  
   MimeMessage message = new MimeMessage(session);  
   message.setFrom(new InternetAddress(from));//change accordingly  
   message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));  
   message.setSubject(subject);  
   message.setText(body); 
   message.setContent(html+"<br>"+body, "text/html; charset=utf-8");
     
   //send message  
   Transport.send(message);  
  
   System.out.println("message sent successfully");  
   
  } catch (MessagingException e) {throw new RuntimeException(e);}  
}


}

Jsp page (Userview)

1)Welcome.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">
<%@ taglib uri="/struts-tags" prefix="S" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Welcome: <S:property value="name"/>  
</body>
</html>


2)Test.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html>
<html>
<head>
<title>Kclink Employee Registration</title>

</head>
<body>
<s:iterator value="imgList">

<table>
<!-- <tr>

<td colspan="1"><b>id</b></td>
<td colspan="1"><b>Name</b></td>
<td colspan="1"><b>Email</b></td>
<td colspan="1"><b>MovileNum</b></td>
<td colspan="1"><b>Password</b></td>
</tr> -->
<tr>
<td colspan="1"><s:property value="id" /></td>
<td colspan="2"><s:property value="name" /></td>
<td colspan="3"><s:property value="Email" /></td>
<td colspan="2"><s:property value="mobilenumber" /></td>
<td colspan="2"><s:property value="password" /></td>
</tr>

</table>
</s:iterator>


</body>
</html>


3)Success.jsp

    <%@ taglib prefix="s" uri="/struts-tags"%>

<h1>Iterator_Values</h1>

<%-- <s:iterator value="imgList">  --%>
<s:autocompleter list="imgList"></s:autocompleter>
<s:property value="name" />
<br>

<%-- </s:iterator> --%>
</body>
</html>


4)Viewfile.jsp

       <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib uri="/struts-tags" prefix="s"%>
<!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>Insert title here</title>
</head>
<body>
<s:form action="viewrecords">
   <s:submit value="Save&Continue"/> 
  
  </s:form>  
</body>
</html>


Login.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">
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<s:form action="login">
<s:textfield name="Email" label="Enter Your E-Mail"/><br/>
<s:password name="password" label="Password"/><br>
<s:submit value="LOGIN"/>
</s:form>
</body>
</html>

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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>RegistrationExample1</display-name>
- <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>
- <welcome-file-list>
  <welcome-file>head.jsp</welcome-file>
  </welcome-file-list>
  </web-app>











Comments

Popular posts from this blog

Commonly asked Top 26 Java Interview Questions

1.what is a transient variable? A transient variable is a variable that may not be serialized. If you don't want to serialize any objects then we have to use Transient Keyword. 2. Why do threads block on I/O? Threads block on i/o (that enters the waiting state) so that other threads may execute while the i/o Operation is performed. So your blocked thread of execution blocks only that: the thread of execution. 3. How are Observer and Observable used? Objects that subclass the Observable class maintain a list of observers. When an Observable object is updated it invokes the update() method of each of its observers to notify the observers that it has changed state. The Observer interface is implemented by objects that observe Observable objects(refer 1.4). 4. What is synchronization and why is it important?                      With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources. Withou

All difference Question in java(common difference) (Collections)

1)Difference Between Interfaces And Abstract class? All the methods declared in the Interface are Abstract,where as abstract class must have atleast one abstract method and others may be concrete. In abstract class keyword abstract must be used for method, where as in Interface we need not use the keyword for methods. Abstract class must have Sub class, where as Interface can’t have sub classes.  An abstract class can extend only one class, where as an Interface can extend more than one. 2)Difference between Arrays and Vectors?             Because Vectors are dynamically-allocated, they offer a relatively memory-efficient way of handling lists whose size could change drastically. Arrays are fixed in size and it's wastage of memory problem. A Vector might be a good choice for such an arrangement. Moreover, Vectors have some useful member functions that make coding simpler. While in array is the bad choice of the arrangement and the code become more complex. Vecto

Decode of the image file from hibernate and struts

Create a java file for the decoding the image Image.java package com.img.action; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import javax.imageio.ImageIO; import javax.servlet.ServletRequest; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import com.img.dao.ConnectionDAO; import com.img.pojo.Film_TablePojo; import com.img.pojo.imagePojo; import com.opensymphony.xwork2.ActionSupport; public class Show extends ActionSupport { List<Object> values = new ArrayList<Object>();  public List<Object> getValues() { return values; } public void setValues(List<Object> values) { this.values = values; } /*HttpServletRequest request;     public void setServletRequest(HttpServletRequest