CREATE TABLE `login_master` (
`login_id` int(10) NOT NULL,
`login_name` varchar(30) NOT NULL,
`login_password` varchar(30) NOT NULL,
`hint_password` varchar(50) NOT NULL,
`temp` varchar(30) NOT NULL,
PRIMARY KEY (`login_id`)
) ENGINE=MEMORY DEFAULT CHARSET=latin1;
Login.jsp
<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<div id="login">
<s:form action="Login" id="form1" method="post">
<h2>
<p><s:textfield name="username" label="username" id="textfield" /></p>
<br>
<p><s:password name="password" label="password" id="textfield2"></s:password></p>
</h2>
<s:submit value="Authentification" />
</s:form> <s:if test="hasActionErrors()">
<s:div id="errror1" cssStyle="color:red ">
<p><s:actionerror /></p>
<br>
</s:div>
</s:if> <br clear="all" /></body>
</html>
struts.xml
<?xml version="1.0" encoding="iso-8859-1"?> <!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="Login" class="Action.LoginAction">
<result name="Home">Login.jsp</result>
<result name="success">Home.html</result>
<result name="input">Login.jsp</result>
</action>
</package>
</struts>
Action.java
package Action;
import java.util.Map;
import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.SessionAware;
import DAO.Logindao;
import bean.Loginbean;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class LoginAction extends ActionSupport implements ModelDriven<Loginbean>,SessionAware{
/**
*
*/
private static final long serialVersionUID = 1L;
Logger logger = Logger.getLogger(LoginAction.class);
Loginbean bean=new Loginbean();
private Map<String, Object> session = null;
Logindao dao=new Logindao();
boolean isValidat;
public String execute()
{
if(dao.find(bean.getUsername(),bean.getPassword()))
return "success";
else
this.addActionError("The username or password is incorrect.");
return "Home";
}
public String Logout()
{
return "Logout";
}
public Loginbean getModel()
{
return bean;
}
public void setSession(Map<String, Object> session) {
this.session = session;
}
public Map<String, Object> getSession() {
return session;
}
}
Loginbean.java
package bean;
public class Loginbean {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Logindao.java
package DAO;
import java.util.Iterator;
import java.util.List;
import master.LoginMaster;
import org.hibernate.Query;
import org.hibernate.Session;
import util.HibernateUtil;
public class Logindao{
Session session;
@SuppressWarnings("unchecked")
public boolean find(String name, String password) {
session = HibernateUtil.getSession();
String SQL_QUERY= " from LoginMaster u where u.loginName='" + name + "' and u.loginPassword='" + password + "'";
System.out.println(SQL_QUERY);
Query query = session.createQuery(SQL_QUERY);
Iterator<LoginMaster> it = query.iterate();
List<LoginMaster> list = query.list();
if (list.size() > 0) {
session.close();
return true;
}
session.close();
return false;
}
}
HibernateUtil.java
package util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static SessionFactory sessionFactory;
public static void createSessionFactory() {
sessionFactory = new Configuration().configure().buildSessionFactory();
System.out.println("Value of sessionFactory is==="+sessionFactory);
}
public static Session getSession() {
return sessionFactory.openSession();
}
}
Struts2dispatcher.java
package util;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter;
import org.hibernate.HibernateException;
public class Struts2dispatcher extends StrutsPrepareAndExecuteFilter{
public void init(FilterConfig filterConfig) throws ServletException {
super.init(filterConfig);
try {
HibernateUtil.createSessionFactory();
System.out.println("application initializing successfully");
} catch (HibernateException e) {
throw new ServletException(e);
}
}
}
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">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:330/LoginMasterdatabase</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">validate</property
<mapping resource="master/LoginMaster.hbm.xml" />
</session-factory>
</hibernate-configuration>
LoginMaster.java
package master;
/**
* LoginMaster generated by hbm2java
*/
public class LoginMaster implements java.io.Serializable {
private int loginId;
private String loginName;
private String loginPassword;
private String hintPassword;
private String temp;
public LoginMaster() {
}
public LoginMaster(int loginId, String loginName, String loginPassword,
String hintPassword, String temp) {
this.loginId = loginId;
this.loginName = loginName;
this.loginPassword = loginPassword;
this.hintPassword = hintPassword;
this.temp = temp;
}
public int getLoginId() {
return this.loginId;
}
public void setLoginId(int loginId) {
this.loginId = loginId;
}
public String getLoginName() {
return this.loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public String getLoginPassword() {
return this.loginPassword;
}
public void setLoginPassword(String loginPassword) {
this.loginPassword = loginPassword;
}
public String getHintPassword() {
return this.hintPassword;
}
public void setHintPassword(String hintPassword) {
this.hintPassword = hintPassword;
}
public String getTemp() {
return this.temp;
}
public void setTemp(String temp) {
this.temp = temp;
}
}
`login_id` int(10) NOT NULL,
`login_name` varchar(30) NOT NULL,
`login_password` varchar(30) NOT NULL,
`hint_password` varchar(50) NOT NULL,
`temp` varchar(30) NOT NULL,
PRIMARY KEY (`login_id`)
) ENGINE=MEMORY DEFAULT CHARSET=latin1;
Login.jsp
<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<div id="login">
<s:form action="Login" id="form1" method="post">
<h2>
<p><s:textfield name="username" label="username" id="textfield" /></p>
<br>
<p><s:password name="password" label="password" id="textfield2"></s:password></p>
</h2>
<s:submit value="Authentification" />
</s:form> <s:if test="hasActionErrors()">
<s:div id="errror1" cssStyle="color:red ">
<p><s:actionerror /></p>
<br>
</s:div>
</s:if> <br clear="all" /></body>
</html>
struts.xml
<?xml version="1.0" encoding="iso-8859-1"?> <!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="Login" class="Action.LoginAction">
<result name="Home">Login.jsp</result>
<result name="success">Home.html</result>
<result name="input">Login.jsp</result>
</action>
</package>
</struts>
Action.java
package Action;
import java.util.Map;
import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.SessionAware;
import DAO.Logindao;
import bean.Loginbean;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class LoginAction extends ActionSupport implements ModelDriven<Loginbean>,SessionAware{
/**
*
*/
private static final long serialVersionUID = 1L;
Logger logger = Logger.getLogger(LoginAction.class);
Loginbean bean=new Loginbean();
private Map<String, Object> session = null;
Logindao dao=new Logindao();
boolean isValidat;
public String execute()
{
if(dao.find(bean.getUsername(),bean.getPassword()))
return "success";
else
this.addActionError("The username or password is incorrect.");
return "Home";
}
public String Logout()
{
return "Logout";
}
public Loginbean getModel()
{
return bean;
}
public void setSession(Map<String, Object> session) {
this.session = session;
}
public Map<String, Object> getSession() {
return session;
}
}
Loginbean.java
package bean;
public class Loginbean {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Logindao.java
package DAO;
import java.util.Iterator;
import java.util.List;
import master.LoginMaster;
import org.hibernate.Query;
import org.hibernate.Session;
import util.HibernateUtil;
public class Logindao{
Session session;
@SuppressWarnings("unchecked")
public boolean find(String name, String password) {
session = HibernateUtil.getSession();
String SQL_QUERY= " from LoginMaster u where u.loginName='" + name + "' and u.loginPassword='" + password + "'";
System.out.println(SQL_QUERY);
Query query = session.createQuery(SQL_QUERY);
Iterator<LoginMaster> it = query.iterate();
List<LoginMaster> list = query.list();
if (list.size() > 0) {
session.close();
return true;
}
session.close();
return false;
}
}
HibernateUtil.java
package util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static SessionFactory sessionFactory;
public static void createSessionFactory() {
sessionFactory = new Configuration().configure().buildSessionFactory();
System.out.println("Value of sessionFactory is==="+sessionFactory);
}
public static Session getSession() {
return sessionFactory.openSession();
}
}
Struts2dispatcher.java
package util;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter;
import org.hibernate.HibernateException;
public class Struts2dispatcher extends StrutsPrepareAndExecuteFilter{
public void init(FilterConfig filterConfig) throws ServletException {
super.init(filterConfig);
try {
HibernateUtil.createSessionFactory();
System.out.println("application initializing successfully");
} catch (HibernateException e) {
throw new ServletException(e);
}
}
}
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">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:330/LoginMasterdatabase</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">validate</property
<mapping resource="master/LoginMaster.hbm.xml" />
</session-factory>
</hibernate-configuration>
LoginMaster.java
package master;
/**
* LoginMaster generated by hbm2java
*/
public class LoginMaster implements java.io.Serializable {
private int loginId;
private String loginName;
private String loginPassword;
private String hintPassword;
private String temp;
public LoginMaster() {
}
public LoginMaster(int loginId, String loginName, String loginPassword,
String hintPassword, String temp) {
this.loginId = loginId;
this.loginName = loginName;
this.loginPassword = loginPassword;
this.hintPassword = hintPassword;
this.temp = temp;
}
public int getLoginId() {
return this.loginId;
}
public void setLoginId(int loginId) {
this.loginId = loginId;
}
public String getLoginName() {
return this.loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public String getLoginPassword() {
return this.loginPassword;
}
public void setLoginPassword(String loginPassword) {
this.loginPassword = loginPassword;
}
public String getHintPassword() {
return this.hintPassword;
}
public void setHintPassword(String hintPassword) {
this.hintPassword = hintPassword;
}
public String getTemp() {
return this.temp;
}
public void setTemp(String temp) {
this.temp = temp;
}
}
No comments:
Post a Comment
Comment