0

Hello i am New jsp i want to check condition in jsp. whether value is null or not.? i have write following code in jsp page

   <% String s = request.getParameter("search"); %>
    <%=s %>
    <% if (<%=s ==null) { %> 
     <div>textbox is empty</div>

   <% } else { %>
   <div>textbox value..
    <% } %>

i get textbox value in variable if textbox value is null then it should display first message othervise second. tell me how to do?

jlordo
  • 37,490
  • 6
  • 58
  • 83
Kapil
  • 320
  • 6
  • 10
  • 23
  • 1
    By the way, the best practice is to avoid using scriptlet in your jsp. http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files – Drogba Apr 24 '13 at 07:36
  • This is more of a syntax problem - I think once Rajan gets his answer, the question ought to be deleted. – aquaraga Apr 24 '13 at 07:37
  • your question say if textbox value is null then execute `if` otherwise `else`? what is your textbox value? value of s ? – harsh Apr 24 '13 at 07:40

6 Answers6

9

The best way to do it is with JSTL. Please avoid scriptlets in JSP.

<c:choose>
  <c:when test="${empty search}">
   <div>textbox is empty</div>
  </c:when>
  <c:otherwise>
    <div>textbox value is ${search}</div>
  </c:otherwise>
</c:choose>
Community
  • 1
  • 1
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
5
    <% String s = request.getParameter("search"); %>
    <%=s %>
    <% if (s==null || s.isEmpty()) { %> 
     <div>textbox is empty</div>

   <% } else { %>
   <div>textbox value..
    <% } %>
harsh
  • 7,502
  • 3
  • 31
  • 32
  • if i dont insert value in text box then also it also go to else part – Kapil Apr 24 '13 at 07:37
  • 1
    @Rajan can you check what value of `s` is printed? it might not be `null` but empty `""` or having spaces. check modified answere.. – harsh Apr 24 '13 at 07:42
3

Does it even compile? <% if (<%=s ==null) { %> should at least be

<% if (s == null) { %>

If you want to check for empty string as well, do

<% if(s == null || s.trim().length == 0) { %>
NilsH
  • 13,705
  • 4
  • 41
  • 59
2
<% String s = request.getParameter("search"); 
     if (s ==null) { %> 
     <div>textbox is empty</div>

   <% } else { %>
   <div><span><%=s%></span></div>
    <% } %>

edited to include empty string

<% 
       String s="";
     if(request.getParameter("search")!=null)
      {
          s=request.getParamater("search");
      }
     if(s.trim().length()==0)
        {%>
           <div>Empty Field</div>
        <%}
          else{%>
              <div><span><%=s%></div>
           <%}%>
ntstha
  • 1,187
  • 4
  • 23
  • 41
1
<% String s = request.getParameter("search"); %>
    <%=s %>
    <% if (s ==null) { %> 
     <div>textbox is empty</div>

   <% } else { %>
   <div>textbox value..
    <% } %>
Alpesh Gediya
  • 3,706
  • 1
  • 25
  • 38
0

In jsp, it is easy to check whether a variable is empty or not.

String search;
if(search.isEmpty()){
out.println("The variable is empty ?");
}
else{
out.println("The variable is Not empty ?");  
}
Vishal Yadav
  • 1,020
  • 4
  • 15
  • 30
Bhagawat
  • 468
  • 4
  • 12