0% found this document useful (0 votes)
4 views4 pages

User Authentication Servlet Example

The document outlines the creation of a dynamic web project named 'Pracital3_4' that includes a user login page (userLogin.jsp) and a servlet (authuserWithDb) for user authentication. The servlet connects to a MySQL database to validate user credentials against a 'userinfo' table. It provides feedback to the user based on whether the entered username and password match any records in the database.

Uploaded by

Amit Vikram
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

User Authentication Servlet Example

The document outlines the creation of a dynamic web project named 'Pracital3_4' that includes a user login page (userLogin.jsp) and a servlet (authuserWithDb) for user authentication. The servlet connects to a MySQL database to validate user credentials against a 'userinfo' table. It provides feedback to the user based on whether the entered username and password match any records in the database.

Uploaded by

Amit Vikram
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

4.

Write a Servlet program to send username and password using HTML forms and authenticate the user

Create a dynamic web project name it : Pracital3_4

Create Userlogin page name it : [Link]

<%@ page language="java" contentType="text/html; charset=UTF-8"


pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<form action="authuserWithDb" method="POST">


<table>
<tr>
<td>User Name:</td>
<td><input type="text" name="uname"/></td>
</tr>

<tr>
<td>User Password:</td>
<td><input type="text" name="upassword"/></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Submit"/></td>
</tr>

</table>
</form>
</body>
</html>

Create authuserWithDb servlet (Authenticating user with userinfo table)

package pk;

import [Link];
import [Link];
import [Link].*;

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

/**
* Servlet implementation class authuserWithDb
*/
@WebServlet("/authuserWithDb")
public class authuserWithDb extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public authuserWithDb() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request,
HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
// TODO Auto-generated method stub
[Link]().append("Served at:
").append([Link]());
}

/**
* @see HttpServlet#doPost(HttpServletRequest request,
HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
// TODO Auto-generated method stub
//doGet(request, response);
String un = [Link]("uname");
String ps = [Link]("upassword");

PrintWriter out = [Link]();

// initialize following value to authenticate with user data.


String un1 = "";
String ps1 = "";

// check the data that has come from form - that is uname and
upassword
Connection con;
Statement st;
ResultSet rs;
try
{
[Link]("[Link]");
con =
[Link]("jdbc:mysql://localhost:3306/db01?
allowPublicKeyRetrieval=true&useSSL=False","root","admin@123");
st = [Link]();
rs = [Link]("select * from userinfo where uid =
'"+ un+"'");
while ([Link]())
{
un1 = [Link](1);
ps1= [Link](2);
}
if (un1 == null || un1 == "")
{
[Link]("No such user available in user
table");
}
else
{
[Link]("Yes - Your are valid user - Welcome "
+ un1);
}
} catch(Exception e) {}

}
}

Project hierarchy

Table used in the application


table name: userinfo
create table userinfo
(
uid varchar(10),
uname varchar(20),
utype varchar(10),
remarks varchar(20)
);
insert into userinfo values ("amit","amitkumar","amit@123","admin");
insert into userinfo values
("minaal","minaalpatel","minaal@123","student");
insert into userinfo values ("sunil","sunilmohan","sunil@123","user");
insert into userinfo values ("pavan","pavankumar","pavan@123","accnt");

output:

with data exists in database

Output With an invalid user

You might also like