Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help please.. Working With Null String Values 1

Status
Not open for further replies.
Jan 8, 2001
163
US
Hi there. I need help learning how to deal with string variables that may or may not be null in a JSP page.I have a program which (in simplified form) looks like this...


Main.jsp
(1) Goes to class where several users are selected. Each user's user info is put into tokenized format and added to a vector. Each vector element looks like this...
"username{firstname{lastname" IE/ USER_B{JACK{SMITH
(2) The username is NEVER null but the firstname and lastname can be and often are null.
(3) When the Vector is returned each row of user information is detokenized and put into a row in a table.
(4) The user can click on a row to go to EditUser.jsp and all of that user's data is passed as parameters to that page.

EditUser.jsp
(1) Each peice of the user's info is retrieved using request.getParameter
(2) The data is put into the form which can then be used to edit the user's information.
(3) On submit the form submits the form data to UpdateUser.jsp


My problem is figuring out how can I make my table on Main.jsp display correctly when firstname and/or lastname are null. I tried setting any null strings to &-nbsp on Main.jsp so it would look nice in the table. Unfortunately, when the form is submitted it then sends those blank spaces to EditUser.jsp. They need to be null.

Does anyone have any suggestions as to how I can approach this? Any suggestions would be appreciated.


Thanks,
CrystalVisualBOracle|-0
 
Two Comments:
1) You should be tokenizing and detokenizing. This is additional unnecessary work in an OO world. You should have a Data Object Class that contains the username, firstname, and lastname.
2) Show the code for the EditUser.jsp file. You should not be setting a value for these fields so that when the form is submitted you will be retrieving null from the request. It will be easier to help you if I saw code.

Please take number one under strong consideration. Trust me it makes for easier, more maintainable, and faster code.
Wushutwist
 
"You should be tokenizing and detokenizing. This is additional unnecessary work in an OO world. You should have a Data Object Class that contains the username, firstname, and lastname."

Thanks for your help! I'm pretty new to Java so I have yet to use Data Objects. I've seen them referred to in books and on this site. Unfortunately, I haven't found a good example or explanation so I haven't done it. Is there a help site or example that you know of by chance? I have quite a few good JSP & JAVA books but I can't find anything on Data Objects specifically. Maybe I'm looking it up wrong. I really just need something to get me started. Any help would be great. Thanks Again!

CrystalVisualBOracle :)

 
A Data Object is just a basic Java class that follows the JavaBean conventions (ie getter and setter method naming). For your example here is a simple DO:
Code:
public class WebUser {
  private username;
  private firstname;
  private lastname;

  public WebUser() {
     this(null,null,null);
  }   

  public WebUser(String username, String firstname, String lastname) {
     this.username = username;
     this.firstname = firstname;
     this.lastname = lastname;
  }

  public void setUsername(String username) {
     this.username = username;
  }

  public void setFirstname(String firstname) {
     this.firstname = firstname;
  }

  public void setLastname(String lastname) {
     this.lastname = lastname;
  }

  public String getUsername() {
     return username;
  }

  public String getFirstname() {
     return firstname;
  }

  public String getLastname() {
     return lastname;
  }

} // END OF CLASS

Again this is a very basic Data Object. You should at least be also overriding the toString(), equals(), and hashCode() methods that are inherited from Object. Wushutwist
 
I'm a tad confused. Right now, in Main.jsp, I call a method which selects out each user and their information like this...

SELECT USERNAME||'{'||FIRSTNAME||'{'||LASTNAME
FROM TABLE.USERS
WHERE UPPER(USERNAME) LIKE '%TEST%';


It then gets put into a vector in tokenized format like this...

For each resultset
Put into vector so Vectorname(1) might like this ...
'JCARSON{JOHN{CARSON'
and Vectorname(2) might look like this...
'DCLARK{DICK{CLARK'


When I'm done the vector has user info for several users. The vector is then passed back to the JSP page, each row is detokenized, and it's displayed in a table.

Are you saying that I can put all of this info into a data object rather than a vector? I guess I'm a little confused as to how the data object sets multiple rows of user data.

Thanks,
CrystalVisualBOracle
 
Here is the procedure:
Code:
Open Connection
  Get ResultSet
    Get Next Record
      Build WebUser Data Object
      Add WebUser Data Object to Vector
    Loop until no more records
  Close ResultSet
Close Connection

This will require a slight change to your SQL Statement because you want to retrieve the separate fields. It will now be:
Code:
SELECT USERNAME,FIRSTNAME,LASTNAME 
FROM TABLE.USERS 
WHERE UPPER(USERNAME) LIKE '%TEST%';

Wushutwist
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top