Reputation: 45
I wrote this jsp code. It is a basic login control. I am trying to check inputs with my database. If they are correct then there is no problem but if the inputs are null or they are not in database I want to give error and redirect the guest to the login page.
<%@ 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>Login Process</title>
</head>
<body>
<%
String userid = request.getParameter("username");
String pwd = request.getParameter("password");
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/ecommerce", "root1" , "1234");
Statement st = con.createStatement();
ResultSet rs;
rs = st.executeQuery("select * from users where UserFirstName='" + userid + "' and UserPassword='" + pwd + "'");
while(rs.next()){
if(userid.length()!= 0 || pwd.length()!= 0){
if (rs.getInt("Admin") == 1) {
session.setAttribute("userid", userid);
response.sendRedirect("Admin.jsp");
} else if(rs.getInt("Admin") == 0){
session.setAttribute("userid", userid);
response.sendRedirect("User.jsp");
}
else {
out.println("Invalid username or password <a href='index.jsp'>try again</a>");
}
}
else{
out.println("Empty username or password <a href='index.jsp'>try again</a>");
}
}
%>
</body>
In this code two line is not working
else {
out.println("Invalid username or password <a href='index.jsp'>try again</a>");
}
and this one
else{
out.println("Empty username or password <a href='index.jsp'>try again</a>");
}
my browser just gives me a blank page instead else scopes.
Upvotes: 0
Views: 5528
Reputation: 672
Enclose the code in try - catch block and if any exception occurred will redirect to login page.
try
{
String userid = request.getParameter("username");
String pwd = request.getParameter("password");
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/ecommerce", "root1" , "1234");
Statement st = con.createStatement();
ResultSet rs;
rs = st.executeQuery("select * from users where UserFirstName='" + userid + "' and UserPassword='" + pwd + "'");
while(rs.next())
{
if(userid.length()!= 0 || pwd.length()!= 0){
if (rs.getInt("Admin") == 1) {
session.setAttribute("userid", userid);
response.sendRedirect("Admin.jsp");
} else if(rs.getInt("Admin") == 0){
session.setAttribute("userid", userid);
response.sendRedirect("User.jsp");
}
else {
out.println("Invalid username or password <a href='index.jsp'>try again</a>");
}
}
else{
out.println("Empty username or password <a href='index.jsp'>try again</a>");
}
}
}
catch (Exception e)
{
out.println("Exception: "+e.getMessage()+" :Invalid username or password <a href='index.jsp'>try again</a>");
}
e.getMessage(), will show the exception message, is optional since it will help for debugging but may reveal sensitive information if it is in a production system.
Upvotes: 0
Reputation: 46881
if the inputs are null or they are not in database I want to give error
use if (rs.next())
instead of while (rs.next())
to check for no records.
Sample code: (Modify it as per your requirement)
String userid = request.getParameter("username");
String pwd = request.getParameter("password");
// check for empty username or password before making a call to database
if (userid == null || userid.trim().length() == 0 || pwd == null
|| pwd.trim().length() == 0) {
out.println("Invalid username or password <a href='index.jsp'>try again</a>");
} else {
... // request for database query
if (rs.next()) { // use if instead of while to check for no record
...
} else {
out.println("Empty username or password <a href='index.jsp'>try again</a>");
}
}
Upvotes: 1