Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

How to Create Login Form in JSP Web page, Login Form Tutorial in JSP Web Page:-


1)  Create Table using Mysql Server 

     create table admin(username varchar(50),password varchar(50))

       insert  into admin values('admin',12345);

2)  Create Form in Jsp for login


    

<%@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="AdminSer" method="post">
            <p><input type="text" name="txtuser" placeholder="Enter username" /></p>
            <p><input type="password" name="txtpass" placeholder="Enter password" /></p>
            <p><input type="submit" name="btnsubmit" value="Login" /> </p>
        </form>
    </body>
</html>

3)  Create Servlet for admin login and write database code
     
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class AdminSer extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       try (PrintWriter out = response.getWriter()) {
           Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/java56","root","");
            Statement st = con.createStatement();
            ResultSet res = st.executeQuery("select * from admin where username='"+request.getParameter("txtuser")+"' and password='"+request.getParameter("txtpass")+"'");
            if(res.next())
            {
                response.sendRedirect("ViewStudent.jsp");
            }
            else
            {
                 response.sendRedirect("index.jsp?q=invalid userid and password");
            }
        }
        catch(Exception ex)
        {
           
        }
    }

   

}


Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post