Link to home
Start Free TrialLog in
Avatar of hrappapo
hrappapo

asked on

How do I stop Tomcat from printing "null"?

If a value is null, I want Tomcat to display it in the webpage as blank.  Weblogic has a 'printnulls' option, which will do this.  How do I do this in Tomcat?
We have tried Tomcat 6.0.14 and 6.0.18 and both display "null" on the page.
I have heard that there are versions where nulls display as blanks.  Does anybody know the most recent version that does that?

Thanks.
Avatar of Mick Barry
Mick Barry
Flag of Australia image

how are you displaying it?

Avatar of hrappapo
hrappapo

ASKER

Below is sample code.
The input field ends up saying 'null'.  I want it to be blank.

Thanks.
Harris.


<%
HashMap h = new HashMap();
%>

<html>

<body>

<form>
<input type="text" value="<%= h.get("nothing") %>">
</form

</body>
</html>
Here are three ways to do it.  
<%@ page import="java.util.HashMap" %>
<%
  HashMap h = new HashMap();
  String s = (String)h.get("nothing");
  if(s == null)s = "";
  pageContext.setAttribute("h", h);
%>
<html>
<body>
   <form>
          <input type="text" value="<%= h.get("nothing") == null ? "": h.get("nothing")%>">
          <input type="text" value="<%=s%>">
          <input type="text" value='${h["nothing"]}'>
   </form
</body>
</html>

Open in new window

Thanks.  
But I need a solution that doesn't involve changing any of my code.
We were using weblogic with the 'printNulls' attribute set to 'false' and fields like this were showing as blank.
We ported our code over to Tomcat and now 'nulls' are showing up everywhere.
We need a universal solution.
I have heard that some previous versions of Tomcat did what I'm looking for (displayed blank instead of 'null' for null values) but I don't know which is the latest version that does that.
Here are some other postings I've found of people having the same problem I am:

http://www.junlu.com/msg/117417.html

http://forums-beta.sun.com/thread.jspa?messageID=2920066
Maybe objects has an idea. He will probably be around soon.
I finally thought of something. I guess you create a Filter and a HttpServletResponseWrapper to replace the substring
value="null"
with
value=""
You could do something like is done at
http://balusc.blogspot.com/2007/12/whitespacefilter.html 
We could help you.  
From memory the behaviour change with tomcat 5
only way I can see you can fix that without changing your code is to patch tomcat.

a filter would do it but has the danger of stripping null from the responses where it is not meant to.

Good point about the filter.  I only want to strip the null when it comes time to display values on a webpage.

Any idea what the patch for tomcat would be?  Or how I could find out?  Or where I could get it?
not off the top of my head, would have to go digging thru the source for that

Would you be willing to go digging thru the source (I assume you mean the Tomcat source code) to find this?  Or can you tell me how to do that?  I've never made changes to the Tomcat source code, so I don't even know where to begin.  Thanks.
Sorry, I'm flat out with client and student work at the moment.


@hrappapo:

In tomcat conf/web.xml there are few init params like "trimSpaces" .. give a try by setting it to true by default it is false. Not sure if this would solve your problem. It would trim the spaces betweeb scriptlets and JSTL tags that you have in your code , am not sure abt nulls worth giving a try.

-Murali*



ASKER CERTIFIED SOLUTION
Avatar of hrappapo
hrappapo

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