Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Jsp JSP Login Form with MySQL Database Connection and back end validation

Related Tutorials


 
 

Share on Google+Share on Google+

JSP Login Form with MySQL Database Connection and back end validation

Advertisement
In this section we have discussed how to crate a login form using MySQL Database Connection and back end validation. Attached video tutorial will guide you step by step to create your own example.

Here we have created a simple login form using MySQL Database Connection and back end validation. We have also added a video tutorial of the program that will guide you in creating it. We will be using Eclipse IDE for compile and Tomcat 7 server for deploying the application.

The record will be fetched from database and matched with the input value given through the login form.

Example of creating the login form is given below with code and images:

We will create a simple login logout example using JSP. When a user inputs information in a form, it is validated with the record saved into the database table. So first we create a database table that will have dummy values inserted by us.

We have used JSP implicit object session and setAttribute() method for setting the attribute value in session and getAttribute() method for getting the attribute value from the same session. invalidate() method is used for ending the session.

Then other jsp pages like home.jsp, login.jsp, logout.jsp, welcome.jsp, error.jsp and index.jsp pages will be created.

Following video will help you to create JSP Login form backed with MySQL database:

The following image is of the Database table that shows the information added in it. It is with these values that the program checks the information added by the user and returns the result.

home.jsp

When compiling the program in eclipse, the home page will include the following code and the page will look like the one in the image. home.jsp page has the interface for the user to input and submit their records.

<%@ page import="java.sql.*" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Home</title>
</head>
<body>
<%
Connection con= null;
PreparedStatement ps = null;
ResultSet rs = null;

String driverName = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/record";
String user = "root";
String password = "root";

String sql = "select usertype from userdetail";

try {
Class.forName(driverName);
con = DriverManager.getConnection(url, user, password);
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
%>
<form method="post" action="login.jsp">
<center><h2 style="color:green">JSP Login Example</h2></center>
<table border="1" align="center">
<tr>
<td>Enter Your Name :</td>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<td>Enter Your Password :</td>
<td><input type="password" name="password"/></td>
</tr>
<tr>
<td>Select UserType</td>
<td><select name="usertype">
<option value="select">select</option>
<%
while(rs.next())
{
String usertype = rs.getString("usertype");
%>
<option value=<%=usertype%>><%=usertype%></option>
<%
}
}
catch(SQLException sqe)
{
out.println("home"+sqe);
}
%>
</select>
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="submit"/></td>
</table>
</form>
</body>
</html>

login.jsp

The login page in the Eclipse will have the following code and the page will look like the one in the image. login.jsp page is where the code for getting the input field values and the database table records is written. sql query is used to fetch the matched record from table.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login</title>
</head>
<body>
<%! String userdbName;
String userdbPsw;
String dbUsertype;
%>
<%
Connection con= null;
PreparedStatement ps = null;
ResultSet rs = null;

String driverName = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/record";
String user = "root";
String dbpsw = "root";

String sql = "select * from userdetail where name=? and password=? and usertype=?";

String name = request.getParameter("name");
String password = request.getParameter("password");
String usertype = request.getParameter("usertype");

if((!(name.equals(null) || name.equals("")) && !(password.equals(null) || password.equals(""))) && !usertype.equals("select"))
{
try{
Class.forName(driverName);
con = DriverManager.getConnection(url, user, dbpsw);
ps = con.prepareStatement(sql);
ps.setString(1, name);
ps.setString(2, password);
ps.setString(3, usertype);
rs = ps.executeQuery();
if(rs.next())
{
userdbName = rs.getString("name");
userdbPsw = rs.getString("password");
dbUsertype = rs.getString("usertype");
if(name.equals(userdbName) && password.equals(userdbPsw) && usertype.equals(dbUsertype))
{
session.setAttribute("name",userdbName);
session.setAttribute("usertype", dbUsertype);
response.sendRedirect("welcome.jsp");
}
}
else
response.sendRedirect("error.jsp");
rs.close();
ps.close();
}
catch(SQLException sqe)
{
out.println(sqe);
}
}
else
{
%>
<center><p style="color:red">Error In Login</p></center>
<%
getServletContext().getRequestDispatcher("/home.jsp").include(request, response);
}
%>
</body>
</html>

This is the login form which a user will see when the program is completed.

welcome.jsp

The welcome page in the Eclipse will have the following code and the page will look like the one in the image. welcome.jsp page is where the code for displaying the valid user name after the values are matched is written.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome</title>
</head>
<body>
<p>Welcome, <%=session.getAttribute("name")%></p>
<p><a href="logout.jsp">Logout</a>
</body>
</html>

This page will be shown to the user when he Logs in successfully.

error.jsp

The error page in the Eclipse will have the following code and the page will look like the one in the image. error.jsp page is where the code for displaying the error message when any problem occurs in logging in is written. This page is shown when the information entered by the user does not matches with the one in database.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login Error</title>
</head>
<body>
<center><p style="color:red">Sorry, your record is not available.</p></center>
<%
getServletContext().getRequestDispatcher("/home.jsp").include(request, response);
%>
</body>
</html>

logout.jsp

The logout page in the Eclipse will have the following code and the page will look like the one in the image. logout.jsp page is where the code for logout of user from the application is written.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Logout</title>
</head>
<body>
<% session.invalidate(); %>
<p>You have been successfully logout</p>
</body>
</html>
Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 8, 2013

Related Tutorials

Discuss: JSP Login Form with MySQL Database Connection and back end validation  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:5
priyanka
January 6, 2014
problem in login.jsp file

HTTP Status 500 - An exception occurred processing JSP page /login.jsp at line 56 -------------------------------------------------------------------------------- type Exception report message An exception occurred processing JSP page /login.jsp at line 56 description The server encountered an internal error that prevented it from fulfilling this request. exception org.apache.jasper.JasperException: An exception occurred processing JSP page /login.jsp at line 56 53: 54: 55: 56: if((!(name.equals(null) || name.equals("")) && !(password.equals(null) || 57: password.equals(""))) && !usertype.equals("select")) 58: 59: { Stacktrace: org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) javax.servlet.http.HttpServlet.service(HttpServlet.java:728) root cause java.lang.NullPointerException org.apache.jsp.login_jsp._jspService(login_jsp.java:122) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:728) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) javax.servlet.http.HttpServlet.service(HttpServlet.java:728) note The full stack trace of the root cause is available in the Apache Tomcat/7.0.35 logs. -------------------------------------------------------------------------------- Apache Tomcat/7.0.35
priynka
January 6, 2014
problem

i use mysql as back end nd when i run this code then i get this error as under =================================== HTTP Status 500 - An exception occurred processing JSP page /login.jsp at line 56 -------------------------------------------------------------------------------- type Exception report message An exception occurred processing JSP page /login.jsp at line 56 description The server encountered an internal error that prevented it from fulfilling this request. exception org.apache.jasper.JasperException: An exception occurred processing JSP page /login.jsp at line 56 53: 54: 55: 56: if((!(name.equals(null) || name.equals("")) && !(password.equals(null) || 57: password.equals(""))) && !usertype.equals("select")) 58: 59: { Stacktrace: org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) javax.servlet.http.HttpServlet.service(HttpServlet.java:728) root cause java.lang.NullPointerException org.apache.jsp.login_jsp._jspService(login_jsp.java:122) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:728) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) javax.servlet.http.HttpServlet.service(HttpServlet.java:728) note The full stack trace of the root cause is available in the Apache Tomcat/7.0.35 logs. -------------------------------------------------------------------------------- Apache Tomcat/7.0.35
Shridip chandole
March 1, 2014
Null Value insert into database

I have done login page and registration page but when i click to registration on anker tag of login page then it automatic insert null value in data base why?Following 2 pages in jsp Login.jsp <form id="form1" name="form1" method="post" action="Login.jsp" onsubmit="return check()"> <table> <tr> <td><label>Enter Email-id</label></td> <td><input name="user" type="text" onBlur="uservalid();" id="uname"/></td> <td><label id="usr"></label></td> </tr> <tr> <td><label>Enter Password</label></td> <td><input name="password" type="password" id='pwd' onBlur="passvalid();"/></td> <td><label id="pass"></label></td> </tr> <tr> <td><input type="submit" name="Submit" value="Submit" /></td> </tr> <tr><td>&nbsp;</td><td>Register for new User <a href="Registration.jsp">click here</a></td></tr> </table> Registration.jsp <%@page import="java.sql.*,java.util.*" language="java" %> <%@page import="java.awt.*,javax.swing.*" %> <% Connection con=null; Statement stmt=null; ResultSet rs=null; String fname=request.getParameter("fname"); String lname=request.getParameter("lname"); String user=request.getParameter("user"); try { Class.forName("com.mysql.jdbc.Driver"); con=DriverManager.getConnection("jdbc:mysql://localhost/loginuser","root","SIBM"); stmt=con.createStatement(); stmt.executeUpdate("insert into admininfo(fname,lname,user,pass,pass1,mob,gender,dob) values('"+fname+"','"+lname+"','"+user+"','"+request.getParameter("password")+"','"+request.getParameter("password")+"','"+request.getParameter("mobile")+"','"+request.getParameter("gender1")+"','')"); JOptionPane.showMessageDialog(null, "Registration Success"); } catch (SQLException e) { // TODO: handle exception e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }
sihem
June 14, 2014
thanks alot

thank you for this tutorial
sihem
June 14, 2014
thanks alot

thank you for this tutorial
DMCA.com