22.Servlet Login


import java.io.IOException;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class loginServlet extends HttpServlet
{
Connection con =null;
String user = null;
String pass = null;

@Override
public void init()
{
try{
Class.forName(“java.sql.Driver”);
con= DriverManager.getConnection(“jdbc:mysql://localhost:3306/new?zeroDateTimeBehavior=convertToNull”,”root”,””);
}
catch(Exception e)
{
System.out.println(“Error while Loading”);
}
}

public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
try{
user=request.getParameter(“name”);
pass=request.getParameter(“password”);
String sql=”Select * from login where name”+user+””;
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(sql);
String username = null;
String password = null;

while (rs.next())
{
username=rs.getString(2);
password=rs.getString(3);
}

if(username.equals(user)&&password.equals(pass))
{
System.out.println(“Login Successfull”);
}
else
{
System.out.println(“Login Failed”);
}
}
catch(Exception e)
{

}
}
}
Index.html

<!DOCTYPE html>
<!–
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
–>
<html>
<head>
<title>TODO supply a title</title>
<meta charset=”UTF-8″>
<meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
</head>
<body>
<center>
<form action=”loginServlet” method=”POST”>
UserName:<input type=”text” name=”name” value=”” /><br>
Password: <input type=”password” name=”password” value=”” /><br>
<input type=”submit” value=”submit” />
<input type=”reset” value=”Reset” />
NewUser<a href=”register.html”>Register</a>

</form>
</center>

</body>
</html>