Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Total newbie dingbat question - getParameter

Status
Not open for further replies.

tigersbite

Programmer
Nov 12, 2000
2
0
0
US
Ok, this is really bugging me because it should work. My sample test.jsp is as follows:

<html>

<head>
<title>Test getParameter Page</title>
</head>

<body>
<% String myString = request.getParameter(&quot;fName&quot;); %>
<% if (myString == &quot;Sam&quot;) { %>
Hello There, <%= myString %>.
<% } else { %>
You're not Sam! You entered <%= myString%>.
<% } %>
</body>

</html>

I put the script in my server's root. The url I entered is:


Page processes fine, but my output is:

You're not Sam! You entered Sam.

Obviously the parameter is being passed to the script. Why isn't the string comparing correctly to the literal? Is there something I'm missing here? Thanks in advance.
 
Try the code below. I think the problem might be the placement of the <% %>. Its my understanding that blocks of code like and if else must be contained in one block. Im not sure if the sytax of the println is correct. There is a command which prints a line to the html document, you'll have to look it up. Hope this helps.

<%! String myString %>
<% myString= request.getParameter(&quot;fName&quot;); %>

<% if (myString == &quot;Sam&quot;)
{
println(&quot;Hello There&quot; + myString);
}
else
{
println(&quot;You're not Sam! You entered&quot; + myString);
}
%>
 
Unfortunately, that's not the problem. The problem occurs when I compare the value of the request.getParameter to either a string variable or literal. If I output the value of myString to my web page, it looks as it should. Is it possible the server is adding some binary information to the parameter before it is prosessed for output to html?
 
Dear tigersbite,

String str = new String(&quot;Sam&quot;);
System.out.println( str == &quot;Sam&quot;);

will output 'false'

Just part of Java. You need to learn how to work with the java.lang.String class. I would suggest learning in a Java appliction environment rather than a JSP environment to simplify things.

Hope this helps
-pete
 
Use the equals method instead of == , equals method does a comparision of the value of the string, when you do == you are actually trying to see if 'myString' object and &quot;Sam&quot; object are same or not, which will be same only if they point to the same object.
So doing a comparision always use equals method which does the comparision of value of the string.

Use the following:-
(myString.equals(&quot;Sam&quot;))

<html>

<head>
<title>Test getParameter Page</title>
</head>

<body>
<% String myString = request.getParameter(&quot;fName&quot;); %>
<% if (myString.equals(&quot;Sam&quot;)) { %>
Hello There, <%= myString %>.
<% } else { %>
You're not Sam! You entered <%= myString%>.
<% } %>
</body>

</html>

This did work..
I hope this helps.
Rumpa
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top