• 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 ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Use a form parameter from one JSP in the JSTL forEach tag of the same/a different JSP

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 67363
173
Mac Mac OS X IntelliJ IDE jQuery Java
 
Izanami Caster
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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}"
 
Bear Bibeault
Marshal
Posts: 67363
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Request parameters are strings. If you need to use one in the context of a number you'll need to convert it.
 
Izanami Caster
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bear Bibeault
Marshal
Posts: 67363
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps you could google "JSTL convert string to integer".
 
Saloon Keeper
Posts: 3026
407
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Izanami Caster wrote:


You should be able to use the parameter directly - no need to transform it.
 
Izanami Caster
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 25194
64
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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



The first link that Google gave me was JSTL - Core <fmt:parseNumber> Tag.

By the way you generally can't just put Java code into JSTL and hope it will work. JSTL is a language of its own, and it should have most features which you need to produce HTML. If not, then you can write your own tag libraries (in Java).
 
Ron McLeod
Saloon Keeper
Posts: 3026
407
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With this JSP code:

I see this result in the browser:

 
Ron McLeod
Saloon Keeper
Posts: 3026
407
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Izanami Caster wrote:Using the following directly causes a NumberFormatException :


Please show us the stack trace for the exception.
 
Ron McLeod
Saloon Keeper
Posts: 3026
407
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe I am seeing different behaviour than you because of the difference in JSTL libraries.

I tested using Tomcat 8.5.32.0, which it looks like is using this implementation:
 
Izanami Caster
Greenhorn
Posts: 17
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ron McLeod
Saloon Keeper
Posts: 3026
407
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the change that mattered was updating the Servlet version in the DD from 2.3 (almost 20 years old) to something more recent.

Also, you should be able to clean up your JSP code and get rid of the extra bits you added and just use:
    <c:forEach var="ad" begin="5" end="${param.numberOfQuestions}">.

 
Ron McLeod
Saloon Keeper
Posts: 3026
407
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I changed my test app to use on the Servlet API version 2.3, and I and saw the same failure that you were reporting:
 
Izanami Caster
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the perspective, @Ron! The thread stands resolved!
 
Gravity is a harsh mistress. But this tiny ad is pretty easy to deal with:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!