Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Login Issue, forwarding to JSP problem  RSS feed

 
Jeremy McNally
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BELOW I have pasted my code. My issue here is this: When the user enters login data into form, the data IS retrieved from the database JUST FINE. I created the customer object perfectly and running a test in my MAIN method, the VALIDATION in the LoginServlet actually works. I tested it out of another class though. Now when I actually run the program on my server, (even though the validation is perfectly right I believe), I keep being LOOPED back to the login.jsp page or I get shown a NullPointerException.
I REALLY NEED YOUR HELP ON THIS YOU GUYS. Any solid answers to my problem would be very much appreciated. I have NO CLUE what the problem is and it has been 2 full days now to where I keep coming back to this and simply can't get this figured out. Someone I know suggested I may not have permissions? I have no idea what that means if that is even my problem. I mean I know what it means but I wouldn't get how? I am the owner of my laptop and I am also the one writing the program. I never had anything to do with setting permissions on this.









____________________________________________________________________________________________________



________________________________________________________________________________________________________________


THE (LOGINDAO.java) --> this method retrieves the data from the database that is used in the LOGINSERVLET for validation purposes for the login.



 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 36463
464
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is your JSP located? Usually the code looks something like:


The WEB-INF folder is so nobody can read your JSP directly - only through your servlet. And the initial / makes it an absolute path so you and your server are laking about the same location.

 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




HTML form parameter names are case-sensitive, so the pWord variable is going to be null regardless of what the user entered.

(By the way I found this much easier to see after I edited your post to include the post tags. Previously the code was very hard to read. It would be helpful if you could do that yourself in future posts.)
 
Jeremy McNally
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your response and I will do what you mentioned in the future so thanks for the tip.
As far as the case sensitive form parameter, I spelt it in a case sensitive manner in the (request.getParameter("pWord");) which was exactly how it was spelt in the JSP form.
I don't believe that is my issue here. Any other suggestions? Maybe bad validation? Anything else?


// this is pasted right from my LoginServlet. That is a small 'p'.
String pWord = request.getParameter("pWord");
 
Jeremy McNally
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is located in my WEB CONTENT folder and not the WEB-INF. I know this may have nothing to do with this initial post but (Should I put ALL my JSP's in the Web-Inf folder)? If I do, do I use your exact same syntax you showed me "/web-inf/blah/blah"?
Thank you.

Overall though What is my exact issue here does anyone know?
 
Jeremy McNally
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also I keep my servlets packaged up in the JAVA RESOURCES FOLDER... Is this not a good place for them. I haven't had an issue with it yet?
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeremy McNally wrote:As far as the case sensitive form parameter, I spelt it in a case sensitive manner in the (request.getParameter("pWord");) which was exactly how it was spelt in the JSP form.


Well, no, it wasn't. I posted the line from the JSP form where it was spelled "PWord" and not "pWord".

I don't believe that is my issue here. Any other suggestions?


Okay then. My suggestion is to do some debugging. I predicted that the value of the pWord variable would be null after it was assigned the value of the request parameter. Your prediction differs from mine. So put in a debugging statement which outputs the value of that variable.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSPs should be kept in a folder (or folders) under WEB-INF to prevent them from being directly accessed by the browser via URL. That way, the only way the JSP can be displayed is by going through its controller.

Btw, note that the "web content" folder is an IDE artifact. It is not a standard folder and is meaningless outside of the IDE (and to people not using the same IDE).
 
Jeremy McNally
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear! I just asked you the same question via another post. Disregard it, I didn't realize this was you in this one too. Thank you.
 
Jeremy McNally
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul I think you saved me. You were right. I looked at it like 3 times after you mentioned the case sensitive issue and didn't see it. I DO NOW. That just REALLY helped me.

And Bear, you are certainly right about what you said in terms of the WEB-INF folder with my JSP's. I just need to start with /WEB-INF and then the rest of the path to my JSP? That is it?
 
Jeremy McNally
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PAUL!!! That issue FIXED MY PROBLEM. I can NOT thank you enough. This has been a huge problem for me. I am serious THANK YOU. AND Thank you BEAR.
 
Jeremy McNally
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I DO HAVE ONE SMALL ISSUE THAT CONTINUES THOUGH:

The validation works and when I enter a CORRECT username/password I am then transferred to my (flightsearch.jsp) page.
BUT, when I enter an INVALID login I am provided a (NullPointerException) instead of being looped back to (login.jsp). Do you see any issues. I thought I had it all right.
Because IF login is correct, forward me to (flightsearch.jsp) ELSE forward me to (login.jsp). Do I need to do a (redirect) maybe you think?
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 36463
464
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which line of code is giving the null pointer.

ps - note that all uppercase is considering "shouting" on the internet. Bold is emphasis (which I think is what you are intending.)
 
Jeremy McNally
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the part of the code in the (LoginServlet) that is not working properly. When an invalid login is submitted, it gives me a NullPointerException. This the second half of my main problem from my initial post. The other half has been fixed.



 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Always use a context-relative path to the JSP starting with a / character. Otherwise, you are rolling the dice on whether the JSP will be found or not.
 
Jeremy McNally
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It still won't work when I use the slash.

Please see the Server Code below: Maybe this will say something that I am just not seeing perhaps...


 
Jeremy McNally
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I actually just fixed this mess on my own. I added a 'check' in the else part. Here is what I did below:

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
407 is certainly not correct. What is that you want to happen on failing the check?
 
Jeremy McNally
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my code, IF invalid I need it to go to (login.jsp); IF valid go to (flightsearch.jsp). I fixed my issue by using the ELSE IF statement BUT in order to use that I needed the ELSE underneath that.
There was nothing else to put in the else{} part of it than what I did. IF nothing works persay, and for some reason it resorts to the 'else' part of it, the 407 status code will say Need Authentication and that would be the end of it.

If I went the way I did before where I did (if valid - go to (flightsearch.jsp) else - go to (login.jsp). The else part would give me the NullPointer Exception. When I changed it to IF and ELSE IF they both work fine but in order to use the ELSE IF, I needed to put an ELSE underneath it (at least from what I am aware of). That 3rd part the If statement is completely unnecessary but what else could I do?
That part will never need to be used anyway. It is more or less just a filler.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm looking at your code and befuddled why you think there are three conditions. You want to go to one of 2 locations, right? I think you need to sit back away form the code for a minute and write out in English what you are actually trying to do.
 
Jeremy McNally
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No Bear, you don't understand what I am saying.
I Know that there are not three conditions. There are two.

1.)

2.)


I know that these two conditions are ALL I need. The problem here is, "if" I don't use that 3rd part; the (else) part of it, than I 'still' get the NullPointerException. If I include that senseless part, than I don't get the NullPointer and it all works. Also given that, that part will never be used, I find it the ONLY way to fix my problem and is really just a filler.

Do you understand where I am coming from?

 
Jeremy McNally
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can give you the Zip file so you can run it on your machine (if you want). You will see exactly what I mean. But this is the only place where my issue lies and I have no clue why if I remove that pointless 3rd condition, why I get a NullPointerException. I should just be able to use and IF-ELSE statement but with that I "Still" get the NullPointerException.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't need a zip file. I (you) need a paragraph in English describing what you are trying to do, rather than trying to infer it from the code.

Worrying about the NPE at this point is premature. Getting the code factored down to a proper level will likely take care of that on its own.
 
Jeremy McNally
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I feel really stupid! Bear, you were more than right. All I needed to do was (write a more Pseudo-code version of it) take out the ELSE or ELSE IF part and just add a second IF statement for the other condition (leaving out that status code stuff). That was all my problem was. NOW my issue is 100% fixed and will probably be posting soon about Session Management depending on what happens. You gave me a hint via someone else's post but I am having a hard time making it work. All in all Bear, thanks for giving me some advice on the problem I just worked out. See what I did below:

 
Jeremy McNally
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear, I got your reply from my other topic and the IF and then the ELSE part of things you said would be more than fine is what I 'originally' had from the start of everything before I started doing other things.
When I had it like that, I would ONLY get the 'else' part of it to work. Otherwise I would get a NullPointerException.
I pasted what I just did that fixed everything and got rid of the 'un'necessary 3rd pointless condition I had. Just 2 IF statements made everything work and got rid of the pointless parts.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you got that part straightened out? That's good!

It's true that working code is always good, but... when you have code like this:

if (X) do something;
if (not X) do something else;

then you should revise it to look like this:

if (X) do something
else do something else;

So in your case:


Why is this important? After all, the code works, so why mess with it?

It's because later you might have to come back and modify your boolean condition which decides where to send the user. If you have that condition in the code twice, then you would have to change it in both places. It might happen that you only changed one of the two places, and that would introduce a bug. Or you might change the two places differently, and that would introduce a bug. And it's worse when the code in the two places isn't the same, as in your case. You might change the first one in one way and the second one in a different way. Again a bug. In general duplicate code isn't good because it's a problem waiting to happen.

 
Jeremy McNally
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The thing is, is that don't have more than one place where this code is located unless you mean something more in-depth and if so would you elaborate Paul?
Like I said before when I do what you are talking about I 'always' have an issue. If you have a solution I would really like to hear it because the only thing that is working 'just' fine right now is the 2 if statements instead of one if-else statement. The if-else statement is what I 'originally' had and should work BUT doesn't.
 
Jeremy McNally
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay actually I see more what you mean... you are talking as if I had to go back to it and I only changed ONE condition instead of two it would introduce a bug. More opportunity for error. I see. That is not my ideal situation still but I really can't see another solution at this point unless you have an idea. if-else is perfect but no luck.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Refactor the code to use the single condition and post back if there are problems. It is impossible that the code would stop working if you replace the second condition with an else and introduce no other errors.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!