File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes problem in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "problem in servlet" Watch "problem in servlet" New topic
Author

problem in servlet

Jonathon Stride
Ranch Hand

Joined: Aug 06, 2009
Posts: 34
so i was writing a servlet that has to do some string comparision (related to DB2 database query)

in the the dopost() method the following code is there :

String a="A";
out.println(rs.getString(3));

if(rs.getString("type1").equals(a)){

RequestDispatcher view=request.getRequestDispatcher("adminlogin.jsp");
view.forward(request, response);
}
else if("B".equals((String)rs.getString(3))){
RequestDispatcher view=request.getRequestDispatcher("customerlogin.jsp");
view.forward(request, response);
}
else if("C".equals((String)rs.getString(3))){
RequestDispatcher view=request.getRequestDispatcher("workerlogin.jsp");
view.forward(request, response);
}
else if("D".equals((String)rs.getString(3))){
RequestDispatcher view=request.getRequestDispatcher("publiclogin.jsp");
view.forward(request, response);
}
out.println(rs.getString("type1"));
out.println(a);
out.println(rs.getString("type1")==a);

the output , as seen in the browser is :

Processing.....!
A
A
A
false


somehow the "A" from the result set doesnt compare with "A" in the servlet

what is wrong here ?


2 interviews failed cause of not having SCJP ( and counting...)
not anymore !

SCJP 6 (70%)

now ready to count other reasons :P...
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183


is supposed to be out.println(rs.getString("type1").equals(a));

Using == compares the objects references, not the logical equality of the objects.


JDBCSupport - An easy to use, light-weight JDBC framework -
Jonathon Stride
Ranch Hand

Joined: Aug 06, 2009
Posts: 34
out.println(rs.getString("type1").equals(a));

still gives me output as false

when i print rs.getString("type1")
and a
separately , i get A as output
but when i compare them they are different ?

rs is a result set from a sql query on a db2 database ,type1 is a field of type VARCHAR

what exactly is the problem and is it related to the db or something
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18649
    
    8

Jonathon Stride wrote:out.println(rs.getString("type1").equals(a));

still gives me output as false

when i print rs.getString("type1")
and a
separately , i get A as output
but when i compare them they are different ?

rs is a result set from a sql query on a db2 database ,type1 is a field of type VARCHAR

what exactly is the problem and is it related to the db or something


You didn't say how you were comparing them. Or did you mean that when your code compared them, they were different? If that's what you meant, then they were indeed different and you couldn't tell that by looking at... whatever it was you looked at. Perhaps there are leading or trailing whitespace that you didn't notice when you looked?
Jonathon Stride
Ranch Hand

Joined: Aug 06, 2009
Posts: 34
i printed both sides separately in firefox and they look the same

A and A
but the comaprision is giving false

as you say, if there are any trailing spaces , is there a way to remove them from the string before doing the comparision

Jonathon Stride
Ranch Hand

Joined: Aug 06, 2009
Posts: 34
LOL i made the table field length ( in db2 ) == 1 and the comparision is successful

you are right there were 9 whitespaces (earlier length was 10) in the result set column 3
and it wasnt comparing it successfully with the single letter "A"

thanks a lot for pointing that out


that said , is there a way i can remove the trailing spaces before comparing ?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18649
    
    8

Jonathon Stride wrote:that said , is there a way i can remove the trailing spaces before comparing ?


String has a trim() method.
krishna kanthgaru
Greenhorn

Joined: Feb 12, 2007
Posts: 17
or store in DB , the trimmed version.

TRIM(myString)

i believe this would be better if you trailing spaces dont make sense in your application


no signature ... i am not a celebrity yet
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem in servlet