Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error resolving JSP variable

 
John Lawrenson
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 64972
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Stefan Evans
Bartender
Posts: 1751
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 64972
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's certainly the way I'd approach it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic