Hi,

I need your help... I need to get the value selected by a user from the HTML page and pass this value into my servlet program for processing. So I did something like this:

Part of my jsp code:

<select name="tool" id="tool">
    <option value=kim>KIM</option>
    <option value=ontomat>Ontomat Annotizer</option>
    <option value=gate>GATE</option>
</select>

And:

Part of my java class protected void doGet(...) metod:

String s=request.getParameter("tool");
	out.println(s);
	if (s.toLowerCase()=="kim") 
	out.println(s);
        else 	out.println("error");

When I select "kim", I want it to print
kim
kim

My problem is that what ever I select it prints out
[what ever I selected]
error

The line

out.println(s);

prints what I selected but "if" part does not work the way I would like it to...

I am new at all this and could really use some help...

Thanks,
Jelena

Recommended Answers

All 3 Replies

Member Avatar for rakhi4110

Hi varia,
Well the problem is not in the SELECT tag.
Try using this code in your JSP file

String check="kim";
        String s=request.getParameter("tool");
	out.println(s);
	if (s.toLowerCase().equals(check)) 
	out.println(s);
        else 	
	out.println("error");

This is because there is difference between == operator and the equals() method.
== operator is used to compare the references of the objects.

public boolean equals(Object o) is the method provided by the Object class. The default implementation uses ==
operator to compare two objects.

But since the method can be overriden like for String class. equals() method can be used to compare the values of two
objects.

String str1 = new String("MyName");
String str2 = new String("MyName");

if(str1 == str2){
out.println("Objects are equal")
}else{
out.println("Objects are not equal")
}

if(str1.equals(str2)){
out.println("Objects are equal")
}else{
out.println("Objects are not equal")
}

Output:
Objects are not equal
Objects are equal

Another snippet:

String str2 = "MyName";
String str3 = str2;

if(str2 == str3){
out.println("Objects are equal")
}else{
out.println("Objects are not equal")
}

if(str3.equals(str2)){
out.println("Objects are equal")
}else{
out.println("Objects are not equal")
}

Output:
Objects are equal
Objects are equal.

Check out this and reply whether your prob is solved or is there something else which i can do for you.
Thanks and Regards,
RAKHI

rakhi,

Thanks a million... The problem is solved... It works great now...

Thank you,
Jelena

Member Avatar for rakhi4110

Jelena,
You are welcome.. anytime to help you..
Since your problem is solved you can mark the status of this post as solved.

Thanks and regards,
RAKHI

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.