Get your CodeRanch badge!*
The moose likes Servlets and the fly likes how to display errors in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "how to display errors in servlet" Watch "how to display errors in servlet" New topic
Author

how to display errors in servlet

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 939
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

Joined: Jan 10, 2002
Posts: 60080
    
  65

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.)


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 939

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

Joined: Jan 10, 2002
Posts: 60080
    
  65

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

Joined: Aug 20, 2011
Posts: 939
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

Joined: Jan 10, 2002
Posts: 60080
    
  65

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

    Joined: Aug 20, 2011
    Posts: 939
    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

    Joined: Mar 30, 2012
    Posts: 124

    one more way is you can set an attribute in HttpServletRequest & show the error message depending on that.
    Punit Jain
    Ranch Hand

    Joined: Aug 20, 2011
    Posts: 939
    okay..
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60080
        
      65

    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

    Joined: Mar 30, 2012
    Posts: 124

    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

    Joined: Jan 10, 2002
    Posts: 60080
        
      65

    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.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: how to display errors in servlet
     
    Similar Threads
    Session variables in Netscape 4.76?
    Not able to navigate the user to another jsp in servlets using RequestDispatcher
    MVC example | wrox press ch06
    Login - revisited
    Redirect to the requested page failed using form-based authentication