Login Servlet

Steps to create Servlet which takes user name and password from user and verifies in database


  • Create HTML form which takes username and password from user
  • Create Servlet that verifies username and password
  • Configure Servlet URL in web.xml

We already created user profile previously.
profile_table

Login HTML


1
2
3
4
5
6
7
8
9
10
11
12
13
<html>
<body>
   <form action="http://localhost:8980/Javaforlearn/index" method="post">
 
     User name : <input type="text" name="username" required="required" />
     <BR><BR>
     Password : <input type="password" name="pass" required="required"/>
     <BR><BR>
 
   <input type="Submit" value="Sign In">
 </form>
</body>
</html>
loginservlet.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
 
package com.spy.javaforlearn;
 
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
public class LoginServlet extends HttpServlet
{
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException 
	{
 
		resp.setContentType("text/html");
		PrintWriter out = resp.getWriter();
 
		//1.1. Get the Form Data
		String username = req.getParameter("username");
		String password = req.getParameter("pass");
 
		//1.2. Validate the Form Data
		if(username==null || username.trim().isEmpty())
		{
			out.println("In-Valid User Name / Password");	
		}
		else if(password==null || password.trim().isEmpty())
		{
			out.println("In-Valid User Name / Password");	
		}
 
		else
 
		{
		try 
		 {
 
		 //2. Authenticate the Credentials
 
		    Connection con = null;
		    PreparedStatement pstmt = null;
 
		    ResultSet rs = null;
 
	  try 
	      {
	         //1. Load the Driver
		  Class.forName("com.mysql.jdbc.Driver").newInstance();
 
		  //2. Get the DB Connection via Driver
		String dbUrl = "jdbc:mysql://localhost:3306/test?user=root&amp;password=root";
		con = DriverManager.getConnection(dbUrl);
 
		 //3. Issue SQL Queries via Connection
		String query = " Select *from user_detail where UserName=? and password=? ";
 
			pstmt = con.prepareStatement(query);
			pstmt.setString(1,username);
			pstmt.setString(2,password);
			rs = pstmt.executeQuery();
 
			//4. Process the Results returned by SQL Queries
			if(rs.next())
			{
				/*
				 * Valid Credentials;
				 * I. Create the Session 
				 * for the First Time
				 */
 
				out.print("Login Successfull");
 
			}	
 
			else
			{			
			  out.println("In-Valid User Name / Password");					
			}
 
		} 
              catch (Exception e)
                    {
 
                        e.printStackTrace();
 
                     } finally{
 
                     //5. Close all JDBC Objects
 
                              try 
			       {
				if(con != null)
                                {
				  con.close();
				}
 
                              if(pstmt != null)
                                {
				   pstmt.close();
			        }
				if(rs != null)
                                 {
				   rs.close();
	                         }
 
                           } 
                        catch (SQLException e) 
                              {
				 e.printStackTrace();
			      }
 
                     }//End of Authentication
 
                   } 
 
			catch (Exception e1) 
                       {
 
				out.println("Something goes wrong, please try again");
 
			}
		}//End of Validation
	}//End of doPost
}//End of Class
Configuring web URL in web.xml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://java.sun.com/xml/ns/javaee" 
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
 <display-name>Javaforlearn</display-name>
 <welcome-file-list>
 <welcome-file>index.html</welcome-file>
 <welcome-file>index.htm</welcome-file>
 <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
 
 <servlet>
 <servlet-name>login</servlet-name>
 <servlet-class>com.spy.javaforlearn.LoginServlet</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>login</servlet-name>
 <url-pattern>/index</url-pattern>
 </servlet-mapping>
</web-app>
open login form URL

login-form
Provide username and password and click on sign in


2
when you click sign button request is forwarded to Login servlet


3

Leave a Reply

Your email address will not be published. Required fields are marked *