• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1010
  • Last Modified:

error while using ternary operator in jsp

whats wrong with this

<input type="text" searchField="true" class='text' maxlength="2"  size="2"  value='<%=out.println(request.getParameter("countryId") != null ? request.getParameter("countryId") : "" )%>'         name="countryId"

i am getting this error

Incompatible type for method. Can't convert void to java.lang.Object. out.print(out.println(request.getParameter("countryId") != null ? request.getParameter("countryId") : "" ))

chaitu chaitu
chaitu chaitu
1 Solution
chaitu chaituAuthor Commented:
i solved the problem like this

<input type="text" searchField="true" class='text' maxlength="10"  size="10"  value='<%out.println(request.getParameter("countryName") != null ? request.getParameter("countryName") : "" );%>'   name="countryName" >

expression is a combination of variable and operation that final return only one value.

You use this
out.println(request.getParameter("countryId") != null ? request.getParameter("countryId") : "" )
It's not expression since it return void. jsp expression cannot accept method that return void.

If you really want to use expression instead of scriptlet, I think it should be
<%=request.getParameter("countryId") != null ? request.getParameter("countryId") : ""%>
ok...for your reference,

whatever you give inside the <%= and %> is considered to be a out.print
i.e. <%=yourContent%> itsellf refers to out.print(yourContent);

So, this is the way normally developers use:

<input type="text" searchField="true" class='text' maxlength="10"  size="10"  value='<%=request.getParameter("countryName") != null ? request.getParameter("countryName") : ""%>'  name="countryName">
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

haaa....didnt noticed your comment sampol..
For more clarification about jsp,

when you create jsp file and deploy to server. the server will translate jsp to servlet line by line.


will be convert to

PAQed, with points refunded (50)

Community Support Moderator
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now