Monday, 3 April 2017

Develop a simple JSP Program for User login Form With static and Dynamic database.

*Static Database

*FileName.
        
       -Index.html : For UserName & Password.
       -Process.jsp: For Static Database.
       -Welcome.jsp :Welcome File

*Code

Index.html



    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <form action="Process.jsp" method="post">
        Username:<input type="text" name="uname">
        password:<input type="password" name="pass">
        <input type="submit">
        </form>
       
    </body>
</html>


Process.jsp


<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<%

     String a=request.getParameter("uname");

     String b=request.getParameter("pass");

 if(a.equals("queue") && b.equals("queue"))

       {
             response.sendRedirect("Welcome.jsp");

       }

 %>  

Welcome.jsp


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%
    out.println("Welcome to world of jsp");
    %>


*Output


 

*Dynamic Database

*FileName
   
              -index.jsp :For Register Form.

              -servlet.jsp: Insert data indata base & redirect page                            login.jsp

              -login.jsp : Make sure Login Data.

              -wel.jsp:Welcome File. 


*Code

*index.jsp


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form action="servlet.jsp" method="post">
            Enter your UserName:<input type="text" name="name"><br>
            Enter your Password:<input type="password" name="pas"><br>
            Enter your email:<input type="email" name="email"><br>
          
            <input type="submit">
        </form>
       
    </body>
</html>

 
 

*servlet.jsp

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

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <%
              String name=request.getParameter("name");
              String pass=request.getParameter("pas");
              String email=request.getParameter("email");
            
             
           
              Class.forName("com.mysql.jdbc.Driver"); 
              Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/phgraph","root","ujash7878");
              PreparedStatement ps=con.prepareStatement("insert into reg values(?,?,?)");
             
ps.setString(1,name);
ps.setString(2, pass);
ps.setString(3, email);

 int i=ps.executeUpdate();

if(i>0)
{
    response.sendRedirect("login.jsp");
}
else
{
    out.println("Try Again");
}

       
       
        %>
    </head>
    <body>
</body>
</html>


*login.jsp



<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form action="wel.jsp" method="post">
            Name:<input type="text" name="name"><br>
            password:<input type="password" name="pas"><br>
            <input type="submit">
        </form>
      
       
    </body>
</html>
 

*Wel.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>JSP Page</title>
    </head>
    <body>
        <%
            String name=request.getParameter("name");
              String pass=request.getParameter("pas");
             
 Class.forName("com.mysql.jdbc.Driver"); 
              Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/phgraph","root","ujash7878");
              PreparedStatement ps=con.prepareStatement("select * from reg where name=? and pas=?");
              ps.setString(1, name);
              ps.setString(2, pass);
             
   ResultSet rs=ps.executeQuery();
     
       if(rs.next())
       {
           out.println("<h1>Welcome :"+name+"</h1>");
       }
       else
       {
           out.println("<h1>Not Done</h1>");
       }

       %>
     </body>
</html>


 *OUTPUT

           


 



 

  
Thanks ;)

Happy Coding.



 

 

 

  
 
    

 

7 comments: