You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Richard McKinley <rr...@randtech.com> on 2000/05/18 18:48:10 UTC

getParameter not comparing to Strings

When I use request.getParameter() it returns a string that look exactly like
the string to which I want to compare it.  Alas, the result of the compare
is that they are not the same.  Attached is a small .jsp that shows the
problem.  What am I doing wrong?


***********start getparameter.jsp**************
<%
String myrequest = "fish";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<!-- The expression tags interpolate script variables into the HTML -->

<title>getrequest</title>
</head>

<body bgcolor=white>

<h1>
  The result of request.getParameter("someparameter") = 
  <%= request.getParameter("someparameter")%>
</h1>
<a href="getparameter.jsp?someparameter=<%= myrequest%>">test</a><br>
<%
if (request.getParameter("someparameter") == myrequest) {
  out.print(request.getParameter("someparameter") + "==" + myrequest +
"<br>");
} else {
  out.print(request.getParameter("someparameter") + "!=" + myrequest +
"<br>");
}
%>
</body>

Re: getParameter not comparing to Strings

Posted by "Edward W. Rouse" <er...@vei.net>.

Richard McKinley wrote:

> When I use request.getParameter() it returns a string that look exactly like
> the string to which I want to compare it.  Alas, the result of the compare
> is that they are not the same.  Attached is a small .jsp that shows the
> problem.  What am I doing wrong?
>
> ***********start getparameter.jsp**************
> <%
> String myrequest = "fish";
> %>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
> <head>
> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
>
> <!-- The expression tags interpolate script variables into the HTML -->
>
> <title>getrequest</title>
> </head>
>
> <body bgcolor=white>
>
> <h1>
>   The result of request.getParameter("someparameter") =
>   <%= request.getParameter("someparameter")%>
> </h1>
> <a href="getparameter.jsp?someparameter=<%= myrequest%>">test</a><br>
> <%
> if (request.getParameter("someparameter") == myrequest) {

try : if (myrequest.equals(request.getParameter("someparameter")) {
instead. you could also use
(request.getParameter("someparameter").equals(myrequest)) but I feel the first
way is easier to read.

>
>   out.print(request.getParameter("someparameter") + "==" + myrequest +
> "<br>");
> } else {
>   out.print(request.getParameter("someparameter") + "!=" + myrequest +
> "<br>");
> }
> %>
> </body>
>
> --------------------------------------------------------------------------
> To unsubscribe, email: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commmands, email: tomcat-user-help@jakarta.apache.org


Re: getParameter not comparing to Strings

Posted by Vincent Aumont <vi...@vslab.com>.
(string1 == string2)   compares the references, not the  string themselves.
You must use string1.equals(string2).

Richard McKinley wrote:

> When I use request.getParameter() it returns a string that look exactly like
> the string to which I want to compare it.  Alas, the result of the compare
> is that they are not the same.  Attached is a small .jsp that shows the
> problem.  What am I doing wrong?
>
> ***********start getparameter.jsp**************
> <%
> String myrequest = "fish";
> %>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
> <head>
> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
>
> <!-- The expression tags interpolate script variables into the HTML -->
>
> <title>getrequest</title>
> </head>
>
> <body bgcolor=white>
>
> <h1>
>   The result of request.getParameter("someparameter") =
>   <%= request.getParameter("someparameter")%>
> </h1>
> <a href="getparameter.jsp?someparameter=<%= myrequest%>">test</a><br>
> <%
> if (request.getParameter("someparameter") == myrequest) {
>   out.print(request.getParameter("someparameter") + "==" + myrequest +
> "<br>");
> } else {
>   out.print(request.getParameter("someparameter") + "!=" + myrequest +
> "<br>");
> }
> %>
> </body>
>
> --------------------------------------------------------------------------
> To unsubscribe, email: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commmands, email: tomcat-user-help@jakarta.apache.org


Re: getParameter not comparing to Strings

Posted by Ryan Rich <ry...@richservices.com>.
You cannot compare Strings using ==, you must use the method equals().

Instead of:
if (request.getParameter("someparameter") == myrequest) { 

Use:
if (request.getParameter("someparameter").equals(myrequest)) {


 
On Thu, 18 May 2000, Richard McKinley wrote:

> When I use request.getParameter() it returns a string that look exactly like
> the string to which I want to compare it.  Alas, the result of the compare
> is that they are not the same.  Attached is a small .jsp that shows the
> problem.  What am I doing wrong?
> 
> 
> ***********start getparameter.jsp**************
> <%
> String myrequest = "fish";
> %>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
> <head>
> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
> 
> <!-- The expression tags interpolate script variables into the HTML -->
> 
> <title>getrequest</title>
> </head>
> 
> <body bgcolor=white>
> 
> <h1>
>   The result of request.getParameter("someparameter") = 
>   <%= request.getParameter("someparameter")%>
> </h1>
> <a href="getparameter.jsp?someparameter=<%= myrequest%>">test</a><br>
> <%
> if (request.getParameter("someparameter") == myrequest) {
>   out.print(request.getParameter("someparameter") + "==" + myrequest +
> "<br>");
> } else {
>   out.print(request.getParameter("someparameter") + "!=" + myrequest +
> "<br>");
> }
> %>
> </body>
> 
> --------------------------------------------------------------------------
> To unsubscribe, email: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commmands, email: tomcat-user-help@jakarta.apache.org
> 
>