Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Login won't redirect

 
stevan pivnicki
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After login the user should be redirected to another page but nothing happens.





 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65340
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you verified that the redirect statement is executing?
 
stevan pivnicki
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Have you verified that the redirect statement is executing?


How to verify ? What do you mean by that ?
 
Tapas Chand
Ranch Hand
Posts: 583
8
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With the help of loggers you can verify whether the statement is executing.
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Few things about your code.
1) If the user is null, nothing will happen. There is no indication of wrong username or password.
2) If an exception is thrown, again nothing might happen (based on your web.xml).
3) Why send a redirect? Redirect is asking the "browser" to go visit some other page. I don't know the requirement but usually a forward makes more sense.
4) HTTP GET requests are usually idempotent, the login logic should be using POST method.
 
stevan pivnicki
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tapas Chand wrote:With the help of loggers you can verify whether the statement is executing.


I see what you mean. I was thinking of using loggers but I used simple System.out.println() to print out the user if he's in the database, just to see if the code is actually working.

@Amit Ghorpade

Thanks for the tips. Yes I know that GET method is not the right thing for log in but this project is for exercise purpose only plus I wanted to see in the url if the values from the form are passed.
I agree that POST is much more effective in the real type of situation.


Anyway, I managed to do the redirect user but only to JSP page, I still can't make the redirection to another servlet.
 
Tapas Chand
Ranch Hand
Posts: 583
8
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please go through the "sendRedirect" method in Oracle site
http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletResponse.html
I think you are missing the trick with URL patterns (HINT:forward slash "/").
 
stevan pivnicki
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tapas Chand wrote:Please go through the "sendRedirect" method in Oracle site
http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletResponse.html
I think you are missing the trick with URL patterns (HINT:forward slash "/").


Thanks for the link.


Anyway I have another problem, I'm getting error HTTP Status 500 - java.lang.NullPointerException.

So the user is logged in and redirected to the WebShopPage.jsp and he needs to see list of products but instead of that the error shows.

WebShopServlet.java



WebShopPage.jsp

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65340
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are redirecting directly to the JSP, you're skipping the controller which sets up the data. Your JSPs should never be accessible via URL, and should not be the target of redirects. Redirect to the controller servlet.

Please read this article for more information on properly structuring Java web apps.

You also shouldn't be using scriptlets in a JSP. That's technology that has been obsolete for 14 years!
 
stevan pivnicki
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:If you are redirecting directly to the JSP, you're skipping the controller which sets up the data. Your JSPs should never be accessible via URL, and should not be the target of redirects. Redirect to the controller servlet.

Please read this article for more information on properly structuring Java web apps.

You also shouldn't be using scriptlets in a JSP. That's technology that has been obsolete for 14 years!


Nope, I'm not redirecting it directly to JSP, the login form calls the LoginServlet and it looks like this



My project contains servlet package, dao package, class package and of course WebContent folder for the JSP and HTML pages.


As for the scriplets, I need that way to do it for the test I'm having in a few days.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65340
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"test" means this is an academic exercise. It's best to point this out in the beginning so people understand you are under constraints that do not exist in the real world.
 
stevan pivnicki
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...this project is for exercise purpose...


Yeah I mentioned that in a few posts back.
 
stevan pivnicki
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK so I found out that my session is null. I owe thanks to this thread http://www.coderanch.com/t/292838/JSP/java/check-session-exists

So I have set session in servlet but I have done the same on JSP page.

Nevertheless I'm still curious why did the session set itself to be null, does anybody know that ?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65340
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any reason you are not just using the implicit ssession scripting variable?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic