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-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file>head.jsp</welcome-file>
</welcome-file-list>
</web-app>
Comments
Post a Comment