Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

how to display errors in servlet

 
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Sheriff
Posts: 67601
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67601
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67601
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1143
    5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Ranch Hand
    Posts: 171
    Hibernate Spring Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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: 1143
    5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    okay..
     
    Bear Bibeault
    Sheriff
    Posts: 67601
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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: 171
    Hibernate Spring Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Sheriff
    Posts: 67601
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    You showed up just in time for the waffles! And this tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    reply
      Bookmark Topic Watch Topic
    • New Topic