Link to home
Start Free TrialLog in
Avatar of chaitu chaitu
chaitu chaituFlag for India

asked on

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") : "" ))

Avatar of chaitu chaitu
chaitu chaitu
Flag of India image

ASKER

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" >
Avatar of sompol_kiatkamolchai
FYI

<%=expression%>
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">
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.

<%="abc"%>

will be convert to

out.println("abc");
ASKER CERTIFIED SOLUTION
Avatar of modulo
modulo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial