Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!

Izanami Caster

Greenhorn
+ Follow
since Mar 05, 2020
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
2
Received in last 30 days
0
Total given
1
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Izanami Caster

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 this...my 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.




Thanks,
IC
3 months ago
JSP
Hi Ranch,

Scenario:

- 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.

Thanks,
IC
3 months ago
JSP
Thanks for the perspective, @Ron! The thread stands resolved!
3 months ago
JSP
Thanks Ron! The good news - I've got what I wanted; the not so good news - I'm not sure how.

The problem turned out more annoying...somehow, the sessionScope and param references weren't being read, despite me having added what I thought were the correct libraries.

I had previously accessed requestScope, so this was a bit bothersome. Just when I was about to give up, I  did a few things that ended up working for me.

Here's a summary  involving A) the deployment descriptor and B) , the POM. Could you help me disambiguate ?

A) I initially used a maven-archetype-webapp archetype to create this project. I added the following dependencies initially:



The archetype choice created a Deployment Descriptor file with the following content, as it would



….I changed this explicitly to the following, based on an internet source that I don't recall now...



B) I also added the jstl-api library along with the previously added dependencies, so that my POM dependencies looked as follows:



Now, I'm not sure if the last dependency did the trick, or if it was the DD change; it initially didn't work, but then somehow, after a quick, unrelated edit to the JSP (printing the requestScope and sessionScope side by side), it started working.

I'm able to access sessionScope and param variables now.

Here's an excerpt from the JSP, that falls in line with my original requirement.



Could you throw some perspective into this if possible ? Is JSP engine unpredictable in that way ? Did adding the additional library do the trick, or was it the DD ?

I apologize for the open ended question; I don't like the fact that I don't know what worked.

Thanks,
IC
3 months ago
JSP
hi Ron, Bear,

Clarification:

Using the following directly causes a NumberFormatException :



....prompting the question, "Shouldn't this be converted to an Integer/Number first ?"

Using an Integer.parseInt as follows is probably bad syntax, but the effect is the same..NumberFormatException:

${Integer.parseInt(param.numberOfQuestions)}

Perhaps there's a provision in JSTL that allows me to code this more elegantly. Googling isn't helping.

Thanks,
IC
3 months ago
Hi Bear,

I tried the following.




This is most certainly incorrect syntax, but I've hit a dead end here.

The JSP Error is the same...

Root Cause
java.lang.NumberFormatException: For input string: "${Integer.parseInt(param.numberOfQuestions)}"
java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)

Thanks,
IC
3 months ago
Hi Bear,

I did try that prior to this post (through https://www.journaldev.com/2064/jsp-expression-language-el-example-tutorial#jsp-el-implicit-objects).




The JSP Error:
java.lang.NumberFormatException: For input string: "${param.numberOfQuestions}"
3 months ago
I may be wrong but the answer to this is seemingly simple, but unknown to me.

My Problem: Let's say I'm trying to set myself up for a mock OCP test and I'd like to keep count.

My Approach :
Step 1: Create a form (form 1) that accepts the total number of questions (say 70) and the duration of the exam in minutes (150); pass these request parameters to another form (form 2)
Step 2: In form 2, set up a table with 70 rows (total number of questions), with column 1 numbered 1 through 70, and column 2 containing 1 text field per row, with my guess/answer (A/B/C/D etc.).

The issue:
I'm able to access Form1's request parameters in Form2 using a JSP expression; my need though for Form 2, is a table-like display with blank text fields per question, so a forEach is the key.
My attempts to access the request parameters within the forEach tag have failed so far.

Eg:

>

Any idea where I'm going wrong here ?

Thanks,
IC
3 months ago
Hi @Paul!

The following worked.



I had to add the following:

1) Add <%@ taglib prefix="c" uri = "http://java.sun.com/jsp/jstl/core"; %> to the JSP page. Doing this alone didn't help. So I also did 2.
2) Add the following maven dependency (POM) to support JSTL:



Thanks a lot for the help!

Thanks,
IC
3 months ago
JSP
Hi,

I'm using a backend service to

1) Fetch a list of users' email-addresses  and
2) Print them out in a table.

For now, I've used out.println to print out just the values without the table markup.

I don't want to use out.println to print table markup (feels a little dirty). Is there a better way to print table rows iteratively within a scriptlet ? Am I doing this the wrong way ? Please advise.



Thanks,
IC
hi @Claude,

THANK YOU! Changing the hibernate release version worked.


Summary:

1) I ran into an issue with a Hibernate-generated Oracle SQL query in which the table alias in the FROM clause was being prefixed, incorrectly, with AS.
2) This was reverified by running the SQL query on SQL+ both with (didn't work) and without (worked) the keyword AS.
3) The problem seems to be with version  6.0.0.Alpha4. Changing that dependency to <version>5.4.12.Final</version> worked.

Output:





Thanks again!
@Dave Tolls:

1) Here's my POM :



@Claude Moore

II)   I did run the Hibernate generated query without the "AS"; worked with a hardcoded ID just fine.
Hi Paul,

That shouldn't be the problem, Oracle 19c's dialect isn't different from that of Oracle 12c.

Having said that Hibernate doesn't offer a 19c. That still shouldn't be the problem though.

Thanks