Search code examples
javahtmljspscriptlet

handling null values in text box in jsp


Say a text box takes value from Transfer Object as below in a jsp:

<INPUT TYPE="text" NAME="test" ID="test" value="<%=testTO.getTest()%>">

However, if getTest returns null, null will be displayed.

How to use ? : with scriptlet so that if the value is null, blank is displayed else the value returned from TO.


Solution

  • use it like this for printing blank:

    <INPUT TYPE="text" NAME="test" ID="test" value="<%= ((testTO.getTest()==null)?"":testTO.getTest()) %>">
    

    ok add this condition

    (&& testTO.getTest().length() == 0)
    

    if it is already returning a String if not you must use this

    (&& testTO.getTest().toString().length() == 0)
    <INPUT TYPE="text" NAME="test" ID="test" value="<%= ((testTO.getTest()==null && testTO.getTest().length() == 0)?"":testTO.getTest()) %>">