File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Error resolving JSP variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Error resolving JSP variable" Watch "Error resolving JSP variable" New topic

Error resolving JSP variable

John Lawrenson

Joined: Apr 27, 2012
Posts: 1
I am new to JSP, but I am trying to execute the following code on my page:

${val.content_type}<img src="<%=request.getContextPath()%>/<%=application.getInitParameter(val.content_type)%>">

Tomcat returns an error "val.content_type cannot be resolved to a type"

If I comment out "<%=application.getInitParameter(val.content_type)%>", ${val.content_type} gets displayed correctly on the page as "application/pdf"
If I execute <%=application.getInitParameter("application/pdf")%>, I get the expected results.

What an I missing?

Thank you.

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63844

Welcome to the Ranch.

Your page mixes EL with Java scriptlets?

Is this something you are writing, or someone's mess you inherited?

Modern JSP should not contain any scriptlets, but legacy pages that were created 10 years or so ago will contain them.

But mixing modern EL with antiquated scriptlets is just a big old mess.

But to address your problem, removing/adding a scriptlet shouldn't have such odd effects. Once we find out where this is coming from, we'll likely need more info to diagnose.

[Asking smart questions] [About Bear] [Books by Bear]
Stefan Evans

Joined: Jul 06, 2005
Posts: 1580
EL expressions and java expressions evaluate in different contexts.

${val.content_type} is approximately equal to <%= pageContext.findAttribute("val").getContent_type() %>
<%= val.content_type %> has no meaning/value - and indeed raises an error as you have seen.

The offending expression:

because you are attempting to evaluate EL in a java scriptlet expression.

You could convert this expression completely to EL like this:

Or potentially better using JSTL to add the context path:
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63844

That's certainly the way I'd approach it.
I agree. Here's the link:
subject: Error resolving JSP variable
It's not a secret anymore!