Practical 4
c) Create a registration and login JSP application to
register and authenticate the user based on username
and password using JDBC.
Index.html
Enter Username:
Enter Password:
Re-Enter Password:
Enter Email:
Enter Country:
Reset Register
<html>
<form action="reg.jsp" >
Enter Username: <input type="text" name="t1"><br>
Enter Password: <input type="password" name="t2"><br>
Re-Enter Password: <input type="password" name="t3"><br>
Enter Email: <input type="text" name="t4"> <br>
Enter Country: <input type="text" name="t5"> <br>
<input type="reset"> <input type="submit" value="Register">
</form>
</html>
Creating a jsp page having name reg.jsp
reg.jsp
<%@page contentType="text/html" pageEncoding="UTF-8" import="java.sql.*"%>
<html>
<body>
<%
String uname=request.getParameter("t1");
String pass1=request.getParameter("t2");
String pass2=request.getParameter("t3");
String email=request.getParameter("t4");
String country=request.getParameter("t5");
if(pass1.equals(pass2))
try
Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/student1","admin"
,"nilam");
PreparedStatement pst=con.prepareStatement("insert into info values(?,?,?,?,?)");
pst.setString(1,uname);
pst.setString(2,pass1);
pst.setString(3,pass2);
pst.setString(4,email);
pst.setString(5,country);
int row=pst.executeUpdate();
if(row==1)
out.println("Registration successfully done......");
else
out.println("Registration Failed.....");
%>
<jsp:include page="index.html"></jsp:include>
<%
catch(Exception e)
out.println(e);
else
{
out.println("Password Mismatch....");
%>
<jsp:include page="index.html"></jsp:include>
<%
%>
</body>
</html>
Login.html
Enter Username:
Enter Password:
Reset login
<html>
<form action="log.jsp">
Enter Username: <input type="text" name="t1"><br>
Enter Password: <input type="password" name="t2"><br>
<input type="reset"> <input type="submit" value="login">
</form>
</html>
Creating a servlet page having name log.java
log.java
<%@page contentType="text/html" import="java.sql.*"%>
<!DOCTYPE html>
<html>
<body>
<%
String uname=request.getParameter("t1");
String pass1=request.getParameter("t2");
try
Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/student1","admin"
,"nilam");
PreparedStatement pst=con.prepareStatement("select pass1 from info where
name=?");
pst.setString(1,uname);
ResultSet rs=pst.executeQuery();
if(rs.next())
if(pass1.equals(rs.getString(1)))
out.println("<h1> login Successfully!!!! </h1>");
}
else
out.println("<h1> username does not exist..!!!! </h1>");
%> <jsp:include page="index.html"></jsp:include>
<%
catch(Exception e)
out.println(e);
%>
</body>
</html>
Output