Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

comparing strings with equals method

 
john_smith
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to is the my first day that i am joining this forum. i hope i will get the good responce .
my email importdoc@hotmail.com
In the servlet below i am trying to compare tow string .
one i am retrieving from the database and othe i am decalaring in the instane variable in the doPost method
on the baese of these two string i wan to navigate between different pages.
i am using weblogic and sql server.

package projServlets;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.http.HttpServlet;
import java.util.*;
import java.io.*;
import java.sql.*;
public class LoginServlet extends HttpServlet{

public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
Connection con = null;
Statement statement = null;
ResultSet rs = null;
Object enu_obj = null;
String userid= null;
String password = null;
String category = null;
String category1 = "manager"; //the value of string i want to compare
res.setContentType("text/html");
PrintWriter out= res.getWriter();
String userid1 = req.getParameter("userid");
String password1 = req.getParameter("password");
try{
String strsql ="SELECT userid,password,category " +
"FROM customer WHERE userid = '"+userid1+"'";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con =DriverManager.getConnection("jdbc dbc roject");
statement = con.createStatement();
rs = statement.executeQuery(strsql);
Vector vec_get_user = new Vector();
while(rs.next()){
try{
vec_get_user.addElement(rs.getString("userid"));
vec_get_user.addElement(rs.getString("password"));
vec_get_user.addElement(rs.getString("category"));
}catch(Exception e){
e.printStackTrace();
}
}
Enumeration enum = vec_get_user.elements();
while(enum.hasMoreElements()){
enu_obj = enum.nextElement();
if (enu_obj!=null){
userid = (String)enu_obj;
}
enu_obj = enum.nextElement();
if (enu_obj!=null){
password = (String)enu_obj;
}
enu_obj = enum.nextElement();
if (enu_obj!=null){
category = (String)enu_obj;
}
}
if (userid==null || password==null){
res.sendRedirect("http://localhost:7001/projecthtml/logindenied.html");
}
//the values of two string to be compared
//category is coming from the database
//category1 is defined in the instance variable
if (category.equals(category1)) {
res.sendRedirect("http://localhost:7001/projecthtml/manager.html");
}
else
res.sendRedirect("http://localhost:7001/projecthtml/user.html");
}catch (SQLException sql){
System.err.println(sql.getMessage());
}catch (ClassNotFoundException cnf){
System.err.println(cnf.getMessage());
}catch (Exception e){
System.err.println(e.getMessage());
}

}

}
 
mustang india
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is just a suggestion, try printing the values u got from the servlet and the database.
Also use equalsIgnoreCase() to checkthe strings. This might help
 
Manohar Karamballi
Ranch Hand
Posts: 227
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai..
U did not clearly mentioned what ur problem is..
I assume ur facing problems in comparisons and it is evaluating false where it is supposed to be evaluated to true...
Follow following steps...
-- when u retrive string from DB check it for null and trim it..
-- As told in previous reply use equalsIgnoreCase while comparing..
Hope this helps..
Rgds
Manohar
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic