Monday, July 9, 2018

Signup and Login using JSP

NOTE:

Please create a database inside MYSQL server before using this code

to create a database
-> create database testdb;

to take this database in use
-> use testdb;

to create a table inside testdb database
-> create table user_details(name varchar(100),email varchar(100),password varchar(100),mobile varchar(15),information varchar(255),primary key(email));

note: email is primary key, so it must be unique and also not null

note: here database name is testdb and table-name is user_details

note: please create a dynamic-web-project in eclipse ide and create six (ie. 6)  jsp inside WebContent folder, names of jsp must be following

1. index.jsp
2. signup.jsp
3. signup-handler.jsp
4. login.jsp
5. login-handler.jsp
6. welcome.jsp  

note: also copy and paste mysqlconnector.jar file inside the lib folder (lib folder is present inside WEB-INF folder)


 # #  this is the code for index.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>

<a href="signup.jsp">to signup click here</a>

<br>

<a href="login.jsp">to login click here</a>

</body>
</html>


  ##  this is the code for signup.jsp ##

<html>
<head>
<title>
SIGNUP
</title>
</head>
<fieldset>
<legend>signup-form</legend>
<form action="signup-handler.jsp" 
method="post">

Name
<br>
<input type="text" name="h_name" required>
<br>

Email
<br>
<input type="email" name="h_email" required>
<br>

Password
<br>
<input type="password" name="h_pass" required>
<br>

Mobile
<br>
<input type="text" name="h_mobile" required>
<br>

Tell something about you
<br>
<textarea name="h_info" rows="10" cols="50">
</textarea>

<br>

<input type="submit" value="signup">

</form>
</fieldset>
</body>
</html>


  ##  this is the code for signup-handler.jsp ##

<%@page import="java.sql.Statement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ 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>signup</title>
</head>
<body>
<%
// fetch html form data and store inside some variables
String name = request.getParameter("h_name");
String email = request.getParameter("h_email");
String pass = request.getParameter("h_pass");
String mobile = request.getParameter("h_mobile");
String info = request.getParameter("h_info");

// ## JDBC CODE START FROM HERE ##

// load driver class
Class.forName("com.mysql.jdbc.Driver");

// get connection
Connection co = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb","root","root");

// get statement
Statement st = co.createStatement();

// create an sql statement
String sqlQuery = "insert into user_details values('"+name+"','"+email+"','"+pass+"','"+mobile+"','"+info+"')";

// execute sql statement
int nora = st.executeUpdate(sqlQuery);

// if no of records are more than 1, means data has been inserted inside table
// nora means Number Of Records Affected
if(nora > 0)
{
// show data at client side (bole to web browser)
out.println("<b><i style='color : red'>your data has been saved inside our database</i></b><br>");
out.println("<a href='login.jsp'>click here to login</a>");
}

// close the connection
co.close();

// ## JDBC CODE ENDS HERE ##
%>
</body>
</html>


  ##  this is the code for login.jsp ##

<html>
<head>
<title>
LOGIN
</title>
</head>
<fieldset>
<legend>login-form</legend>
<form action="login-handler.jsp" 
method="post">

Email
<br>
<input type="email" name="h_email" required>
<br>

Password
<br>
<input type="password" name="h_pass" required>
<br>

<input type="submit" value="login">

</form>
</fieldset>
</body>
</html>




  ##  this is the code for login-handler.jsp ##

<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ 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>login</title>
</head>
<body>
<%
// fetch html form data and store inside some variables
String email = request.getParameter("h_email");
String pass = request.getParameter("h_pass");

// ## JDBC CODE START FROM HERE ##

// load driver class
Class.forName("com.mysql.jdbc.Driver");

// get connection
Connection co = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb","root","root");

// get statement
Statement st = co.createStatement();

// create an sql statement
String sqlQuery = "select * from user_details where email='"+email+"' and password='"+pass+"'";

// execute sql statement and get result-set
ResultSet rs = st.executeQuery(sqlQuery);

// since we know that email is a primary key, so there will be only 1 record inside result-set
// if email is correct otherwise the result-set will have no record
// so check if result-set has a record or not
if(rs.next()) // means result-set has a record
{
// show data at client side
out.println("<b style='color : blue'><i>your data is available inside our records</i></b>");

// show a hyperlink
out.println("<br> <a href='profile.jsp'>procced after login</a>");
}
else // there is no record inside result-set
{
// show data at client side
out.println("<b style='color : red'><i>your data is not available inside our records</i></b>");

// show a hyperlink
out.println("<br> <a href='index.jsp'>click here for home page</a>");
}

// close the connection
co.close();

// ## JDBC CODE ENDS HERE ##
%>
</body>
</html>



  ##  this is the code for 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>profile</title>
</head>
<body>
<center>
<h3 style="color : red">
welcome user
</h3>
</center>
</body>
</html>


16 comments: