Reputation: 350
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?
Upvotes: 0
Views: 46125
Reputation: 468
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 ?");
}
Upvotes: 0
Reputation: 49372
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>
Upvotes: 9
Reputation: 1173
<% 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>
<%}%>
Upvotes: 2
Reputation: 7692
<% String s = request.getParameter("search"); %>
<%=s %>
<% if (s==null || s.isEmpty()) { %>
<div>textbox is empty</div>
<% } else { %>
<div>textbox value..
<% } %>
Upvotes: 5
Reputation: 13821
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) { %>
Upvotes: 3
Reputation: 3794
<% String s = request.getParameter("search"); %>
<%=s %>
<% if (s ==null) { %>
<div>textbox is empty</div>
<% } else { %>
<div>textbox value..
<% } %>
Upvotes: 1