fumeng
fumeng

Reputation: 1830

The only proper use of a scriptlet in JSP

Let me preface this post by saying the following (so no one marks it as a duplicate).

-- I know that, per the JSP specification, that scriptlets in JSP pages should be avoided at all costs. And that their purpose, if necessary, can be to transform data returned from processing the client's requests into a proper client-ready format.

So, my question is this: Does the following case fall into that category? Is it an appropriate use of a scriptlet?

At the top of my JSP page I use a scriplet to capture server-side data:

<% User user = User.getUser(request); %>

Then in the JSTL I use a scriptlet to reference this object:

<c:set var="userName" value="<%=user.getLoginName()%>" />

Later on when I'm submitting my form via AJAX I populate a value using EL:

params.userName = "${userName}";

I know that using scriptlets in JSP is considered very bad form but how else can I get the User object?

Do I need to force the backend developer to pass that value back to me so I can reference it via JSTL? I'm guessing that is what I must do.

Upvotes: 2

Views: 174

Answers (1)

Dave Newton
Dave Newton

Reputation: 160251

Expose the user to JSP in the Java code and retrieve it via EL.

That can be done with the servlet backing your JSP, a filter, etc. depending on what framework you're using. There's no reason to use scriptlets in this case.

Upvotes: 2

Related Questions