Himank Jain
Himank Jain

Reputation: 23

Problem with using JSP and Javascript simultaneously

When response.sendRedirect is used with Javascript the JS code is not working. My goal is to make the user stay on same page if login credentials are wrong but instead it goes to a blank page. If login is wrong the JS code runs and get redirect to a blank page. I want it to remain on the same page.

Here is the HTML/CSS Code.

<!DOCTYPE html>
<html>
<head>
    <title>Page0</title>
    <style>

        h3 {

    font-family: Calibri;
    font-size: 35pt;
    color: #929292;
    font-style: normal;
    font-weight: bold;
    text-align: center;

        }

    input[type=text] {
        background-color: #E7E5E5;
        width: 300px;
        height: 30px;
        position: relative;
        display: block;
        margin: auto;
        padding: 12px 15px;
        border: none;
    }

    input[type=password] {
        background-color: #E7E5E5;
        width: 300px;
        height: 30px;
        position: relative;
        display: block;
        margin: auto;
        padding: 12px 15px;
        border: none;
    }

div {

    width: 400px;
    height: 450px;
    background-color: white;
    margin: auto;
    left: 0; right: 0; top: 0; bottom: 0;
    position: fixed;
    border-radius: 10px;
}

button {

    display: block;
    margin: auto;
    padding: 15px 118px;
    font-size: 30px;
    color: white;
    border: none;
    background-color: #07CCF5;
}


    </style>
</head>
<body bgcolor="#07CCF5">
<div class="boxed">

    <h3>Login</h3>
    <form action="Page0-0.jsp" method="POST">
        <input type="text" id="txt1" name="Username" placeholder="Enter Username">
        <br><br>
        <input type="password" id="txt2"  name="Password" placeholder="Enter Password">
        <br><br>
        <button type="submit" value="submit">Sign In</button>
        </form>

</body>
</html>


Here is the jsp code.


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

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

    <%

    try{
    String s1=request.getParameter("Username");
    String s2=request.getParameter("Password");
    String s3="",s4="";
    Class.forName("com.mysql.jdbc.Driver").newInstance();  
    java.sql.Connection con=DriverManager.getConnection(  
    "jdbc:mysql://localhost:3306/Login","root","");  
    Statement stmt=con.createStatement();  
    ResultSet rs=stmt.executeQuery("select * from log where U_name='"+s1+"'");  
    while(rs.next()){
    s3=rs.getString(1);
    s4=rs.getString(2); 
    }
    if(s1!=""&&s2!=""){
    if(s1.equals(s3)&&s2.equals(s4))
        response.sendRedirect("Page0-1.jsp");
    else
        response.sendRedirect("Page0.jsp");
    }
    else{  

        %>
       <script type="text/javascript">
       alert("Invalid username or password try again");
       </script>
       <%
      //response.sendRedirect("Page0.jsp");
    }   
    con.close();
    }
    catch(Exception e){
    out.print(e.toString());
    }
    %>  

</body>   
</html>

Upvotes: 0

Views: 63

Answers (1)

Deltapimol
Deltapimol

Reputation: 176

I am no expert but there are a lot of issues in your code , I would like to share my thoughts on them.

While you are using the rs.next() to iterate through your set of username and password, the string in which you are storing it (i.e. s3 and s4) is getting overwritten. Use an ArrayList if you want all your entries stored and not strings,and then check for the username and password. However this method is not great. Instead I suggest using the below code for checking the values from database.

Your 'if and else' statements are wrong, you are checking if s1 and s2 are null and then doing nothing about it in case they are indeed null. Further only one of the last 2 'else' statements will get executed and not both in case you want a JS alert to pop up at wrong username and password.

            String uname=request.getParameter("Username");
            String passwd=request.getParameter("Password");
            PreparedStatement ps;
            ResultSet rs;      

            ps=con.prepareStatement("select * from log where username=? and user_password=?");
            ps.setString(1, uname);
            ps.setString(2, passwd);
            rs=ps.executeQuery();

            if(!rs.next()) 
            {   
                 // Your code if username and password don't match
                 // Use only one: JS redirect code or the JSP redirect code
                  //JS alert code here 
            }
            else 
                {
                 //  Your code if username and password match
                 // Use only one: JS redirect code or the JSP redirect code
                 }

In future try keeping your database connection with data retrieval methods in separate Java Class methods with proper parameters and return types as per requirement and call them on the JSP page when needed.

Upvotes: 1

Related Questions