• Post Reply Bookmark Topic Watch Topic
  • New Topic

Showing error messages

 
Viv Singh
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a question about what is the "best/professional" way of displaying error messages in jsp.

At the moment I have the following:

Lets say I have a page from which you can create a new user and the request gets sent to a servlet.

If the user is successfully created and the data is stored in the database, my servlet sends a status code back as follows:



Now in my users.jsp I do the following:



I guess this is not a "good" way of solving the problem.
Any suggestions on a better way would be appreciated.

Thanks.
 
Nishan Patel
Ranch Hand
Posts: 689
Eclipse IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi,

This message you are giving on jsp and using java code to display it I think that is not a good way.

Make one servlet which set your Message according to your result and then pass your request using requestdispacher object and forward it.

So, using this you don't have to pass code 1 or 2 according to your result condition set your message String variable which set to request variable and get it request parameter at Jsp and use simple EL(Expression Language ) to display your message.

So, using this you remove all your scriplet code from jsp and it looks better for you.


 
Jilesh Lakhani
Ranch Hand
Posts: 49
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Best way is to use Ajax....

however with your environment you can either use form post, appending parameters to URL or sharing session between Servlet and JSP.
user.jsp will check if there is any session with id "errorMessage" if yes then display the error message else user.jsp will just show the error message.

so your control will be like
user.jsp ----- from submit--> servlet ---- if error---> user.jsp
user.jsp ----- from submit--> servlet----if success---> success.jsp
 
Nishan Patel
Ranch Hand
Posts: 689
Eclipse IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hello,

As Jilesh Lakhani Says

Best way is to use Ajax....


and
sharing session between Servlet and JSP.


But what I think for just displaying message use Ajax and session is not good to use.

Because Ajax is good when some data depends on another data of same page or then we don't need to refresh page we just refresh some part of page.

Or use a session is not good because we have to take care for session variable after using it we have to make it null otherwise it remain at application until some times.

So, If this will complete using simple request then I think no need to use some complex logic.



 
Jilesh Lakhani
Ranch Hand
Posts: 49
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yep that's true. You wanted to know the alternatives may be next alternative would be showing error on servlets or URL connection to JSP page you can do that too.
However I would still standby Ajax is best bet use it if you can in your application instead of going through the post cycle
 
Jilesh Lakhani
Ranch Hand
Posts: 49
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NOTE: its was Ajax or Session. and not ajax and session
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may find the way of error handling here interesting: http://balusc.blogspot.com/2008/07/dao-tutorial-use-in-jspservlet.html

Just use servlets and javabeans the right way.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!