Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone,

I am new to JSP and servlet, so i started with this simple login control design page in jsp where i check username and password from my database.
I have this index.html page which directs me to login.jsp page where i have written my code, but each time this page does not comes but the control goes to else part and "invalid.jsp" page come out.
I know that at start both the values of "uname" and "pass" are null , but somehow i am not able to rectify this logic.

Please help me.
Thanks in advance.
Down goes my login.jsp code.
HTML
<%-- 
    Document   : login
    Created on : Mar 21, 2015, 7:06:41 PM
    Author     : new
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*;" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form method="post" acion="login.jsp">
            <table>
                <tr>
                    <td><label>User Name:</label></td>
                    <td><input type="text" name="user" placeholder="user name" required=""></td>
                </tr>
                <tr>
                    <td><label>Password:</label></td>
                    <td><input type="password" name="pass" placeholder="user password" required=""></td>
                </tr>
                <tr>
                    <td><input type="submit" value="Login" ></td>
                </tr>
            </table>
            <%! String uname,pass;%>
            <%
             try
             {
                uname=request.getParameter("user");
                pass=request.getParameter("pass");
                
                Connection cn;
                Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
                cn=DriverManager.getConnection("jdbc:derby://localhost:1527/niit;user=gaurav;password=1234");
                PreparedStatement pst;
                pst=cn.prepareStatement("select * from userlogin where username='"+uname+"' and password='"+pass+"'");
                ResultSet rs;
                rs=pst.executeQuery();
                if(uname==null && pass== null)
                {
                    uname="";
                }
               //s out.println("ok");
                if(rs.next())
                {
                    session.setAttribute("username", uname);
                    response.sendRedirect("index.html");
                    
                   // session.setAttribute("username", uname);
                //response.sendRedirect("userpage.jsp");
                }
               else
                {
                    response.sendRedirect("invalid.jsp");
                }
             }
           catch(Exception ex)
           {
               out.println(ex);
           }
           
           %>
        </form>
    </body>
</html>
Posted
Updated 21-Mar-15 22:23pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900