• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

comparing strings with equals method

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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());
}

}

}
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 227
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic