Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • salvin francis
  • fred rosenberger
  • Frits Walraven

JSTL | For TreeMaps T1 and T2, display T2's value for a key common to T1 and T2

Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranch,


- Quiz taker keys in answers for a user defined number of questions, which are saved off in a TreeMap, T1, ordered by question number. T1 has sessionScope.
- TreeMap T2 has the correct answers ordered by question number. T2 has sessionScope too.

Prerequisite: Use JSTL.

My Approach:

Role 1:
Quiz Taker:
1) Take the quiz by answering questions one by one, hitting a submit button after each question. The answers are saved off in T1 by a servlet, S1. This servlet then dispatches the same JSP J1.   Accomplished!
2) After each submit, display the question number along with the user's answer in a growing table in J1. This table also has a third column C3 with an input field (explained in the next point).   Accomplished!
3) When the allotted time has passed, or when the quiz-taker  decides, 'Finalize' the answers by hitting a Finalize button. This seals the user's answers. Accomplished!

Role 2:
   Score Calculator:
4) The user looks up a source (say OCPs' answers to review questions) and keys in the correct answers manually into C3. Once all the answers have been keyed in to the input text field Tx1, the user hits the SCORE button. Accomplished!
    a) This action is forwarded to the servlet S1, which saves the answers off to TreeMap T2. Accomplished!
    b) S1 then calculates the score, and passes and then dispatches J1. Accomplished!
    c) About text Field Tx1: This input field has a default value intended to be set to T2's values for the question key. NOT ACCOMPLISHED!!

Now, I know the following doesn't, and shouldn't work, but I was hoping someone could understand my problem based on the above description and fill in the gaps.

Please refer to my attachment to achieve more clarity as to this problem.

Please advise.

Posts: 25468
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Izanami Caster wrote:Prerequisite: Use JSTL.

Why is that a prerequisite? Normally you would do that sort of logic in the servlet which forwards to the JSP, and leave the JSP to just do ordinary HTML output.
Saloon Keeper
Posts: 21993
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSTL is only part of the problem. The "c:set" and "c:forEach" tags are JSTL. But the table value expressions are actually EL (Expression Language). EL - in its current form as Unified Expression Language -  is a standard interpreted expression language used by JSPs. JavaServerFaces View Templates, dependency injection annotations and anywhere else that it's convenient to be able to reference the contents of a JavaBean.

EL allows for referencing simple properties ("myBean.username"), array elements ("myBean.groceryList[5].itemName"), Map lookups ("myBean.groceries{id}" - keyer "key" is a variable) and all the permutations you can spin off of such constructs. It's well worth developing a solid familiarity with.

In the example given, I'd read the data structure as a Map containing keys associated with object of some unnamed class. Let's call it "Answer". Answer class thus would have a property named "answer". Since the answerMap object has two properties: "key" and "value" and we want the "answer" property of the "value" object.

Buf answerMap is only comprised of those two properties: key and value. So the offending statement is looking for a property named "correctAnswerMap" and that doesn't exist.

A more likely setup would have the Answer class having two properties: "answer" and "correctAnswer", so the EL expression would be "value="${answerMap.correctAnswer}"".
Izanami Caster
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi @Paul,

The JSTL pre-requisite was my preference. I just wanted to test its maneuverability and my comfort with it.

hi @Tim,

Thank you for the clarity regarding JSTL, EL, and the uses of the latter! The perspective was much needed.

As for the example, the following Java-code would explain it better (assuming TreeMap T1 contains the users answers and TreeMap T2 contains the correct answers)….I'm writing untested code here just for analogy...hope syntax errors are excused:

- correctAnswerMap was meant to be a reference to t2.

I had wanted to write code that

1) Places T1's and T2's values for the same key, adjacent to each other.
2) Avoids iterating through both T1 and T2 to arrive at common keys.

- The following is a somewhat costly approach to last resort. It works okay, but I wonder whether there's a better way to do this. I've renamed correctAnswerMap to cMap for brevity.

And tomorrow is the circus! We can go to the circus! I love the circus! We can take this tiny ad:
Two software engineers solve most of the world's problems in one K&R sized book
    Bookmark Topic Watch Topic
  • New Topic