Home Jsp Login Authentication using Bean and Servlet In JSP
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Login Authentication using Bean and Servlet In JSP

Advertisement
In this section, we have developed a web application of login authentication using Bean in JSP.

Login Authentication using Bean and Servlet In JSP

     

In this section, we have developed a web application of login authentication using Bean  in JSP. Five files are used  "login.jsp", "loginbean.jsp", "welcome.jsp","LoginBean.java" and "login.java" in the code given below.

Brief description of the flow of application :

1). Create a webpage "login.jsp"  to login the user. 

2). Create a webpage "loginbean.jsp" to set the parameter using JSP Bean and forward to the Servlet page.

3). Create a bean file "LoginBean.java" to mapping the parameter from "loginbean.jsp".

4). Create a Servlet  "login.java" to validate the username and password from the database.

5). Create a webpage "welcome.jsp" display a message after successfully user login.  

 

Step:1 Create a web page  "login.jsp"  to login the user.

<html>
<head>
</head>
<body>
<form name="loginform" method="post" action="loginbean.jsp">
<br><br>
<table align="center"><tr><td><h2>Login Authentication</h2></td></tr></table>
<table width="300px" align="center" style="border:1px solid #000000;background-color:#efefef;">
<tr><td colspan=2></td></tr>
<tr><td colspan=2>&nbsp;</td></tr>
  <tr>
  <td><b>Login Name</b></td>
  <td><input type="text" name="userName" value=""></td>
  </tr>
  <tr>
  <td><b>Password</b></td>
  <td><input type="password" name="password" value=""></td>
  </tr>
  <tr>
  <td></td>
  <td><input type="submit" name="Submit" value="Submit"></td>
  </tr>
  <tr><td colspan=2>&nbsp;</td></tr>
</table>
</form>
</body>
</html>


 Step:2 To create a "loginbean.jsp" to set the parameter of the login.

<%page language="Java" import="java.sql.*" %>  
<HTML> 
<HEAD><TITLE>DataBase Search</TITLE></HEAD>  
<BODY>
<jsp:useBean id="db" scope="request" class="logbean.LoginBean" >
  <jsp:setProperty name="db" property="userName" value="<%=request.getParameter("userName")%>"/>
  <jsp:setProperty name="db" property="password" value="<%=request.getParameter("password")%>"/>
 </jsp:useBean>
<jsp:forward page="hello">
  <jsp:param name="username" value="<%=db.getUserName()%>" />
  <jsp:param name="password" value="<%=db.getPassword()%>" />
</jsp:forward> 
</body>
</html>

Step:3 To create a "LoginBean.java" to mapping the parameter of  "loginbean.jsp".

package logbean;
public class LoginBean {
  String userName="";
  String password="";
  public String getUserName() {
  return userName;
  }
  public void setUserName(String userName) {
  this.userName = userName;
  }
  public String getPassword() {
  return password;
  }
  public void setPassword(String password) {
 this.password = password;
  }
  }
  

Step:4 To create a  Servlet "login.java" for validate the user login.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.sql.*;
import java.sql.*;
public class login extends HttpServlet
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException,IOException{
  response.setContentType("text/html");
  PrintWriter out = response.getWriter();
  System.out.println("MySQL Connect Example.");
  Connection conn = null;
  String url = "jdbc:mysql://localhost:3306/";
  String dbName = "user_register";
  String driver = "com.mysql.jdbc.Driver";
  String userName = "root"
  String password = "root";
 String username="";
 String userpass="";
 String strQuery= ""
  Statement st=null;
  ResultSet rs=null;
  HttpSession session = request.getSession(true);
  try {
  Class.forName(driver).newInstance();
  conn = DriverManager.getConnection(url+dbName,userName,password);
  if(request.getParameter("username")!=null &&
     request.getParameter("username")!="" && request.getParameter("password")!=null &&
     request.getParameter("password")!="")
  {
  username = request.getParameter("username").toString();
  userpass = request.getParameter("password").toString();
  strQuery="select * from userregister where 
    username='"+username+"' and  password='"+userpass+"'";
 System.out.println(strQuery);
  st = conn.createStatement();
  rs = st.executeQuery(strQuery);
  int count=0;
  while(rs.next())
  {
  session.setAttribute("username",rs.getString(2));
  count++;
  }
  if(count>0)
  {
  response.sendRedirect("welcome.jsp");
  }
  else
  {
 response.sendRedirect("login.jsp");
  }
  }
  else
  {
 response.sendRedirect("login.jsp");
  }
  System.out.println("Connected to the database"); 
  conn.close();
  System.out.println("Disconnected from database");
  catch (Exception e) {
  e.printStackTrace();
  }
  }
}

Step :5 To create the webpage "welcome.jsp" to display the message after successful message.

<HTML> 
<HEAD><TITLE>Welcome</TITLE></HEAD>  
<BODY>
<br><br><br><br>
<table align="center" style="border:1px solid #000000;">
<%
if(session.getAttribute("username")!=null && session.getAttribute("username")!="")
{
String user = session.getAttribute("username").toString();
%>
<tr><td align="center"><h1>Welcome <b><%= user%></b></h1></td></tr>
<%
}
%>
</table>
</body>
<html>


Output:

Login Authentication form :


After Successful Login user :


Download the application

Advertisement

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: December 19, 2008

Ask Questions?    Discuss: Login Authentication using Bean and Servlet In JSP   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
rajesh
April 8, 2011
placing files to which folders

sir i m using tomcat so i want to know that where should i placed the file login.java in the classes folder or in jsp-examples folder
rajesh
April 8, 2011
error i palcin the fiels

it gives me following error HTTP Status 404 - /jsp-examples/hello type Status report message /jsp-examples/hello description The requested resource (/jsp-examples/hello) is not available.
tara
April 17, 2011
hi

can u please explain me the step 2 and 3 in more detail why is that jsp page required why the mapping is required
darshan
July 7, 2011
check

How you are calling login.java servlet from jsp?
mrbui
July 19, 2011
IF SQL 2k5

IF I want connect by SQL Server 2005 then I do how?? Sorry, english level very bad! ^^
Vlad
June 12, 2013
Thanks

worked for me!
LAKSHMIKANTH VALLAMPATI
September 17, 2011
Suggestion about your articles

Dear Roseindia, Your articles are very good. All your articles are explains the MySQL connectivity. If you provide the jsp and java database connectivity with ORACLE then roseindia is very much benefial for many more programmers. Why because many of the universities having oracle in thier academic curriculum. Thank you
Deepak Mishra
September 26, 2011
Wrong code posted

I used the code but it appears as wrong. in forwarding the page name has been mentioned as wrong. <jsp:forward page="hello"> <jsp:param name="username" value="<%=db.getUserName()%>" /> <jsp:param name="password" value="<%=db.getPassword()%>" /> </jsp:forward> instead of hello it will be login
d
February 9, 2013
feedback

it only goes to the success page. no matter if the values entered are valid or no.
Barun
October 13, 2011
sql query comprison is case-insensitive

It was very useful to me to go through it. Thanks a lot for that. But, one thing that I found is that the login-credentials check is case-insensitive.
Kofi
December 8, 2011
Something not clear.

In the code bellow what does the stand for. <jsp:forward page="hello"> <jsp:param name="username" value="<%=db.getUserName()%>" /> <jsp:param name="password" value="<%=db.getPassword()%>" /> </jsp:forward>
khushi
March 18, 2013
login page

nice help..
Wolf
January 27, 2012
For those getting errors

If you are getting error: "resource not found" ; then, simply made the following change in loginbean.jsp --> in place of "hello" write "login" --> here login is the login.java Illustrated: <jsp:forward page="hello"> //**--> make change here <jsp:param name="username" value="<%=db.getUserName()%>" /> <jsp:param name="password" value="<%=db.getPassword()%>" /> change it to: <jsp:forward page="login"> <jsp:param name="username" value="<%=db.getUserName()%>" /> <jsp:param name="password" value="<%=db.getPassword()%>" /> Note: Also see welcome.jsp. If you get error like 'string caannot be declared here', then do this, <% String user = null; **rest code same// Hope it helped, Wolf
Sathish
February 2, 2012
how to connect with database

it is very useful code. but i couldn't get database values on browser..
karthika
February 6, 2012
Getting error

Am getting error when i click submit button in this app..
Ramesh
February 28, 2012
hello

thanks
Punitha
February 29, 2012
html,servlet,jsp

please provide the code and explanation using db2 and RAD for login page authentication
ann
March 12, 2012
use of bean .

why ur using bean like loginbean and all here. without bean we cant do login procedure. pls help me
Philip
April 4, 2012
The requested resource (/hello) is not available.

<jsp:forward page="hello"> Im getting an error in relation to this bit of the code, can anyone help?
wajid
May 12, 2012
j2ee

lot of thanks....!
Manoj
May 15, 2012
Login authentication

thanks it helped me soo much
soumya
May 17, 2012
error in loginbean.jsp file

i found some difficulties while running these codes in eclipse: in loginbean.jsp page:: 7th line (<jsp:useBean id="db" scope="request" class="logbean.LoginBean" >)--> shows error "multiple annotation found at this line, undefined type" 14th line(<jsp:param name="username" value="<%=db.getUserName()%>" />) and 15th line (<jsp:param name="password" value="<%=db.getPassword()%>" />) lines showing the same error--> db cannot be resolved.. can any one help me out with the solution?
Fury91
May 22, 2012
Thanks

Nice Tutorial, thanks :)
pratik
June 8, 2012
jspbeanlogin

hey friend i am getting arror /jspbeanlogin/ not found when i m trying to run the code in netbeans. please help me out.
bebop
June 11, 2012
in response to error while running

in regards to the following error: org.apache.jasper.JasperException: /jspbeanlogin/loginbean.jsp(9,57) Attribute value request.getParameter("userName") is quoted with " which must be escaped when used within the value org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:40) This is a small syntax error in loginbean.jsp...to resolve this issue replace all occurences of value="" with value='' as follows value="<%=request.getParameter("userName")%>" with value='<%=request.getParameter("userName")%>' /* single quotations*/ has something to do with jsp 2.0 I think.
bebop
June 11, 2012
class attribute logbean.LoginBean is invalid

after resolving the syntax error in loginbean.jsp I ran into another issue that I am still trying to resolve. I now recieve the following error from apache tomcat6: org.apache.jasper.JasperException: /jspbeanlogin/loginbean.jsp(7,0) The value for the useBean class attribute logbean.LoginBean is invalid. I've tried moving LoginBean.java into folder logbean which is where LoginBean.class resides. I am about to recompile the file and see if this works. If anyone has come accross this, help is greatly appreciated!
Pratik
June 25, 2012
Need a Help

Hey Brother can you please tell me how i should put this file in a folder in webapp. and what change i need to do to run this program in my machine. thanks.
milot
July 11, 2012
error issue

am having on error on login servlet java strQuery="select * from userregister where username='"+username+"' and password='"+userpass+"'"; is there any wrong spelling or missing quotation
Rudra Chauhan
July 23, 2012
Username and Password

I am entering a random username and password bec i dnt know what the values to be entered. on submit click this is showing... /loginbean.jsp (line: 9, column: 57) Attribute value request.getParameter("userName") is quoted with " which must be escaped when used within the value and if i have to create a database then how should i create it.
Godala
August 21, 2012
Code not working

the above code which u provided is not working. it says the resource hello is not found which u have mentioned in loginbean.jsp
KB
December 27, 2012
JSP runned

I Runned EFFECTIVLY....... just change from "hello" to "login" its working
rayhan
December 26, 2012
thanks

your code helped me a lot. thanks
Sagar
October 3, 2012
error

message /loginbean.jsp (line: 10, column: 57) Attribute value request.getParameter("userName") is quoted with " which must be escaped when used within the value
Anita
October 11, 2012
the above code

in step4 there is 1 error coming in the following line strQuery="select * from userregister where username='"+username+"' and password='"+userpass+"'"; the error says "unclosed character literal" can u plz solve it...
mahesh
October 18, 2012
Error in loginBean.jsp

I tried this application.But i got an error in loginBean.jsp the error seems like "undefinedtype:logbean:LoginBean" can you please help me
Ummu
July 19, 2013
Thanks! Success!

It's works fine with oracle. Only need to modify a few lines. Thanks alot guys, your comments help me correct a few errors in the code. It's a good practice by using bean instead of compiling everything in jsp page.
sri
December 13, 2012
send answers

pls send answers for above questions
DMCA.com