• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 790
  • Last Modified:

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.
0
hrappapo
Asked:
hrappapo
  • 5
  • 4
  • 4
  • +1
1 Solution
 
objectsCommented:
how are you displaying it?

0
 
hrappapoAuthor Commented:
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>
0
 
rrzCommented:
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

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
hrappapoAuthor Commented:
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
0
 
rrzCommented:
Maybe objects has an idea. He will probably be around soon.
0
 
rrzCommented:
I finally thought of something. I guess you create a Filter and a HttpServletResponseWrapper to replace the substring
value="null"
with
value=""
0
 
rrzCommented:
You could do something like is done at
http://balusc.blogspot.com/2007/12/whitespacefilter.html 
We could help you.  
0
 
objectsCommented:
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.

0
 
hrappapoAuthor Commented:
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?
0
 
objectsCommented:
not off the top of my head, would have to go digging thru the source for that

0
 
hrappapoAuthor Commented:
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.
0
 
objectsCommented:
Sorry, I'm flat out with client and student work at the moment.

0
 
Murali MurugesanFull stack Java developerCommented:

@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*



0
 
hrappapoAuthor Commented:
I haven't found any global solution, so I have gone through all my code and replaced null with blanks in any field I think could possibly be null.  Not fun.  I guess using that feature in weblogic was a luxury that I'll have to learn to live without, and develop some new habits.  Thanks everyone for the suggestions.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now