0

I want to dispaly a result parameter which return from servlet to jsp after an event happen. so when i add new user for example a result parameter returns with value that the user has been added successfully , and i want to display the result in jsp page. i make like this in jsp page

<%String result = String.valueOf(request.getAttribute("result"));%>
 <%if (result != null){%>
 <%= result%>
<%}%>

THE PROBLEM is that every time i open the page for the first time result prints as null to the browser even i write if not null print result value, where is the problem ? can someone help/

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
palAlaa
  • 9,500
  • 33
  • 107
  • 166

1 Answers1

2

String.valueOf(..) returns "null" (a String with length 4) if the argument is null. So your quick solution would be not to use String.valueOf(..) at all.

However, it is not advisable to use scriptlets and java code like that in your JSPs. Use JSTL and EL instead. Your code would look like this:

<c:if test="${result != null}">
    ${result}
</c:if>
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • it doesn't work even i make if(result.equals("null")){print result} – palAlaa Oct 20 '10 at 21:05
  • I advised for getting rid of the `String.valueOf(..)` completely. There is no point in comparing a string to the "null" string – Bozho Oct 20 '10 at 21:06
  • many thanx , but why scriptlets are not advisable in jsp pages? – palAlaa Oct 20 '10 at 21:10
  • 2
    @Alaa - see this question http://stackoverflow.com/questions/2188706/how-to-avoid-using-scriptlets-in-my-jsp-page and BalusC's answer – Bozho Oct 20 '10 at 21:11
  • 2
    Actually, it's this one :) http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files The OP was however told/linked that several times before in his previous questions. – BalusC Oct 20 '10 at 22:15
  • @BalusC, ah, yes. :) I wondered where did all those upvotes go. Anyway, there are too many of these. – Bozho Oct 20 '10 at 22:18