• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to display errors in servlet

 
Punit Jain
Ranch Hand
Posts: 1014
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have to design a login page.
what i am doing is, i have designed:
login.jsp:- has the login form.
loginCheck.java:- taking the text field values, and creating object of class authenticate.java and calling the authentication method of authenticate class, and redirecting to the appropriate page based on (login is correct or incorrect).
Authenticate.java:- has the method to authenticate user.

but What i want is when user login with invalid id and password, it should redirect to login.jsp with an error message
if i use only jsp pages, i can do this something like this:

In my login.jsp


In my loginCheck.jsp



but the problem is for checking user authentication, i am not using jsp, but using servlet, so how can i do the same thing in servlet??
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65120
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are eventually going to re-display the login page right?

If only there a way that a servlet controller could pass information to the JSP. Oh wait! There is!

So, how does a servlet controller pass info to its JSP? (Which, of course, should not contain any Java scriptlets.)
 
Punit Jain
Ranch Hand
Posts: 1014
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You are eventually going to re-display the login page right?

Yes

just now what i tried is i created one more jsp, which has:
CheckLogin.jsp


and if the login fails i am redirecting to this jsp, like this:


it redirecting to CheckLogin.jsp, but not again to Login.jsp..
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65120
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That all seems pretty twisted. Have you read this article that shows how data should flow in a modern web app?
 
Punit Jain
Ranch Hand
Posts: 1014
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes i read this article.
but now if i change my whole application, it looks something complex.
whereas i only want to show the error message.?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65120
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't think that its overly complex now? If you can't display a simple error message, what does that say about the structure of your application the way it is?

There doesn't need to be any over-complexity to follow best practices:

  • The login forms submits to a task controller that checks the incoming data and authenticates the user.
  • If successful, redirects to the page controller for the main page of the app.
  • If it fails, records the failure in the session and redirects to the page controller for the login form.
  • The page controller does whatever prep is necessary, and forwards to the login form.
  • On the login form page, JSTL and EL (not Java scriptlets) check to see if there's an error recorded on the session and display the error information if present.
  • The error information is removed from the session as it's already been displayed.

  • Easy as pie.
     
    Punit Jain
    Ranch Hand
    Posts: 1014
    2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    woww, nicely done..

    The login forms submits to a task controller that checks the incomgin data and authenticates the user.
    If successful, redirects tot he page controller for the main page of the app.
    If it fails, records the failure in the session and redirects to the page controller for the login form.
    The page controller does whatever prep is necessary, and forwards to the login form.
    On the login form page, JSTL and EL (not Java scriptlets) check to see if there's an error recorded on the session and display the error information if present.
    The error information is removed from the session as it's already been displayed.


    i was also doing in the same way, the only thing was to display the error, but now done.
    also i was using scriptlets.
     
    Anurag Verma
    Ranch Hand
    Posts: 168
    Hibernate Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    one more way is you can set an attribute in HttpServletRequest & show the error message depending on that.
     
    Punit Jain
    Ranch Hand
    Posts: 1014
    2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    okay..
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Pie
    Posts: 65120
    91
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Anurag Verma wrote:one more way is you can set an attribute in HttpServletRequest & show the error message depending on that.

    The problem with that approach is that the error is detected before a redirect. Request attributes won't be carried across the redirect.
     
    Anurag Verma
    Ranch Hand
    Posts: 168
    Hibernate Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Oops....i missed to mention that after setting the attribute in your request, forward it to the jsp & don't redirect. I can't see need of redirection atleast in this situation. Thanks Bear.
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Pie
    Posts: 65120
    91
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Anurag Verma wrote:Oops....i missed to mention that after setting the attribute in your request, forward it to the jsp & don't redirect. I can't see need of redirection atleast in this situation. Thanks Bear.

    Of course there is. Otherwise hitting refresh will replay the action.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic