File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Accessing a variable from both EL and JSP Expression? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Accessing a variable from both EL and JSP Expression?" Watch "Accessing a variable from both EL and JSP Expression?" New topic
Author

Accessing a variable from both EL and JSP Expression?

Mike Heffelfinger
Greenhorn

Joined: Jan 11, 2010
Posts: 13

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.

Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 650
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.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61760
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Mike Heffelfinger
Greenhorn

Joined: Jan 11, 2010
Posts: 13

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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61760
    
  67

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

Joined: Jan 11, 2010
Posts: 13

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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61760
    
  67

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

Joined: Apr 01, 2009
Posts: 650
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61760
    
  67

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

Joined: Jan 11, 2010
Posts: 13

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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61760
    
  67

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

+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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Accessing a variable from both EL and JSP Expression?