• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with default '' , 0 and 0.0 being inserted into database while using JSTL.

 
Ranch Hand
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all ,
I'm facing a problem while trying to implement my screen using JSTL. In order to be able to dynamically control the screen, I tie the value attribute of an html tag to the bean value. For instance, somewhat like this:

where strmeterSerial is an attribute of the bean,Consumer. That is accessed on the particular screen by placing the following include at the top of the screen

Now , the thing is that fields that have been entered as empty(as in where the user chooses not to enter any data), are replaced with '' and are inserted as '' into the database instead of a NULL.
Even worse , if I used an attribute of type int or float and tied it to a field in the jsp , when left empty it defaults to 0 and 0.0 respectively .These are then further inserted into the database , causing an inexplicably large number of zero values . Also , it becomes impossible to distinguish which of the values in the database are a result of a user manually entering zero or is it because of a default value .

It would be great if anyone could suggest a workaround for this.

I have tried setting the bean as null through the loading class , used the JSTL ternary operator but havent been able to solve my problem yet
 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some of my approaches to try tackling the problem , that have failed are-


for the following jsp snippet.


This approach throws a NPE.
Tried using a flag to hint that its in edit mode(when the user can view the values that he inserted ) and selectively display the value attribute . and not process the value tag during insert mode .
However this code very messy.
Im hoping for a straight forward solution that can help me instantiate the bean to null (when the page initially loads). So that all the corresponding attributes of it are (I hope) null and therefore blank.
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I think you are approximately using right method for using ternary operator in getter method.
But be sure that you are using Float not float I mean for the data type.
And for string check the length if it is zero length make it null.
 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didnt quite get you.Could you please explain with a code sample .I have never used Float class . Also how would this help with '' being inserted in the case of string?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You seem to be thinking that anything submitted from the page needs to be set in tot he DB, as is. You can just perform some data scrubbing in your DB prep code. Conditional statements are good for that sort of thing.

What you do with the JSTL is completely moot. That's just for display.

And I still don't understand this distinction you are trying to make between "default" and other data. I've never heard of this type of distinction. I've never used this type of distinction. And I suspect you're just overcomplicating what should be a straightforward operation.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand why you're writing JSTL code which updates a database. That's a completely wrong approach.

Or perhaps you aren't. But you just complained about database updating and didn't provide any explanation of how it was taking place. It would be better if you explained your actual problem instead of asking about your proposed solution, which -- as you can see -- is not working anyway.
 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vic Hood wrote:Hi all ,
I'm facing a problem while trying to implement my screen using JSTL. In order to be able to dynamically control the screen, I tie the value attribute of an html tag to the bean value. For instance, somewhat like this:

where strmeterSerial is an attribute of the bean,Consumer. That is accessed on the particular screen by placing the following include at the top of the screen

Now , the thing is that fields that have been entered as empty(as in where the user chooses not to enter any data), are replaced with '' and are inserted as '' into the database instead of a NULL.
Even worse , if I used an attribute of type int or float and tied it to a field in the jsp , when left empty it defaults to 0 and 0.0 respectively .These are then further inserted into the database , causing an inexplicably large number of zero values . Also , it becomes impossible to distinguish which of the values in the database are a result of a user manually entering zero or is it because of a default value .

It would be great if anyone could suggest a workaround for this.


That Sir , is the problem.
This is the flow of code.
From a html code

In the servlet


In the DAO class.


I have tried to show how Im trying to process the code. It would be great if the experienced folks here could point out what Im doing wrong here.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree, it's useless to show us code until you try to explain what you are trying to do.
 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What Im trying to do:Get rid of '' , 0.0,0 for strings ,floats and integers that are inserted when the fields that they are tied to it are left untouched .(as In not manually entered )
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's wrong with an if statement?
 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Im sorry ,
But I dont get quite how to implement it ..
Could you please explain with an example?
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, first of all you're making a common beginner mistake: Never use == and != to compare the contents of two strings. So instead of

you should have

However your DAO is inserting values into the database from a variable whose type is "float". And you want that to be null if the user leaves the HTML field empty? Then "float" wasn't really the best choice, because it can't be null. Perhaps you could do something like using the Float.NaN value to represent an unknown value, and then write null to the database if you see that value.
 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Well, first of all you're making a common beginner mistake: Never use == and != to compare the contents of two strings. So instead of

you should have


Thank you Ill make that correction right away.

Paul Clapham wrote:
However your DAO is inserting values into the database from a variable whose type is "float". And you want that to be null if the user leaves the HTML field empty? Then "float" wasn't really the best choice, because it can't be null. Perhaps you could do something like using the Float.NaN value to represent an unknown value, and then write null to the database if you see that value.


Precisely,
two questions
1. What other value can I use to represent , if not float ?So that I can avoid the mistake in future
2.I have never used Float.NaN . How do I implement with respect to my problem?

Thank you for replying.
And also the pressing issue of displaying the values that are tied to the HTML fields as '' upon loading the page . How do I tackle that?
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's hard for me to say what you should do. You showed us something which is inserting data into a database, and you said that the data inserted should be null if the user didn't input anything, otherwise it should be whatever number they input. (Or something like that...)

But apparently this data isn't being input from scratch by the user, it's being input based on the contents of some other data store. We don't know anything about that data store. Can it contain nulls? Or do zeroes have a special value? Or what?

And you're focused on zeroes and blanks and nulls here; a proper design would explain what should happen with normal numeric values as well.
 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The values entering the fields are through a bean class. The attributes of this bean are then tied to each of the value tags of the html elements.
For instance ,
If I were to consider the sample element I placed,

My jsp then references the consumer bean using usebean

where the consumer class is defined as follows(Im asuming you are referring to this as the datastore)
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, so those float fields you're having trouble with can't contain nulls. They can certainly contain zero, but presumably that's a valid value and not special in any way. (That's why I asked that question.) So I don't see why you should be inserting nulls into that other table just because the original value was zero.
 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay what values do I insert if not nulls?
I certainly cant insert zeros if the user does not enter any values as it would be impossible to distinguish between those entered by user and those entered because they were just left empty
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know. Why are you showing the value from that bean in the first place? And if you show the user "37.42" and the user blanks it out, what should happen? What should happen if you show the user "37.42" and the user changes it to "16"?

I don't think I'm quite getting through to you. You're still asking about programming. I'm asking about the design of your system and your data model. You haven't yet explained how the system is supposed to work, and yet you're asking how to write code to make it work.
 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay , sorry for being non descriptive earlier.
This is the design of my code .
1.Every page has three layers strictly following mvc.
the jsp itself has only html elements that have values bound to bean attributes or lists that are populated to display the necessary data.
2. the servlet that accepts the data from the jspthrough the request and then values obtained manually are set to bean properties manually after being checked if necessary ,and then the entre bean is passed to the DAO for operations

As far as the user is concerned when the page loads, he is only supposed to see blank fields when he loads a page irrespective of whether the html element is bound to a float , integer or string element.
When the user performs a save I expect to enter values into the database for whatever fields have been manually entered by the user , null for the rest (not default '',0 or 0.0)
Upon fetching a particular record for editing I use the binding to display all the required fields in the screen based on data in the database and perform an update when the person saves with the edited data in the screen.

I hope I was clear in explaining the design , I would be more than happy to explain any other points
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Next question: Are the values in this Consumer class being populated by you, or populated automagically from the request parameters?
ie using a <jsp:setProperty> or a framework like stripes/struts/JSF which does the translation to the bean layer for you?

If the values in the Consumer Bean can be null, perhaps you could use a Float instead of a float primitive to store the value.
Or perhaps a Double? I always prefer doubles to floats as they have more precision.
By using the Object wrapper instead of the primitive, you get the ability to represent null in your bean.

The DAO layer can then leverage that:
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vic Hood wrote:As far as the user is concerned when the page loads, he is only supposed to see blank fields when he loads a page irrespective of whether the html element is bound to a float , integer or string element.



So then I don't understand why you are populating the fields with data from a bean, if they are supposed to be blank initially.
 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stefan Evans wrote:Next question: Are the values in this Consumer class being populated by you, or populated automagically from the request parameters?
ie using a <jsp:setProperty> or a framework like stripes/struts/JSF which does the translation to the bean layer for you?

If the values in the Consumer Bean can be null, perhaps you could use a Float instead of a float primitive to store the value.
Or perhaps a Double? I always prefer doubles to floats as they have more precision.
By using the Object wrapper instead of the primitive, you get the ability to represent null in your bean.

The DAO layer can then leverage that:



I only use the useBean attribute and populate parameters through the request .No frameworks whatsoever.
Two Questions
1. How do I make the consumer bean null?
2.So a conversion of he required attributes in the bean class of the problematic fields to type Float and Integer resp. and corr changes in DAO should solve the problem?


So then I don't understand why you are populating the fields with data from a bean, if they are supposed to be blank initially.


Rightly asked Paul.In fact,this made me think , the only time I use the bindings is to populate data when I fetch data to display in the fields(that the user has previously entered in to fields).
So instead of


If I did


And in the servlet code where I prepare data to be put into a bean (all attributes of that bean were being automatically set) and then instead of doing

If i did.


Would this solve all my problems ? On initial page load i assume motorating wouldn't be initialized and there fore the fields would appear blank, when the user inserts data, its fetched from fields using request.getParameter in the servlet and forwarded to DAO operations . And whenever A record is fetched for display the value is displayed through what has been set in the request as explained above.

Would this approach solve all the problems or am I missing some aspect, that could haunt me later?
Thank you for your inputs.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So you have this:

Now if "consumer" isn't found in any of the EL contexts (page, request, session) then that <input> element won't have a value attribute. So don't create that "consumer" attribute unless you have to. Asking how to make it null is the wrong question; just don't make it at all.

To put it another way, you have this somewhere in your code:

Just don't do that if it's the initial page load. Like Bear Bibeault said quite some time ago:

Bear Bibeault wrote:What's wrong with an if statement?

 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay Im aware of that .But if I did that , how would I be able to dynamically set values to it(field) ,while fetching data (that is to be displayed), which the user has previously entered.? Im taking about dynamically populating the text fields here
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry, I wrote one thing and then I realized I hadn't read your last post properly. So I went back and wrote something completely different. It looks like you responded to my original rubbish post. Could you read the edited version? Because what you wrote now doesn't make any sense. Sorry about that.
 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
EDIT: I guess you edited your response. With respect to your new response.
This is what I do in the Get method that loads the page.

In fact I had mentioned it in the other thread too , that I explicitly remove "consumer" from the request, I simply dont seem to understand how its created in the first place when it loads through the above method.
Could you kindly point where Im going wrong above?

Also , how do I incorporate an if statement here to do the checking?
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's sort of helpful and sort of unhelpful. I see some code which basically says "If strc_id is a string which isn't empty then do some stuff and forward to another URL". But if strc_id is null or an empty string then it does nothing at all, which doesn't seem right. The user is going to just get a blank screen.

(And I still see != being used to compare strings there.)

So what I can see is various fragments of code which might or might not be related. If that code does happen to forward to pumppopup.jsp, then it shouldn't have a request attribute named "consumer". However that doesn't mean that ${consumer} won't find an attribute in that JSP; it might find a session attribute with that name, for example. You can debug that easily by just outputting ${consumer}, which will display nothing if there's no such attribute available and the toString() of the attribute if there is one.
 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:That's sort of helpful and sort of unhelpful. I see some code which basically says "If strc_id is a string which isn't empty then do some stuff and forward to another URL". But if strc_id is null or an empty string then it does nothing at all, which doesn't seem right. The user is going to just get a blank screen.
.


The user will only enter the screen is with a value for strc_id, because the page in question loads through a doGet method(after clicking a link that passes the value of consumer id as parameter,which is taken as strc_id),so the chances of strc_id being null are not likely unless during testing (or I hope so)

Paul Clapham wrote:
(And I still see != being used to compare strings there.)
.


Sorry ! All these lines were written before I got to know the fallacies of using != yesterday , will make corrections asap

Paul Clapham wrote:
So what I can see is various fragments of code which might or might not be related. If that code does happen to forward to pumppopup.jsp, then it shouldn't have a request attribute named "consumer". However that doesn't mean that ${consumer} won't find an attribute in that JSP; it might find a session attribute with that name, for example. You can debug that easily by just outputting ${consumer}, which will display nothing if there's no such attribute available and the toString() of the attribute if there is one.


Hmm I have tried using ${consumer} and it does throw a value . Your mention of session is something i havent looked at yet though, how do I check and remove it then?
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vic Hood wrote:Hmm I have tried using ${consumer} and it does throw a value . Your mention of session is something i havent looked at yet though, how do I check and remove it then?



No, that isn't what you should do. You need to understand why things were written the way they were, and then fix them appropriately. Otherwise
your system is going to end up as a big pile of quick-and-dirty hacks.
 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:
No, that isn't what you should do. You need to understand why things were written the way they were, and then fix them appropriately. Otherwise
your system is going to end up as a big pile of quick-and-dirty hacks.


Could you kindly elaborate?
Im a newbie and this is my first project using JSP , Java
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vic Hood wrote:Could you kindly elaborate?



No, I really couldn't. So far we've seen fragments of code which may or may not have been the right thing to do. And we've seen comments which
suggest that you don't understand the code which you're working with. Of course since you're a beginner it isn't at all surprising that you don't
understand it, that's quite normal. But your approach of changing random bits of code until you get the result you want isn't the right way.

I can't explain to you how to understand a system, especially a Java EE system which has bits and pieces all over the place whose relationships
aren't obvious. So my suggestion at this point would be to get somebody else who has more experience to come in and help you through that
process. It really needs to be somebody with access to the whole code base and design documents. I really don't think the forum approach will
work in this situation.
 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vic Hood wrote:


So then I don't understand why you are populating the fields with data from a bean, if they are supposed to be blank initially.


Rightly asked Paul.In fact,this made me think , the only time I use the bindings is to populate data when I fetch data to display in the fields(that the user has previously entered in to fields).
So instead of


If I did


And in the servlet code where I prepare data to be put into a bean (all attributes of that bean were being automatically set) and then instead of doing

If i did.


Would this solve all my problems ? On initial page load i assume motorating wouldn't be initialized and there fore the fields would appear blank, when the user inserts data, its fetched from fields using request.getParameter in the servlet and forwarded to DAO operations . And whenever A record is fetched for display the value is displayed through what has been set in the request as explained above.


Thank you for replying Paul , I understand that this requires peer review by some one of experience . However , I dont have access to anyone as such at the moment . So my option would be to remove the bindings as proposed above. Do you foresee any problems with this approach given my predicament?
reply
    Bookmark Topic Watch Topic
  • New Topic