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>