jsp log-in validation help please!
I'm new at using JSP. I am trying to make a log-in validation but it doesn't seem to get the output that i desire. I don't know what's the wrong with my code. please help me. Here's the code:
test.jsp
<%@ page import="java.util.*" %>
<%@ page contentType="text/html;charset=windows-1252"%>
<%@ page import="java.sql.*" %>
<%
String connectionURL ="jdbc:mysql://localhost:3306/petdepot?user=root&password=12345";
Connection connection =null;
Statement statement =null;
ResultSet rs =null;
%>
<%
String username = request.getParameter("uname");
String password = request.getParameter("pwrd");
String redirect ="";
%>
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">
<title> Pet Depot - Your Online Pet Shop </TITLE>
</head>
<body>
<%
if((username.equalsIgnoreCase(username))&&(password.equals("")))
{ %>
<% out.println("You entered a wrong password! Please "); %>
<a href="jlogin left.jsp">log-in again!</a>
<%}elseif((username.equalsIgnoreCase(username))&&(password.equalsIgnoreCase(password)))
{ %>
<%redirect ="validate.jsp";
RequestDispatcher dispatcher =
request.getRequestDispatcher(redirect);
dispatcher.forward(request, response); %>
<%}elseif ((username.equals("")) && (password.equals("")))
{ %>
<% out.println("You have not typed anything, "); %>
<a href="jlogin left.jsp">log-in again!</a>
<%}
elseif ((username.equals(""))&&(password.equalsIgnoreCase(password)))
{ %>
<% out.println("You have entered a wrong username! Please "); %>
<a href="jlogin left.jsp">log-in again!</a>
<%}else
{%> <% out.println("Invalid user!"); %>
<%}%>
<%Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL,"root","12345");
statement = connection.createStatement();
rs = statement.executeQuery("SELECT * FROM useraccounts WHERE username = '" + username +"' and password = '" + password +"'");
while (rs.next()){
String uname = rs.getString("username");
String pwrd = rs.getString("password");
%>
<%} %>
</body>
</html>
jlogin left.jsp
<title>Pet Depot - Your Online Pet Community </title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<form name="form1" method="post" action="test.jsp">
<table width="193" border="0" align="left" cellpadding="3" cellspacing="3">
<tr>
<td width="79">
<div align="left"><b>Username:</b></div></td>
<td width="93">
<div align="left">
<input name="uname" type="text" size="15">
</div></td>
</tr>
<tr>
<td>
<div align="left"><b>Password :</b></div></td>
<td>
<div align="left">
<input name="pwrd" type="password" size="15">
</div></td>
</tr>
<tr>
<td colspan="2">
<div align="center">
<input type="submit" name="Submit" value="Log-in">
</div></td>
</tr>
</table>
</form>
</body>
</html>
validate.jsp
<%@ page import="java.util.*" %>
<%@ page contentType="text/html;charset=windows-1252"%>
<%
String username=request.getParameter("uname");
String password = request.getParameter("pwrd");
%>
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">
<title> Pet Depot - Your Online Pet Shop </TITLE>
</head>
<body>
<%
if (username.equals(username) && password.equals(password)){
out.println("Welcome to Pet Depot " +username );
%>
<%
}else{out.println("Invalid!");
%>
<b><a href="jlogin left.jsp">Login Again!</a></b>
<%
}
%>
</body>
</html>
previosly, the simple code with if constructs was working perfectly, however, when i added the "else if's" my world crashed into pieces. validate.jsp is the temporary home page where a user will be redirected once a valid username and password is inputted.
hope for your help.
this means a lot.
thanks.
God bless.
[7558 byte] By [
dadaa] at [2007-10-3 4:49:25]

i think you are getting a nullpointer exception that is why on your condition username.equalsIgnoreCase(username))null equals null?that is my guess
Thanks!What should I do with that? Another problem that i encounter here is that the only condition that works in my if else construct is the first one, the rest doesn't. i don't know why? is there something wrong with my syntax?
dadaa at 2007-7-14 22:53:55 >

san ka school? tama naman syntax kaya lang gumgawa ka sa jsp ng servlet codes.. although tama naman kaya lang meron equivalent sa jsp yung ibang codes.. at nde sya nagreredirect..
codes are remodified:
test.jsp
<%@ page import="java.util.*" %>
<%@ page contentType="text/html;charset=windows-1252"%>
<%@ page import="java.sql.*" %>
<%
String connectionURL = "jdbc:mysql://localhost:3306/petdepot?user=root&password=jherald";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
String username = request.getParameter("uname")!=null?request.getParameter("uname"):"";
String password = request.getParameter("pwrd")!=null?request.getParameter("pwrd"):"";
String redirect = "";
%>
<%Class.forName("com.mysql.jdbc.Driver").newInstance();
String uname = "";
String pwrd = "";
int count = 0;
try
{
connection = DriverManager.getConnection(connectionURL, "root", "jherald");
statement = connection.createStatement();
rs = statement.executeQuery("SELECT * FROM useraccounts WHERE username = '" + username + "' and password = '" + password + "'");
while (rs.next()) {
uname = rs.getString("username");
pwrd = rs.getString("password");
count++;
}
connection.close();
} catch (Exception ex)
{
ex.printStackTrace();
} finally
{
}
%>
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">
<title> Pet Depot - Your Online Pet Shop </TITLE>
</head>
<body>
<%
if(!password.equals(pwrd)){
%>
<% out.println("You entered a wrong password! Please "); %>
<a href="left.jsp">log-in again!</a>
<%} else if((username.equalsIgnoreCase(uname)) && (password.equalsIgnoreCase(pwrd) && count>0)){%>
<jsp:forward page="validate.jsp"/>
<%}else if ((username.equals("")) || (password.equals(""))){%>
<% out.println("Please input the required fields "); %>
<a href="left.jsp">log-in again!</a>
<% }else if (!username.equals(uname)){ %>
<% out.println("You have entered a wrong username! Please "); %>
<a href="left.jsp">log-in again!</a>
<% } else{%>
<% out.println("Invalid user!"); %>
<a href="left.jsp">log-in again!</a>
<% }%>
</body>
</html>
left.jsp
<HEAD>
<title>Pet Depot - Your Online Pet Community</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
</HEAD>
<form name="form1" method="post" action="test.jsp">
<table width="193" border="0" align="left" cellpadding="3" cellspacing="3">
<tr>
<td width="79">
<div align="left">
<b>Username:</b><font color="red">*</font>
</div>
</td>
<td width="93">
<div align="left">
<input name="uname" type="text" size="15"/>
</div>
</td>
</tr>
<tr>
<td>
<div align="left">
<b>Password :</b><font color="red">*</font>
</div>
</td>
<td>
<div align="left">
<input name="pwrd" type="password" size="15"/>
</div>
</td>
</tr>
<tr>
<td colspan="2">
<div align="center">
<input type="submit" name="Submit" value="Log-in"/>
</div>
</td>
</tr>
</table>
</form>
kau po san kau school?
ung ibang code po kasi n ginamit ko is from this forum also
n ayon s kanila ay jsp. it works nung una kaya lang, nung dinagdagan
ko na yung mga conditions, yun na po, mali n output. kahit valid
or invalid yung username and password ko, hindi n nya nakukuha
yung dapat na output nya.
im using netbeans 5.0 nga po pla in running my codes.
How bout u po?
THANKS a lot!
dadaa at 2007-7-14 22:53:55 >

mapuan ako dati license ece ngaun pero d ko ginamit.. i tested your codes on my Oracle Jdeveloper 10g.. it will work on netbeans.. so yung codes na binagay ko itry mo.. nagremodify ako .. just ask in this forum kung meron ka nde naintindihan.. if i know ill help you.. btw what is your name and school?
really?
mapuan din po ako, CoE. candidate for graduation po ako this term. Kung ok lang po s inyo, pwede po ba na magtanong ako sa inyo ng tungkol sa java? I'm having difficulties po kasi in doing my software design which is a web application. S tingin ko naman po kasi eh napaka reliable nyo pong tanungan ng mga info about java. Hindi pa po kasi ako ganun ka-bihasa sa java.
ok lang po ba?
Thanks po tlg! =)
dadaa at 2007-7-14 22:53:55 >

yun ay kung masasagot ko. hehe ece ako ala pa ko 2 years na nagjajava pero i have experiences on different platforms.. so if i know i can answer you.. especially sa web.. i also make gui based apps
ako naman po mga 4 months pa lang po nagjjava. kaya lang po, pa-iba iba minsan jsp, minsan j2se, minsan java script. kaya po minsan e nalilito na rin po ako s mga codes.
hmnn.. if i have questions po pla, paano ko po kau mare-reach?
thanks po tlg. jherald po ba name nyo? yun kasi po yung password nyo sa database na ginamit nyo eh.
thanks po again.
dadaa at 2007-7-14 22:53:55 >

yup jherald is my name.. you can reach me here on weekdays.. i hope you coding on week days.. makikita ko naman agad name mo pagnagpost ka.. cge ill try to help you.. basically dapat pagaralan u rin yung HTML 4.01 stricts and tags nun kaya yung mga uppercase and lower case it should be correct.. javascript is not java but i can also help you on javascript.. iba ang nagdevelop ng javascript(jscript and tawag nila sa microsoft)
yup i do my coding almost everyday mostly kapag madaling araw. question po, when i input a username and password which is not in my database, nakakapasok pa rin po sha.. what should i do with that po?thanks.
dadaa at 2007-7-14 22:53:55 >

cge w8 lang may gagwin ang codes
index.jsp:
<%@ page contentType="text/html;charset=windows-1252"%>
<%
String error = request.getParameter("error")!=null?request.getParameter("error"):"";
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>untitled</title>
</head>
<body>
<form action="success.jsp" method="POST">
<table cellspacing="0" cellpadding="2" border="0" width="400">
<%if(error.equals("1")){%>
<tr>
<td colspan="2">
<font color="red"> You are not logged. Loggin First</font>
</td>
</tr>
<%}else if(error.equals("2")){%>
<tr>
<td colspan="2">
<font color="red"> Please input username and/or password</font>
</td>
</tr>
<%}else if(error.equals("3")){%>
<tr>
<td colspan="2">
<font color="red"> Invalid login</font>
</td>
</tr>
<%}%>
<tr>
<td>Username</td>
<td>
<input type="text" name="username"/>
</td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password"/></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Submit"/>
</td>
</tr>
<tr>
<td colspan="2">
<a href="mainpage.jsp">To Main Page</a>
</td>
</tr>
</table>
</form>
</body>
</html>
success.jsp
<%@ page contentType="text/html;charset=windows-1252"%>
<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %>
<%
String username = request.getParameter("username")!=null?request.getParameter("username"):"";
String password = request.getParameter("password")!=null?request.getParameter("password"):"";
String connectionURL = "jdbc:mysql://localhost:3306/petdepot?user=root&password=jherald";
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
String uname = "";
String pwrd = "";
int count = 0;
try
{
connection = DriverManager.getConnection(connectionURL, "root", "jherald");
statement = connection.createStatement();
rs = statement.executeQuery("SELECT * FROM useraccounts WHERE username = '" + username + "' and password = '" + password + "'");
System.out.println(username);
while (rs.next()) {
uname = rs.getString("username");
pwrd = rs.getString("password");
count++;
System.out.println("here");
}
connection.close();
} catch (Exception ex)
{
ex.printStackTrace();
} finally
{
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>untitled</title>
</head>
<body>
<%if(username.equals(uname) && password.equals(pwrd)){%>
<%response.sendRedirect("index.jsp?error=2");%>
<%}else if(username.equals(uname) && password.equals(pwrd)){
session.setAttribute("username",username);
%>
<a href="mainpage.jsp">Main Page</a>
<%}else{%>
<%response.sendRedirect("index.jsp?error=3");%>
<%}%>
</body>
</html>
mainpage.jsp
<%@ page contentType="text/html;charset=windows-1252"%>
<%
String username = session.getAttribute("username")!=null?(String)session.getAttribute("username"):"";
if(username.equals("")){
response.sendRedirect("index.jsp?error=1");
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>untitled</title>
</head>
<body>
<h2>Welcome <%=username%>!</h2>
<a href="logout.jsp">Logout</a>
</body>
</html>
logout.jsp:
<%@ page contentType="text/html;charset=windows-1252"%>
<%
session.setAttribute("username","");
session.invalidate();
response.sendRedirect("index.jsp");
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>untitled</title>
</head>
<body>
</body>
</html>
Question po: my mga parts po sa code na may tanong po ako, mga nka bold po cla..
success.jsp
<%@ page contentType="text/html;charset=windows-1252"%>
<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %>
<%
String username = request.getParameter[b]("username")[/b]!=null?request.getParameter("username"):"";
[b]Ung username po b sa request.getParameter("username") ay kailangan nameng palitan ng "uname"? Tma po ba? [/b]
t[b]pos po dito yung "password" will be "pwrd"?
String password = request.getParameter("password")!=null?request.getParameter("password"):"";[/b]
String connectionURL = "jdbc:mysql://localhost:3306/petdepot?user=root&password=12345";
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
String uname = "";
String pwrd = "";
int count = 0;
try
{
connection = DriverManager.getConnection(connectionURL, "root", "12345");
statement = connection.createStatement();
rs = statement.executeQuery("SELECT * FROM useraccounts WHERE username = '" + username + "' and password = '" + password + "'");
System.out.println(username);
while (rs.next()) {
uname = rs.getString("username");
pwrd = rs.getString("password");
count++;
System.out.println("here");
}
connection.close();
} catch (Exception ex)
{
ex.printStackTrace();
} finally
{
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>untitled</title>
</head>
<body>
<%if(username.equals(uname) && password.equals(pwrd)){%>
<%response.sendRedirect("index.jsp?error=2");%>
<%}else if(username.equals(uname) && password.equals(pwrd)){
[b]session.setAttribute("username",username);
%>[/b]
[b]dito po ba, uname din yung ilalagay ko? .setAttibute("uname",username);
[/b]
<a href="mainpage.jsp">Main Page</a>
<%}else{%>
<%response.sendRedirect("index.jsp?error=3");%>
<%}%>
</body>
</html>
how can i check po kung yung database connection po yung ayaw gumana?
thanks po
dadaa at 2007-7-14 22:53:56 >

actually yung binigay ko is complete codes na yun index.jsp ang login nya.. so ala na kayo babaguhin from login to logout na yan..
hello po:
nung tinest ko po yung code, kahit valid na ung username at password, error 2 p rin po yung lumalabas.
tinignan ko po yung code, eto po yung napansin ko: meron pong magkaparehas na conditions s success.jsp:
<%if(username.equals(uname) && password.equals(pwrd)){%>
<%response.sendRedirect("index2.jsp?error=2");%>
<%}else if(username.equals(uname) && password.equals(pwrd)){
session.setAttribute("username",username);
%>
thanks po ng maraming marami!
dadaa at 2007-7-21 10:44:44 >

sorry my mistake.. wrong condition:
replace the success.jsp with this:
<%@ page contentType="text/html;charset=windows-1252"%>
<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %>
<%
String username = request.getParameter("username")!=null?request.getParameter("username"):"";
String password = request.getParameter("password")!=null?request.getParameter("password"):"";
String connectionURL = "jdbc:mysql://localhost:3306/petdepot?user=root&password=jherald";
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
String uname = "";
String pwrd = "";
int count = 0;
try
{
connection = DriverManager.getConnection(connectionURL, "root", "jherald");
statement = connection.createStatement();
rs = statement.executeQuery("SELECT * FROM useraccounts WHERE username = '" + username + "' and password = '" + password + "'");
System.out.println(username);
while (rs.next()) {
uname = rs.getString("username");
pwrd = rs.getString("password");
count++;
System.out.println("here");
}
connection.close();
} catch (Exception ex)
{
ex.printStackTrace();
} finally
{
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>untitled</title>
</head>
<body>
<%if(username.equals("") || password.equals("")){%>
<%response.sendRedirect("index.jsp?error=2");%>
<%}else if(username.equals(uname) && password.equals(pwrd)){
session.setAttribute("username",username);
%>
<a href="mainpage.jsp">Main Page</a>
<%}else{%>
<%response.sendRedirect("index.jsp?error=3");%>
<%}%>
</body>
</html>
Yey!!!!Ok n po! Thanks sooo much po!Mkaka-move on na rin po ako sa log-in! hehehe..thanks so much po s tulong!God bless po!
dadaa at 2007-7-21 10:44:44 >

yeah thnx.. dapat talaga nagtutulungan mga mapuan..btw im only 23 next year pa ko mag24 so nde mo na kailangan magpo.... (',')good luck on your studies.. well if you don't understand what the codes do on the codes that i've given just let me know..
ay ganon po ba, ay hehe.. dpat nga pla wla po.. anyways, i'm 21 naman.. =).. thanks tlg ng madami! cge po, ill continue my coding po muna sa registration part naman.. thanks po ult.. God bless. =)
dadaa at 2007-7-21 10:44:44 >

sa school ba kayo nagcocode?