Practical-17 Develop a simple JSP program for user login form with static and dynamic database.

User login form with Static Database:

index.html:

<!DOCTYPE html>
<html>
    <head>
        <title>With Static Database</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
       <form action="static-action.jsp" method="post">
            Username: <input type="text" name="name"/><br/>
            Password: <input type="password" name="pass"/> <br/>
            <button type="submit">Login</button>
        </form>
    </body>
</html>

static-action.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Static Action</title>
    </head>
    <body>
        <%
            String name = request.getParameter("name");
            String pass = request.getParameter("pass");
            
            if(name.equals("abc") && pass.equals("abc123")){
                out.print("User is Valid");
            }
            else{
                out.print("User is Invalid");
            }
        %>
    </body>
</html>

User login form with Dynamic Database:

Execute following command in MySQL Command Line:

1. create database test;

2. use test;

3. create table login(username varchar(30), pass varchar(30));

4. insert into login(username,pass) values('abc','123');

5. insert into login(username,pass) values('xyz','123');

Now Add mysql-connector jar to project library

Then

index.html:

<!DOCTYPE html>
<html>
    <head>
        <title>With Static Database</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
       <form action="dynamic-action.jsp" method="post">
            Username: <input type="text" name="name"/><br/>
            Password: <input type="password" name="pass"/> <br/>
            <button type="submit">Login</button>
        </form>
    </body>
</html>

dynamic-action.jsp:

<%@page import="java.io.*" %>
<%@page import="java.util.*" %>
<%@page import="java.sql.*" %>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Dynamic Action</title>
    </head>
    <body>
        <%
            String name = request.getParameter("name");
            String pass = request.getParameter("pass");            
            
            try{
                Class.forName("com.mysql.cj.jdbc.Driver");
                Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");

                PreparedStatement pst = con.prepareStatement("select username, pass from login where username=? and pass=?");

                pst.setString(1, name);
                pst.setString(2, pass);

                ResultSet rs = pst.executeQuery();

                if(rs.next()){
                    out.print("Valid User");
                }
                else{
                    out.print("Invalid User");
                }
            }
            catch(Exception e){
                out.print(e);
            }
        %>
    </body>
</html>

Output:



Comments

Popular posts from this blog

Write a program to check whether empty query string is entered in Asp.Net.

Mandavrayji Temple, Muli, Surendranagar, Gujarat

Write a program to change color of Label text control programmatically in Asp.Net.