This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes JSP and the fly likes Mixing JSP and Scriptlets throws compilation errors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSP
Bookmark "Mixing JSP and Scriptlets throws compilation errors" Watch "Mixing JSP and Scriptlets throws compilation errors" New topic
Author

Mixing JSP and Scriptlets throws compilation errors

Uma Bandaru
Ranch Hand

Joined: Jul 30, 2009
Posts: 31
Hi,

I have been mixing scriptlets and html in my code as follows:

<% if (%> <jsp:getProperty name="job" property="status"/> <% == "hot"){%>
<option value="hot" selected="true">Hot </option>
<% } else { %>
<option value="hot" selected="true">Hot </option>
<% } %>

Which I assumed is perfectly doable.
But when I try to run the application, it is throwing compilation errors.

Error(20,26): Syntax error, insert ") Statement" to complete IfStatement
Error(20,78): Syntax error on token "==", invalid (
Error(20,78): The left-hand side of an assignment must be a variable
Error(20,86): Syntax error, insert "AssignmentOperator Expression" to complete Assignment
Error(20,86): Syntax error, insert ";" to complete Statement
Error(22,26): Syntax error on token "else", delete this token

Any suggesstions?

Thanks, Uma

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 63202
    
  69

Uma Bandaru wrote:Which I assumed is perfectly doable.

If you mean scriptlets and HTML, then sure -- if this were 2001. But it's 2011 and scriptlets have been discredited for almost 10 years now. You should be using the JSTL and EL instead.

But irrespective of the year, it's not scriptlets and HTML you are mixing but scriptlets and JSP actions, which is not doable in any decade.

Any suggesstions?

As stated above. It's time to update your JSP knowledge. JSP 2 was introduced in 2002 -- it's time to catch up.

In JSTL and EL, the following:
becomes

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Uma Bandaru
Ranch Hand

Joined: Jul 30, 2009
Posts: 31
Thanks for the response. I am new to jsp el. Can you please elaborate on this?
After <c:if test="${job.status == 'hot'}"> , can I use "test" as a boolean variable?
Like,

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 63202
    
  69

Yes, if you check the docs for <c:if> you will see that you can assign it's result to a scoped variable.
Uma Bandaru
Ranch Hand

Joined: Jul 30, 2009
Posts: 31
Now, I changed my code to:


But I am not able to view anything inside the <select> tag in the page
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 63202
    
  69

Is the generated HTML valid?
Uma Bandaru
Ranch Hand

Joined: Jul 30, 2009
Posts: 31
This was the HTML that was generated:

<tr>
<td width="15%">Degree</td>
<td width="25%">

<select name="degree">



<option value = "hot">
</option>






<option value = "medium">
</option>





<option value = "cold">
</option>


</select>
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 63202
    
  69

And does that not explain why the options are empty?
Uma Bandaru
Ranch Hand

Joined: Jul 30, 2009
Posts: 31
Right, that was silly.. Thanks a lot, it works now.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 63202
    
  69

Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1517
    
    6
Just as an addition to this, rather than writing out such code, I prefer to use a tag which handles it for me like the ones provided by struts/stripes.
ie give it a list of options, and tell it the option that is selected, then let the component render the HTML for it.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 63202
    
  69

Indeed, I've written many such tags. One for example, is given a Map instance that uses the Map keys as the option value attributes, and the Map values as the option text. Another takes an enum class and constructs a set of option elements from the enum definition.
 
Have you checked out Aspose?
 
subject: Mixing JSP and Scriptlets throws compilation errors
 
It's not a secret anymore!