Here you will find a variety of videos on technology, Tutorials, News, Tip And Tricks, Reviews

Breaking

Post Top Ad

Your Ad Spot

Thursday, July 2, 2015

Create Register Page using JSP

Here you can learn how to insert data to MySQL using JSP.



SQL query to create Table.



CREATE TABLE `User` (
  `User_ID` int(10) unsigned NOT NULL auto_increment,
  `First_Name` varchar(45) NOT NULL,
  `Last_Name` varchar(45) NOT NULL,
  `Email_ID` varchar(45) NOT NULL,
  `User_Mobile` varchar(45) NOT NULL,
  `Password` varchar(45) NOT NULL,
  PRIMARY KEY  (`User_ID`)
)






In this application, the user first name , last name, email_id, mobile number and password, will store in a database.


register.jsp


 <!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>
<div class="container">
<section id="content">
<form action="/samplepro/registerpage">
<h1>Create an Account</h1>

<input type="text" placeholder="First Name" required="" id="FirstName" name="FirstName" />

<div>
<input type="text" placeholder="Last Name" required="" id="LastName" name="LastName" />
</div>
<div>
<input type="text" placeholder="Email_ID" required="" id="Email_ID" name="Email_ID" />
</div>
<div>
<input type="text" placeholder="Mobile No." required="" id="Mobile" name="Mobile" />
</div>
<div>
<input type="password" placeholder="Password" required="" id="password" name="password"/>
</div>
<div>
<input type="submit" value="Submit" />
<a href="#">Login</a>
</div>
</form><!-- form -->

</section><!-- content -->
</div><!-- container -->


</body>
</html>  


After entering the user first name, last name, Email_id, mobile number, password, the registerpage.jsp page is called to handle the request. After a user submits a request, the registerpage.jsp page is used to generate the appropriate response for each request made.

registerpage.jsp




 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"  pageEncoding="ISO-8859-1"%>
 <%@page import="java.util.ArrayList"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<!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>
<%
Connection con=null;
PreparedStatement ps=null;
Statement stmt=null;
ResultSet rs=null;
int flag=0;
try
{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/gateschema","root","root");
System.out.println("con="+con);
}
catch(Exception e)
{
e.printStackTrace();
}
System.out.println("welcome");
//PrintWriter out=response.getWriter();
String Id = null;
String FirstName=request.getParameter("FirstName");
String LastName=request.getParameter("LastName");
String Email_ID=request.getParameter("Email_ID");
String Mobile=request.getParameter("Mobile");
String password=request.getParameter("password");


// HttpSession session=request.getSession();
session.setAttribute("fname",FirstName);
session.setAttribute("lname",LastName);

try
{
ps=con.prepareStatement("insert into user(User_ID, First_Name, Lasr_Name, Email_ID, User_Mobile, Password) values(?,?,?,?," + "?,?)");
ps.setString(1,  null);
ps.setString(2, FirstName);
ps.setString(3, LastName);
ps.setString(4, Email_ID);
ps.setString(5, Mobile);
ps.setString(6, password);

int n=ps.executeUpdate();
flag=0;
}
catch (Exception e)
{
e.printStackTrace();
}

if(flag==1)
{
RequestDispatcher rd=request.getRequestDispatcher("register.jsp");
rd.forward(request, response);
}
else
{
RequestDispatcher rd=request.getRequestDispatcher("Welcome.jsp");
rd.forward(request, response);
}
%>
</body>
</html>


The registerpage.jsp page also includes another JSP page, named Welcome.jsp, which is used to display the currently registered user information.



Welcome.jsp




<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
     <%@page import="java.util.ArrayList"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<!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>
<%
    if ((session.getAttribute("Email") == null) || (session.getAttribute("Email") == "")) {
%>
You are not logged in<br/>
<a href="Login.jsp">Please Login</a>
<%} else
{
%>
<% String n=(String)session.getAttribute("fname");
String l=(String)session.getAttribute("lname");
out.print("Welcome "+n + " " +l);
//out.close();
%>
<br><a href='Logout.jsp'>Log out</a><br>
<%
    }
%>



</body>
</html>

The Welcome.jsp page includes another JSP page, name Logout.jsp, which is used to logout the user.


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

<%

out.print("you have successfully Logout");
session.setAttribute("Email", null);
session.invalidate();
response.sendRedirect("Index.jsp");
%>
</body>
</html>


When we run above application, we get response like below images.


Screenshots:






After entering the details and clicking the Submit button stores the all additional information, which includes the first name, last name, email_id, mobile number, password, in the database. After clicking button, you are directed to the Welcome.jsp page.



Database: User



User_IDFirst_NameLasr_NameEmail_IDUser_MobilePassword
1anniejambhulkarannie.jambhulkar@gmail.com9999999999annie123
2AnayagadgeAnaya.gadge@gmail.com8888888888anaya123
3Ashwingadgeashwin@gmail.com9999999999pank
3neetakumbhareneeta@gmail.com9898877777nik





Post Top Ad

Your Ad Spot

Pages