Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 1st, 2002, 01:12 PM
paddy_cassidy paddy_cassidy is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Posts: 0 paddy_cassidy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Nullpointerexception

I keep getting a nullpointerexception for the following pieces of code, can anyone find out what the prob is:

Login.jsp is as follows:

<HTML>
<HEAD>
<TITLE>Registration Login Form</TITLE>
</HEAD>
<BODY>
<form method="POST" action="ValidateLogin.jsp">
<table border="0" cellpadding="0" cellspacing="0" width="100%"
height="69">
<tr>
<td width="1%" height="23">UserId</td>
<td width="70%" height="23"><input type="text" name="userid"
size="20"></td>
</tr>
<tr>
<td width="1%" height="25">Password</td>
<td width="70%" height="25"><input type="password" name="password"
size="20">
</td>
</tr>
<tr>
<td width="1%" height="21"></td>
<td width="70%" height="21"></td>
</tr>
</table>
<p>
<input type="submit" value="Submit" name="SubmitBtn">
<input type="reset" value="Reset" name="B2">
</p>
</form>
</HTML>


ValidateLogin.jsp is as follows:

<%-- Import required Java Classes --%>
<%@ page import="javax.servlet.*" %>
<%@ page import="javax.servlet.http.*" %>
<%@ page import="java.sql.*" %>

<%
String user = request.getParameter("userid");
String pass = request.getParameter("password");

Connection con=null;
Statement stmt=null;
ResultSet rs=null;

String queryText="SELECT * FROM Register WHERE Username = '" + user + "' AND Password = '" + pass +"'";

try {
//initialize the JDBC-ODBC bridge driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

//open a connection to the database
String username = "";
String password = "";
con = DriverManager.getConnection("jdbcdbc:jsp_dbase",username,password);

//execute the query
stmt = con.createStatement();
rs = stmt.executeQuery(queryText);
} catch (Exception e) { }
%>

%>

<HTML>
<HEAD>
<TITLE>Model 1 Architecture</TITLE>
</HEAD>
<BODY>
<table border="0" cellspacing="0" cellpadding="5">
<tr>
<td>
<div align="right">First name:</div>
</td>
<td><b><%= rs.getString("FirstName") %></b> </td>
</tr>
<tr>
<td>
<div align="right">Last name:</div>
</td>
<td><b><%= rs.getString("LastName") %></b> </td>
</tr>
</table>
</body>
</html>

Reply With Quote
  #2  
Old April 1st, 2002, 01:50 PM
paddy_cassidy paddy_cassidy is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Posts: 0 paddy_cassidy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Got the last bit working

I got hte above working but can anyone tell me how i can forward the user to an errorpage if they enter an incorrect username or password.

Reply With Quote
  #3  
Old April 4th, 2002, 06:52 PM
midntrdr midntrdr is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2001
Posts: 45 midntrdr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
if ( "condition valid login" )
{
// go to valid login
}else
resp.sendRedirect("your error page")

Reply With Quote
  #4  
Old April 7th, 2002, 02:35 PM
eithne eithne is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Posts: 2 eithne User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Error

I've tried the above code but I am getting the following error

Method redirect(java.lang.String) not found in interface javax.servlet.http.HttpServletResponse.

Can you help me with this error please, this is what my code looks like

<%-- Import required Java Classes --%>
<%@ page import="javax.servlet.*" %>
<%@ page import="javax.servlet.http.*" %>
<%@ page import="java.sql.*" %>

<%
String user = request.getParameter("Username");
String pass = request.getParameter("Password");

Connection con=null;
Statement stmt=null;
ResultSet rs=null;

if ((user == null)||(pass == null))
{
response.redirect("indexerror.jsp");
}


String queryText="SELECT * FROM User WHERE UserID = '" + user + "' AND Password = '" + pass +"'";

try {
//initialize the JDBC-ODBC bridge driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

//open a connection to the database
String username = "";
String password = "";
con = DriverManager.getConnection("jdbcdbc:db",username,password);

//execute the query
stmt = con.createStatement();
rs = stmt.executeQuery(queryText);
} catch (Exception e) { }

if ((user == "UserID") && (pass == "Password"))
{
response.redirect("irishshareprices.jsp");
}
else
{
response.redirect("indexerror.jsp");
}

%>

Reply With Quote
  #5  
Old April 8th, 2002, 09:32 AM
midntrdr midntrdr is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2001
Posts: 45 midntrdr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Sorry, I was out of town for the weekend.

You have response.redirect()

It should be response.sendRedirect()

That should fix that! Let me know if you need any more help.

Reply With Quote
  #6  
Old April 8th, 2002, 10:17 AM
eithne eithne is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Posts: 2 eithne User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Unhappy Still not working

Thanks for your help.

When I put in response.sendRedirect I got the following error:

The server encountered an internal error (Internal Server Error) that prevented it from fulfilling this request.

I'm not sure what the problem is, any ideas?

Reply With Quote
  #7  
Old April 9th, 2002, 11:17 AM
eithne eithne is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Posts: 2 eithne User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Unhappy Login Screen

I got that problem fixed thanks but i have a new one now

I am working on the login screen so that when an invalid user logs in they go directly to the error page but when a registered user logs in they go to the first page which is called irishshareprices.jsp. The problem is that no matter if it's a registered user or not it's automatically going to the indexerror.jsp page.

The following is the code i'm using:

<%-- Import required Java Classes --%>
<%@ page import="javax.servlet.*" %>
<%@ page import="javax.servlet.http.*" %>
<%@ page import="java.sql.*" %>

<%
String user = request.getParameter("username");
String pass = request.getParameter("password");

Connection con=null;
Statement stmt=null;
ResultSet rs=null;

/*if ((user == null)||(pass == null))
{
response.sendRedirect("indexerror.jsp");
}*/


String queryText="SELECT * FROM User WHERE UserID = '" + user + "' AND Password = '" + pass +"'";

try {
//initialize the JDBC-ODBC bridge driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

//open a connection to the database
String username = "";
String password = "";
con = DriverManager.getConnection("jdbcdbc:db",username,password);

//execute the query
stmt = con.createStatement();
rs = stmt.executeQuery(queryText);
} catch (Exception e) { }


if ((user == rs.getString("UserID")) && (pass == rs.getString("Password")))
{
response.sendRedirect("irishshareprices.jsp");
}
else
{
response.sendRedirect("indexerror.jsp");
}

%>

Reply With Quote
  #8  
Old April 9th, 2002, 01:00 PM
midntrdr midntrdr is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2001
Posts: 45 midntrdr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
From a quick look try this....

if ((user.equals(rs.getString("UserID"))) && (pass.equals(rs.getString("Password"))))


Just stands out to me. Let me know if this doesn't work and i will look at it closer when I get out of work.

Reply With Quote
  #9  
Old April 9th, 2002, 01:53 PM
eithne eithne is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Posts: 2 eithne User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi, thanks for your suggestion, but i'm afraid it didn't work. It gave an internal server error. Is there anyway I can check if there is anything in rs or if it's picking up any records from the database. I can't understand why the code isn't working. I put this line in:

if ((user.equals("UserID")) && (pass.equals("Password")))

but it's still going to the error page instead of the proper one.

Reply With Quote
  #10  
Old April 10th, 2002, 10:48 AM
midntrdr midntrdr is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2001
Posts: 45 midntrdr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Sorry, I should have caught this earlier. Your not getting any rows, thats why your getting the error page. Your not getting any data. rs.next() or rs.getRow()

Reply With Quote
  #11  
Old April 10th, 2002, 02:05 PM
eithne eithne is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Posts: 2 eithne User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks

Hi

I got it working eventually, thanks very much. This is what i used:

rs = stmt.executeQuery(queryText)
boolean found = rs.next();

if (found)
{
response.sendRedirect"irishshareprices.jsp");
}
else
{
response.sendRedirect("indexerror.jsp");
}

} catch (Exception e) { }

Reply With Quote
  #12  
Old April 10th, 2002, 10:27 PM
midntrdr midntrdr is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2001
Posts: 45 midntrdr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Glad you got it working! Something I should mention is that you need to make sure you close your connection! Also close your query! and set them to null. This is good rule. Otherwise you will have major problems if your site gets any kind of traffic. Open connections will kill your site(deadlock) and open querys(database cursors) will run your site out of memory. These are lessons that I had to learn the hard way!! Just thought I would mention it.

Anyways, good luck with your project and if you need help along the road, I tend to check these boards often so ask away!
Best of luck,
Mark

Reply With Quote
  #13  
Old April 10th, 2002, 10:35 PM
midntrdr midntrdr is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2001
Posts: 45 midntrdr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
One more thing about closing connection and querys.....

try
{
//get connection
//run query
// login
//close query
//close connection
// con=null;
// rs=null;
}
catch
{
// exceptions
}
finally
{
// if connection and query !=null
// close them
}

finally block will always get executed!!!!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Nullpointerexception


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |