aspose file tools*
The moose likes JSP and the fly likes Conditionally Handling Dynamic Content Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Conditionally Handling Dynamic Content" Watch "Conditionally Handling Dynamic Content" New topic
Author

Conditionally Handling Dynamic Content

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

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.


GenRocket - Experts at Building Test Data
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
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.


Matthew Phillips
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

Are there any cleaner approaches to this than what I mentioned?
Does struts handle this any differently?
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
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.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

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?
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
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.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

Thanks Jason.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Conditionally Handling Dynamic Content