Skip to main content

Code for Register and Login using hibernate and struts and spring


Code for  the Login Page and the validation:

1)Registration.java

   package com.pojo;

public class Registration {
private int id;


public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
private String Name;

public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
private String email;
private String password;
private String mobile;

public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}

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;
}

}

2)Registration.hbm


<?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">

          <hibernate-mapping>
          <class name="com.pojo.Registration" table="registration">
       
          <id name="id">
         
          <generator class="increment"></generator>
          </id>
          <property name="Name">
           
          </property>
          <property name="email">
         
          </property>
          <property name="password">
         
          </property>
          <property name="mobile">
         
          </property>
          </class>
       
          </hibernate-mapping>


DAO Classes

3)ConnectionDao.java

package com.dao;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.Query;

import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;

import com.pojo.Registration;



public class ConnectionDAO {

Configuration con = new Configuration().configure("hibernate.cfg.xml");
SessionFactory sf = con.buildSessionFactory();
Session session;


public int checkmail(String em)
{


String l="from Registration m where m.email="+"'"+em+"'";
session=sf.openSession();
Query q=session.createQuery(l);
List<Registration> k = q.list();

return k.size();

}

public String register(int id,String Name,String mobile,String email, String password) {

String s2;
session=sf.openSession();
Transaction tx = session.getTransaction();
try {
tx.begin();
Registration pojo = new Registration();
pojo.setId(id);
pojo.setName(Name);
pojo.setMobile(mobile);
pojo.setEmail(email);
pojo.setPassword(password);
session.save(pojo);
tx.commit();
session.close();

} catch (Exception e) {
s2="not inserted";
}

s2="Inserted";
return s2;

}

public boolean checkforLogin(String email,String password){


session=sf.openSession();
Transaction tx = session.getTransaction();
tx.begin();

String SQL_QUERY = ("from Registration s where (s.email='"+ email +"') AND s.password='"+ password +"' ");
Query query=session.createQuery(SQL_QUERY);

List<Registration> list = query.list();

ServletContext ct = ServletActionContext.getServletContext();
Registration r = list.get(0);

String s = r.getName();
ct.setAttribute("user", s);

if (list.size() > 0) {
            session.close();
            return true;
        }
        session.close();
        return false;



}
}

Action Class:
Create a package for the action class

3) LoginAction:
package com.action;


import javax.servlet.ServletRequest;

import org.apache.struts2.ServletActionContext;

import com.dao.ConnectionDAO;
import com.opensymphony.xwork2.ActionSupport;

public class Loginaction extends ActionSupport {
private String email;

public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}

private String password;

public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

public  String execute() throws Exception{
ConnectionDAO con = new ConnectionDAO();
boolean f=con.checkforLogin(email,password);
if(f==true){
return SUCCESS;
}
else
{
ServletRequest ct = ServletActionContext.getRequest();
String s3 = "Wrong details";
ct.setAttribute("err", s3);
return ERROR;
}
}

}

4)Registeraction.java

package com.action;


import javax.servlet.ServletRequest;

import org.apache.struts2.ServletActionContext;



import com.dao.ConnectionDAO;

import com.opensymphony.xwork2.ActionSupport;


@SuppressWarnings("serial")
public class Registeraction extends ActionSupport {


private int id;


public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}

private String Name,email,mobile,password;

public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String execute() throws Exception
{
String status;
ConnectionDAO con = new ConnectionDAO();
int j=con.checkmail(email);
if(j<=0)
{



status=con.register(id,Name,  mobile, email, password);
if (status.equals("Inserted"))
{
ServletRequest ct = ServletActionContext.getRequest();

String s3 = "Thankyou for Registration";
ct.setAttribute("err", s3);
return "success";

}
else
{
return "error";

}

}
ServletRequest ct = ServletActionContext.getRequest();

String s3 = "your email aready in DB";
ct.setAttribute("err", s3);
return "error";
}


}



Configuration file:

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 Eclipse Hibernate Tools.                   -->
<hibernate-configuration>

    <session-factory>
       <!-- Related to the connection START -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/ganesh</property>
<property name="connection.user">root</property>
<property name="connection.password">root</property>
<!-- Related to the connection END -->

<!-- Related to hibernate properties START -->
<property name="show_sql">true </property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect </property>
<property name="hbm2ddl.auto">update</property>
<!-- Related to hibernate properties END -->

<!-- Related to mapping START -->
<mapping resource="com/pojo/Registration.hbm.xml"/>
<!-- <mapping resource="Login.hbm.xml"/> -->

<!-- Related to the mapping END -->
 
    </session-factory>

</hibernate-configuration>




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.0.dtd">
<struts>

<include file="struts-default.xml"/>
   <package name="a" extends="struts-default">
 
<action name="register" class="com.action.Registeraction">
<result name="success">index.jsp</result>
<result name="error">index.jsp</result>
</action>


<action name="abcd" class="com.action.Loginaction">
<result name="success">homepage.jsp</result>
<result name="error">index.jsp</result>

</action>

</package>
</struts> 

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>struts_hibernate_integration</display-name>
  <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>


Jsp File:

1)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>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
error**********
</body>
</html>




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...

Top Most Important Multithreading interview question and answers

1) What is Thread in java? Threads consumes CPU in best possible manner, hence enables multi processing. Multi threading reduces idle time of CPU which improves performance of application. Thread are light weight process. It belongs to java.lang package. It can run mutiple threads simutaneously .                                                               2) How to implements threads in java?         By   implementing java.lang.Runnable interface or extending java.lang.Thread class and then extending run method.   Thread creation by implementing java.lang.Runnable interface.      And then create Thread object by calling constructor and passing reference of Runnable interface i.e. runnable object          Thread threa...

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(HttpS...