Wednesday, 8 April 2020
Tuesday, 7 April 2020
Registartion and Login using Servlet, jsp using MVC pattern and with MYSQL database
Hello coder!!!
Here, we are going to create simple dynamic web app to create Registration and Login form in servlet and jsp. We have used MySql database and this whole project follows MVC pattern.
Following are the required files in this application.
Model
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Registration</title>
</head>
<body>
<form action="./RegisterServlet" method="post">
UserName:<input type="text" name="name" placeholder="Enter Your Name"/><br/>
Email:<input type="email" name="email" placeholder="Enter Your Email"/><br/>
Password:<input type="password" name="password" placeholder="Enter Your Password"/><br/>
Mobile:<input type="tel" name="mobile" placeholder="Enter Your Mobile" /><br/>
<input type="submit" value="Register"/><br/>
Already a User? <Button type="submit" formaction="Login.jsp" >Login</Button>
</form>
</body>
</html>
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="./LoginServlet" method="post">
Email:<input type="email" name="email" placeholder="Enter Your Email"/><br/>
Password:<input type="password" name="password" placeholder="Enter Your Password"/><br/>
<input type="submit" value="Login"/><br/>
New User? <Button type="submit" formaction="Registeration.jsp" >Register</Button>
</form>
</body>
</html>
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Home</title>
</head>
<body>
<% Object name=session.getAttribute("Name"); %>
<%= "Welcome " + name %>
</body>
</html>
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.javaeexpress.model.UserBean;
import com.javaeexpress.model.UserDao;
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name,email,password,mobile;
name=request.getParameter("name");
email=request.getParameter("email");
password=request.getParameter("password");
mobile=request.getParameter("mobile");
UserBean registerbean=new UserBean();
registerbean.setName(name);
registerbean.setEmail(email);
registerbean.setPassword(password);
registerbean.setMobile(mobile);
UserDao rgdao=new UserDao();
String userRegistered=rgdao.registerUser(registerbean);
if(userRegistered.equals("Success"))
{
request.getSession().setAttribute("Name", name);
request.getRequestDispatcher("./Home.jsp").forward(request, response);
}
else
{
request.getRequestDispatcher("./Registration.jsp").forward(request, response);
}
}
}
public class UserBean {
private String name;
private String email;
private String mobile;
private String password;
public UserBean()
{
}
public UserBean(String name, String email, String mobile, String password) {
super();
this.name = name;
this.email = email;
this.mobile = mobile;
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.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;
}
}
import java.sql.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.catalina.core.ApplicationContext;
import com.javaeexpress.database.DBConnection;
public class UserDao {
public String registerUser(UserBean registerbean)
{
String name=registerbean.getName();
String email=registerbean.getEmail();
String password=registerbean.getPassword();
String mobile=registerbean.getMobile();
Connection con=null;
PreparedStatement ps=null;
PreparedStatement ps1=null;
try {
con=DBConnection.createconnection();
String createQuery="create table if not exists Registration (name varchar(20),email varchar(20),password varchar(20),mobile varchar(20))";
String inertQuery="insert into Registration (name,email,password,mobile) values (?,?,?,?)";
ps=con.prepareStatement(createQuery);
@SuppressWarnings("unused")
int i=ps.executeUpdate();
ps1=con.prepareStatement(inertQuery);
ps1.setString(1, name);
ps1.setString(2, email);
ps1.setString(3, password);
ps1.setString(4, mobile);
int res=ps1.executeUpdate();
if(res!=0)
{
return "Success";
}
}catch (Exception e) {
e.printStackTrace();
}
return "Error 404";
}
public String validateUser(UserBean regBean)
{
String email=regBean.getEmail();
String password=regBean.getPassword();
String name;
Connection con=null;
PreparedStatement ps=null;
try {
con=DBConnection.createconnection();
String selectQuery="select name from Registration where email=? and password=?";
ps=con.prepareStatement(selectQuery);
ps.setString(1, email);
ps.setString(2, password);
ResultSet rs=ps.executeQuery();
while(rs.next())
{
if(rs!=null)
{
name=rs.getString("name");
regBean.setName(name);
}
return "Success";
}
}catch (Exception e) {
e.printStackTrace();
}
return "Failed!!";
}
}
Still any Queries feel free to comment down...
Here, we are going to create simple dynamic web app to create Registration and Login form in servlet and jsp. We have used MySql database and this whole project follows MVC pattern.
Following are the required files in this application.
Model
- UserBean.java
- UserDao.java
- DBConnection.java
- RegisterServlet.java
- LoginServlet.java
- Registartion.jsp
- Login.jsp
- Home.jsp
Project Directory
We assume that you have basic knowledge of setting up of project in eclipse and configuring Tomcat server.
let's start...
Registeration.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Registration</title>
</head>
<body>
<form action="./RegisterServlet" method="post">
UserName:<input type="text" name="name" placeholder="Enter Your Name"/><br/>
Email:<input type="email" name="email" placeholder="Enter Your Email"/><br/>
Password:<input type="password" name="password" placeholder="Enter Your Password"/><br/>
Mobile:<input type="tel" name="mobile" placeholder="Enter Your Mobile" /><br/>
<input type="submit" value="Register"/><br/>
Already a User? <Button type="submit" formaction="Login.jsp" >Login</Button>
</form>
</body>
</html>
Login.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="./LoginServlet" method="post">
Email:<input type="email" name="email" placeholder="Enter Your Email"/><br/>
Password:<input type="password" name="password" placeholder="Enter Your Password"/><br/>
<input type="submit" value="Login"/><br/>
New User? <Button type="submit" formaction="Registeration.jsp" >Register</Button>
</form>
</body>
</html>
Home.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Home</title>
</head>
<body>
<% Object name=session.getAttribute("Name"); %>
<%= "Welcome " + name %>
</body>
</html>
RegisterServlet.java
package com.javaeexpress.controller;import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.javaeexpress.model.UserBean;
import com.javaeexpress.model.UserDao;
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name,email,password,mobile;
name=request.getParameter("name");
email=request.getParameter("email");
password=request.getParameter("password");
mobile=request.getParameter("mobile");
UserBean registerbean=new UserBean();
registerbean.setName(name);
registerbean.setEmail(email);
registerbean.setPassword(password);
registerbean.setMobile(mobile);
UserDao rgdao=new UserDao();
String userRegistered=rgdao.registerUser(registerbean);
if(userRegistered.equals("Success"))
{
request.getSession().setAttribute("Name", name);
request.getRequestDispatcher("./Home.jsp").forward(request, response);
}
else
{
request.getRequestDispatcher("./Registration.jsp").forward(request, response);
}
}
}
LoginServlet.java
package com.javaeexpress.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.javaeexpress.model.UserBean;
import com.javaeexpress.model.UserDao;
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String email=request.getParameter("email");
String password=request.getParameter("password");
UserBean regbean=new UserBean();
regbean.setEmail(email);
regbean.setPassword(password);
UserDao regdao=new UserDao();
String check=regdao.validateUser(regbean);
if(check.equals("Success"))
{
String userName=regbean.getName();
request.getSession().setAttribute("Name",userName);
request.getRequestDispatcher("./Home.jsp").forward(request, response);
}
else
{
request.getRequestDispatcher("./Login.jsp").forward(request, response);
}
}
}
UserBean.java
package com.javaeexpress.model;public class UserBean {
private String name;
private String email;
private String mobile;
private String password;
public UserBean()
{
}
public UserBean(String name, String email, String mobile, String password) {
super();
this.name = name;
this.email = email;
this.mobile = mobile;
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.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;
}
}
UserDao.java
package com.javaeexpress.model;import java.sql.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.catalina.core.ApplicationContext;
import com.javaeexpress.database.DBConnection;
public class UserDao {
public String registerUser(UserBean registerbean)
{
String name=registerbean.getName();
String email=registerbean.getEmail();
String password=registerbean.getPassword();
String mobile=registerbean.getMobile();
Connection con=null;
PreparedStatement ps=null;
PreparedStatement ps1=null;
try {
con=DBConnection.createconnection();
String createQuery="create table if not exists Registration (name varchar(20),email varchar(20),password varchar(20),mobile varchar(20))";
String inertQuery="insert into Registration (name,email,password,mobile) values (?,?,?,?)";
ps=con.prepareStatement(createQuery);
@SuppressWarnings("unused")
int i=ps.executeUpdate();
ps1=con.prepareStatement(inertQuery);
ps1.setString(1, name);
ps1.setString(2, email);
ps1.setString(3, password);
ps1.setString(4, mobile);
int res=ps1.executeUpdate();
if(res!=0)
{
return "Success";
}
}catch (Exception e) {
e.printStackTrace();
}
return "Error 404";
}
public String validateUser(UserBean regBean)
{
String email=regBean.getEmail();
String password=regBean.getPassword();
String name;
Connection con=null;
PreparedStatement ps=null;
try {
con=DBConnection.createconnection();
String selectQuery="select name from Registration where email=? and password=?";
ps=con.prepareStatement(selectQuery);
ps.setString(1, email);
ps.setString(2, password);
ResultSet rs=ps.executeQuery();
while(rs.next())
{
if(rs!=null)
{
name=rs.getString("name");
regBean.setName(name);
}
return "Success";
}
}catch (Exception e) {
e.printStackTrace();
}
return "Failed!!";
}
}
DBConnection.java
package com.javaeexpress.database;
import java.sql.*;
public class DBConnection {
public static Connection createconnection(){
Connection con = null;
String path="jdbc:mysql://localhost:3306/hotelmanagement?useTimezone=true&serverTimezone=UTC";
String dbusername="root";
String dbpass="";
try {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
con=DriverManager.getConnection(path,dbusername,dbpass);
}catch(Exception e)
{
e.printStackTrace();
}
return con;
}
}
Output
Still any Queries feel free to comment down...
Location:
India
Subscribe to:
Posts (Atom)