简体   繁体   English

从servlet获得响应

[英]Getting response from servlet

I'm using the following code to get the response from a servlet. 我正在使用以下代码从servlet获取响应。 It will check whether the given name in the variable "get" is in a particular table, and print 1 if it exists. 它将检查变量“get”中的给定名称是否在特定表中,如果存在则打印1

Portion of servlet code: servlet代码的一部分:

get = request.getParameter("nam");// such as get="kannan"
try {
    // Connection code

    ResultSet rs = stmt
            .executeQuery("select * from newfarmer where rname='" + get
                    + "'");
    while (rs.next()) {
        username = rs.getString("rname");

        if (get.equals(username)) {
            out.println(1);
        }

    }
} catch (Exception e) {
    out.println(e.toString());
}

In my android application, I check this response as follows: 在我的Android应用程序中,我检查此响应如下:

response = CustomHttpClient.executeHttpPost(
        "http://moberp.svsugar.com:8080/androidservlt/Modify",
        postParameters);
String res = response.toString();
res = res.trim();

if (res.equals("1")) {
    flag = 1;
    Toast.makeText(getBaseContext(), "Correct", Toast.LENGTH_LONG)
            .show();
} else {
    flag = 2;
    Toast.makeText(getBaseContext(), "please enter correct Ryot name",
            Toast.LENGTH_LONG).show();
}

It works very well for single record. 它对于单一记录非常有效。 I mean, in the table "newfarmer", If "rname" consists of more than one same name only the else part is executed. 我的意思是,在“newfarmer”表中,如果“rname”由多个相同的名称组成,则只执行else部分。

Example: If "kannan" is presented 2 times in the table Servlet output is as 示例:如果表中出现“kannan”2次,则Servlet输出为

1 1

Now in android application, clearly the else part is executed because response is not 1. This is only case of two same names. 现在在android应用程序中,显然else部分被执行,因为响应不是1.这只是两个相同名称的情况。 The table may contains more than 10 same names. 该表可能包含10个以上的相同名称。 If 10 same names, then servlet output is as 如果有10个相同的名称,则servlet输出为

1 1 1 1 1 1 1 1 1 1

So I need to check all. 所以我需要检查所有。 So I need to make changes in my if condition, but I don't know what to do. 所以我需要在if条件下进行更改,但我不知道该怎么做。 Someone please give answer. 有人请回答。

Thanks in advance 提前致谢

instead of while loop Use 而不是while循环使用

if(rs.next())

Now it will print only one time. 现在只打印一次。

No change in android android没有变化

in servlet do this 在servlet中执行此操作

if(rs.next()) 如果(rs.next())

           {
               username=rs.getString("rname");

             if(get.equals(username))
             {
                   out.println(1);
             }

           }

} }

this loop will run only 1 time now if the record is present. 如果记录存在,此循环现在只运行一次。

I don't understand why would get.equals(username) will evaluate to false when you are having a where clause in your SQL query? 我不明白为什么当您在SQL查询中使用where子句时, get.equals(username)将评估为false?

So just try this. 所以试试吧。

if(rs.next()) 
{    
              // The above condition will make the code inside if executed 
              // only if any matching record is found and 
              //hence it will print `1` only once 
              //if any matching record is found. 
   username=rs.getString("rname");

   if(get.equals(username))
   {
       out.println(1);
   }

}

Also you are using stmt.executeQuery("select * from newfarmer where rname='"+get+"'"); 你也在使用stmt.executeQuery("select * from newfarmer where rname='"+get+"'"); which is susceptible to SQL injection. 这容易受到SQL注入的影响。

So better use prepared statement instead. 因此,最好使用预备语句。

尝试if(rs.next()) ,这肯定会帮助你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM