Wrox Programmer Forums
|
JSP Basics Beginning-level questions on JSP. More advanced coders should post to Pro JSP.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the JSP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 27th, 2003, 07:46 PM
tpb tpb is offline
Registered User
 
Join Date: Dec 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to tpb
Default Chapter 5

I can not get the quizhandler.jsp to work as described in the book.
It always gives the same result no matter what combination of choices I make when taking the quiz. I believe the problem is using !=(not equals) with a String. Since a string is really an abject it looks like it is trying to compare the object to the literal. I need to compare the contents of the String object to the literal. Please Help. I even tried to use the String.equal but still get crazy results.

Here is the code that does not work as the book describes.

<%@ page info="Quiz Handler JSP"%>

<html>
  <head>
    <title>Chapter 5 Examples</title>
  </head>

  <body>
    <b> Your Results : </b> <br /><br />
    <%
      String answer1 = request.getParameter("answer1");
      String answer2 = request.getParameter("answer2");
      String answer3 = request.getParameter("answer3");

      boolean bChecking = true;
      boolean bPassed = true;

      while (bChecking) {
        if (answer1 != "even") {
          bPassed = false;
    }

        if (answer2 != "odd") {
          bPassed = false;
    }

        if (answer3 != "1") {
          bPassed = false;
    }

    bChecking = false;
      }

     if (bPassed) {
       out.println("Congratulations, You Passed !!!");
     } else {
       out.println("Sorry !!! Better Luck Next Time!");
     }
   %>
  </body>
</html>
 
Old December 29th, 2003, 09:54 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The String object's .toString() method is automatically called when you do the != comparison check.

Did you copy the code from the download? If you typed it yourself then post the relavent code.

Cheers

Martyn
 
Old December 29th, 2003, 07:02 PM
tpb tpb is offline
Registered User
 
Join Date: Dec 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to tpb
Default

Thanks for the reply and your interest in helping me.

I posted the code for you to look at.
and included it here as well.

The if (answer1 != "even") fires if answer = "even" or "odd".
I displayed the value of answer* and whether it displayed even or odd the if statements fire causeing the bPassed variable to be false.
This really doesn't make sense to me.

Thanks
Tom

<%@ page info="Quiz Handler JSP"%>

<html>
  <head>
    <title>Chapter 5 Examples</title>
  </head>

  <body>
    <b> Your Results : </b> <br /><br />
    <%
      String answer1 = request.getParameter("answer1");
      String answer2 = request.getParameter("answer2");
      String answer3 = request.getParameter("answer3");

      boolean bChecking = true;
      boolean bPassed = true;

      while (bChecking) {
        if (answer1 != "even") {
          bPassed = false;
    }

        if (answer2 != "odd") {
          bPassed = false;
    }

        if (answer3 != "1") {
          bPassed = false;
    }

    bChecking = false;
      }

     if (bPassed) {
       out.println("Congratulations, You Passed !!!");
     } else {
       out.println("Sorry !!! Better Luck Next Time!");
     }
   %>
  </body>
</html>
 
Old December 30th, 2003, 05:56 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Tom,

Sorry, I didn't make my intentions clear enough. It was the code for Quiz.jsp that I wanted; as I need to see how you are setting the value for 'answer1'. Also, out of interest what version of Tomcat are you running?

Cheers

Martyn
 
Old December 30th, 2003, 06:45 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Tom,

I've just successfully ran the Quiz.jsp application using Tomcat 3.2.1. I used the source from the download, so if all else fails you could always try that.

Cheers

Martyn
 
Old December 30th, 2003, 12:08 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Tom,

Here's the URL for the source downloads for some of the old Wrox books: http://support.apress.com/

Cheers

Martyn





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 2 - End of chapter exercises whizzkid1892 BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 1 July 30th, 2008 12:02 PM
chapter 7 - chapter 11 pelopito BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 3 January 6th, 2008 11:40 AM
Generics chapter 12 difficult chapter i found ...? Larryz C# 2005 1 July 4th, 2007 09:40 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.