File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Login servlet not redirecting to JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Login servlet not redirecting to JSP" Watch "Login servlet not redirecting to JSP" New topic
Author

Login servlet not redirecting to JSP

James Haville
Greenhorn

Joined: Jul 19, 2010
Posts: 10
Hello,

I am implementing a simple login example and having some difficulty redirecting to the success page if the username and password match with those in my database.
When I press submit I get a blank page. Would appreciate any thoughts.

My code:

LoginPage.jsp



LoginServlet.java



UserLogged.jsp



web.xml



UserDao.java



UserBean.java and ConnectionManager.java are also in the ExamplePackage

Thanks in advance! James
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

What appears in the URL bar of the browser? What does a View Source show? Any errors in the logs?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
James Haville
Greenhorn

Joined: Jul 19, 2010
Posts: 10
Bear Bibeault wrote:What appears in the URL bar of the browser? What does a View Source show? Any errors in the logs?


I simply forgot to include an import

Thank you for the help.
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 274

In the page directive, remove the ".java".

Here are some comments on your code:

1. In UserLogged.jsp, in the page directive, you don't need the "language" attribute. The only possible value for this is "java", so it's redundant to include it. This was included to allow for languages other than Java to be used in JSP files, but so far only Java is supported.

2. You should avoid using scriptlets in JSP files. In UserLogged, you should use the useBean and getProperty standard actions instead:

3. In UserDao.java, you should use PreparedStatement instead of Statement to execute the database query. This helps prevent SQL Injection attacks, which your code is vulnerable to. What do you think would happen if the user's password had a single quote in it?


SCJP 6 || SCWCD 5
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Michael Angstadt wrote:you should use the useBean and getProperty standard actions instead:

Better yet, use the EL. The getProperty action is clunky and outdated for emitted dynamic values.

But absolutely: scriptlets have been discredited for over 8 years now. There is no excuse or reason for placing then into newly written JSP pages.
James Haville
Greenhorn

Joined: Jul 19, 2010
Posts: 10
Thanks for the comments. This is my first attempt at a web application so it is all new to me but trying to learn as much as possible. I have a few further questions if you wouldn't mind.

I have my page layout sorted with header and footer jspfs for each and am able to log a user if I insert them into my database so I want to implement a create user function next.

1) From some research one of the ways to implement this would be to create a controller servlet which handles requests from the client. Would I have two methods in this servlet doGet(...) and doPost(...) which handle incoming requests according to the userPath (i.e. - request.getServletPath();) and then call methods based on what the request is and then redirect the user to the correct page? Is this an acceptable way to do it? Any other comments on this would also be welcome baring in mind I would like to avoid frameworks for the moment.

2) With the create user method itself what would be the best way of implementing it...(may be a question for another part of the forum)



that would be in my UserDao...if someone could clarify the process from the client request to create a user it would help me out as im getting a little confused as to how the information is passed end to end mainly the Beans part.

Please bear with me I have been learning Java for less than a year and did no programming prior to that. Having said that this forum is one of the best resources I have come across.

Thanks again.
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 274

James Haville wrote:2) With the create user method itself what would be the best way of implementing it

You have a good start. For the SQL that you pass into prepareStatement(), use question marks to denote where a parameter should go. If a parameter is a string, you don't need to put single quotes around the question mark, JDBC handles this for you. For example:


James Haville wrote:if someone could clarify the process from the client request to create a user it would help me out as im getting a little confused as to how the information is passed end to end mainly the Beans part.


You would just create a HTML form with the proper fields (first name, etc), then pull those parameters out in your servlet and pass them in your UserDao.
Lawrence Xu
Greenhorn

Joined: Feb 11, 2004
Posts: 11
you response to error.jsp?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Login servlet not redirecting to JSP