• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Accessing a variable from both EL and JSP Expression?

 
Greenhorn
Posts: 13
Tomcat Server Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a JSP script where I use JSTL to loop over a Result object which contains a set of database records. All I'm doing is printing the field values of each record except for one field which contains a user id. I need to look up the user name in another table from the user id. To look the username up I have a class UserUtil with a static method getUsername(int userID). How can I call this method? What I have below doesn't work. I can't think of a way to get the EL ${link.userid} variable into the JSP expression.

 
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean by "it doesn't work"?

Are you getting an error? Can you share that with us?

Note: I don't know off-hand if you can do what you're trying to do or not, but the error you're getting may just tell us.

 
Sheriff
Posts: 67754
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
Mixing the JSTL and EL with scriptlets is a recipe for disaster to begin with. But you most certainly cannot embed EL expression within scriptlets.

My advice is to choose to either use scriptlets (not recomeended) or to use the JSTL/EL. Mixing them is either going to be outright invalid, or a source of endless problems.
 
Mike Heffelfinger
Greenhorn
Posts: 13
Tomcat Server Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the help.

@Mark - The error I get is:

An error occurred at line: 12 in the jsp file: /WEB-INF/jsp/getLinks.jsp
Syntax error on tokens, delete these tokens



And like Bear said, I just can't embed an EL expression in scriptlets.

@Bear - I'm definitely trying to use JSTL/EL, but I still can't think of a way to look up the username with the userID I have in my JSTL loop. I'll keep thinking.
 
Bear Bibeault
Sheriff
Posts: 67754
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
What does UserUtil.getUsername() do?

How can it be re-jiggered to fit better into an EL-focused page? Is it something that can set up in the page controller prior to even calling the page? Would an EL function be needed? A custom tag? Can it be the property of some bean?

And so on... creating modern JSP pages means thinking about them differently than when scriptlets were used.
 
Mike Heffelfinger
Greenhorn
Posts: 13
Tomcat Server Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I need to modify my select statement in my existing controller to join my links table and users table, then my result set in the view JSP will have the username too instead of just the userID. I can continue using JSTL/EL in the view. Thanks for your help Bear, you got me thinking and on the right track. Also, thanks to Mark too.
 
Bear Bibeault
Sheriff
Posts: 67754
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
Now you're on the right track. The key is to make things as easy as possible for the JSP. The more prep you can do in the controller, the better off you'll be once you get to the JSP.
 
Mark E Hansen
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Normally you'll want to have a Task Controller Servlet and a Page Controller Servlet. The task controller would be the place where you implement the business logic (perform the actions that result from the page submitted by the user). The page controller is where you prepare all the (normally session) data for display by the JSP page. This is where you would do what you're talking about with your SQL query, joining the user data, etc.

One of the benefits you gain by having two separate task/page controller Servlets is the ease with which you can implement the Post Redirect Get (PRG) design pattern (if you haven't heard of this yet, it's in the FAQ).

I hope it's making more sense. It took me a little while to get the whole MVC, Page/Task Controller model through my thick skull, but once I did I was able to see the great benefits.

Best Regards,

 
Bear Bibeault
Sheriff
Posts: 67754
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

Mark E Hansen wrote:One of the benefits you gain by having two separate task/page controller Servlets is the ease with which you can implement the Post Redirect Get (PRG) design pattern (if you haven't heard of this yet, it's in the FAQ).


As well as this article!
 
Mike Heffelfinger
Greenhorn
Posts: 13
Tomcat Server Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm using the Model 2 Front Controller Pattern (with the Front Man framework). I use a lot of AJAX too (JQuery library), so many of my JSPs return only HTML snippets, and Front Man works well in this scenario.
 
Bear Bibeault
Sheriff
Posts: 67754
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
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
+1 that this should all be done outside of the JSP--also consider that if the UserUtil is doing a DB lookup it's doing it each time through the loop, which is measurably more expensive than doing it all at once.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic