String compare with Form field

Reply

Join Date: Mar 2009
Posts: 2
Reputation: ninadg is an unknown quantity at this point 
Solved Threads: 0
ninadg ninadg is offline Offline
Newbie Poster

String compare with Form field

 
0
  #1
Mar 11th, 2009
I am new to JSP and am having trouble comparing string with the value in a form field submitted.

The following is the code that I am executing through Tomcat and is not giving me the desired result:

  1. <%
  2. String myvar = request.getParameter("var1");
  3. boolean first = false;
  4. if (myvar == null) {
  5. first = true;
  6. } else {
  7. if (myvar == "compareme") out.println("string matches"); else out.println("string MISMATCH");
  8. }
  9. %>
  10. <html>
  11. <head><title>String compare with Form Input</title></head>
  12. <body>
  13. <h2>If you enter input as "compareme", I should say "MATCH",
  14. else "MISMATCH"</h2>
  15. <% if (! first) { %>
  16. Input was <%= myvar %> <br>
  17. <% } %>
  18. <hr>
  19. <form>Please enter a string <input name=var1>
  20. and <input type=submit></form>
  21. </body>
  22. </html>
You will notice that even though the correct string "compareme" is submitted in the form, the string comparison still fails. I have tried appending .toString() and .trim() to myvar but that has not helped either.

I am obviously missing something very basic in JSP. Please help.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,648
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 223
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is online now Online
Posting Virtuoso

Re: String compare with Form field

 
0
  #2
Mar 11th, 2009
You are missing something basic in Java which is why you should leave JSP and practice more:

  1. if (myvar.equals("compareme"))
  2. out.println("string matches");
  3. else
  4. out.println("string MISMATCH");
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC