This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
When retrieving data from some location, be it user input from forms or a database or whatever, and giving that information back to a JSP page, if the JSP page needs to display a little bit of different data depending on the data sent to it, what is the cleanest approach to handling this? 1. Use scripplets with if statements to basically echo the appropriate HTML back 2. In the Servlet or whatever I use to get the data, have different JSP files for different data and call the appropriate JSP file accordingly. For an example, on a user login page, when the page is loaded initially, there is a variable that is probably null, so the JSP login page knows that the user hasn't tried to log in yet. But when the user does try and login, and fails, some form of data is returned to the login page that says, this person failed, display a failed message on the JSP login page. So there is still just one login.jsp page, but the data determines whether or not specific HTML is shown. I hope I didn't ramble to much and that makes some sense. Thanks.
I would probably use a scriptlet for your example. It would really depend on how much the content is going to change. If the scriptlet were one or two lines long, I would probably use it. If there were substaintial changes, I would probably isolate the changes into seperate files and then include them into a template based the conditional logic.
Actually I would use conditional tags, such as from JSTL or the Struts taglib, as opposed to scriptlets. With Struts you could do a couple of different things. In struts, all user requests should be going through the controller and handled by some Action. In many cases your Action will decide which view the user should be forwarded to. Let's say I have a document which could be either read-only, or editable depending on the user requesting to view it. In my Struts Action I would determine whether or not the user had edit priveldges, and then forward to the correct view, either a JSP that simply showed the content of the document, or a JSP that allowed the user to edit the document. On the other hand, for some things I will use the same view and use jsp tags to determine whether or not to show certain parts of that view. For example, I might only display a menu option on a page if that user was allowed to execute that option. Or maybe I will show a shopping cart icon only if the user has something in their shopping cart. Regarding your scenario, the Struts way to handle it would be to allow have a login Action process the user's attempt to login. If the Action determined the login was invalid, maybe because the password wasn't correct, the Action would place an ActionErrors object (a collection of ActionError objects which each equate to an error message) containing the appropriate ActionError objects into scope with and return back to the login form. The login form would use jsp tags to determine that an ActionErrors object existed, and would display the messages indicated by each ActionError object to the user.
Thanks Jason. That makes a lot of sense. Regarding: On the other hand, for some things I will use the same view and use jsp tags to determine whether or not to show certain parts of that view. For example, I might only display a menu option on a page if that user was allowed to execute that option. Or maybe I will show a shopping cart icon only if the user has something in their shopping cart. Is this where you would use conditional tags from the JSTL?
Joined: Nov 09, 2000
Originally posted by Gregg Bolinger: Is this where you would use conditional tags from the JSTL?
Exactly. JSTL tags or maybe Struts tags. For example there is a Struts tag <html:errors/> that checks for errors and displays them if there are any. IMHO, anybody developing web apps of any complexity in Java should check out Struts. It seems to have become pretty much the standard way of architecting Java based web apps these days.