wood burning stoves 2.0*
The moose likes Servlets and the fly likes comparing strings with equals method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "comparing strings with equals method" Watch "comparing strings with equals method" New topic
Author

comparing strings with equals method

john_smith
Greenhorn

Joined: May 10, 2002
Posts: 1
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

Joined: Feb 05, 2002
Posts: 60
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

Joined: Jul 17, 2001
Posts: 227
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
 
jQuery in Action, 2nd edition
 
subject: comparing strings with equals method
 
Similar Threads
Vector from bean to servlet?
kindly help me in redirecting....... as soon as possible
Pass session from JSP to servlet?
Servlet connection with Jdbc
Hi,I have a problem in database connection