Login Authentication using Bean and Servlet In JSP

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.

<form name="loginform" method="post" action="loginbean.jsp">
<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>
  <td><b>Login Name</b></td>
  <td><input type="text" name="userName" value=""></td>
  <td><input type="password" name="password" value=""></td>
  <td><input type="submit" name="Submit" value="Submit"></td>
  <tr><td colspan=2>&nbsp;</td></tr>

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

<%page language="Java" import="java.sql.*" %>  
<HEAD><TITLE>DataBase Search</TITLE></HEAD>  
<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:forward page="hello">
  <jsp:param name="username" value="<%=db.getUserName()%>" />
  <jsp:param name="password" value="<%=db.getPassword()%>" />

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{
  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 {
  conn = DriverManager.getConnection(url+dbName,userName,password);
  if(request.getParameter("username")!=null &&
     request.getParameter("username")!="" && request.getParameter("password")!=null &&
  username = request.getParameter("username").toString();
  userpass = request.getParameter("password").toString();
  strQuery="select * from userregister where 
    username='"+username+"' and  password='"+userpass+"'";
  st = conn.createStatement();
  rs = st.executeQuery(strQuery);
  int count=0;
  System.out.println("Connected to the database"); 
  System.out.println("Disconnected from database");
  catch (Exception e) {

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

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


Login Authentication form :

After Successful Login user :

Download the application

Share on Google+Share on Google+

Login Authentication using Bean and Servlet In JSP

Posted on: December 19, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles



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

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
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
December 27, 2012
JSP runned

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

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

message /loginbean.jsp (line: 10, column: 57) Attribute value request.getParameter("userName") is quoted with " which must be escaped when used within the value
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...
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
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.
February 22, 2017

jrvohbw http://www.accomlink.co.uk/adidas-tubular-doom-triple-black-on-feet-015 http://www.accomlink.co.uk/adidas-shoes-high-tops-blue-and-white-283 http://www.frankluckham.co.uk/adidas-superstar-black-and-rainbow-060.php http://www.mandala2012.co.uk/675-adidas-red-white-and-blue-shoes-with-wings.html http://www.hairextensionscity.co.uk/529-nike-roshe-run-neon-blue.html [url=http://www.backpackersholidays.co.uk/041-nike-air-force-1-low-whiteblack.html]Nike Air Force 1 Low White/Black[/url] [url=http://www.wandsworth-plumbing.co.uk/ray-ban-pink-sunglasses-324.htm]Ray Ban Pink Sunglasses[/url] [url=http://www.offerzone.co.uk/418-converse-womens-uk.htm]Converse Womens Uk[/url] [url=http://www.misstilly.co.uk/nike-shoes-919.htm]Nike Shoes[/url] [url=http://www.hairextensionscity.co.uk/888-nike-roshe-run-japan.html]Nike Roshe Run Japan[/url]
December 13, 2012
send answers

pls send answers for above questions