• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JSTL -- converting string to integer

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


how do you convert a string to an integer in JSTL?

here http://osdir.com/ml/jakarta.taglibs.user/2003-12/msg00173.html it says to do:

<c:set var="phNo" value="${1}"/>

to declare an integer, but what if instead of a literal integer there I need to put a param from request? like

<c:set var='phNo' value='${param.pn}' />

I assume ${param.pn} comes in as a string, yes? (just like it would with request.getParameter() ), so how do I convert this to an integer?

I searched long and hard for "JSTL convert string to integer" but did not find much..

essentially I need to do this in JSTL:


String phNo = request.getParameter("pn");
if (phNo == null) {
iPhNo = 1;
} else if (!phNo != null) {
iPhNo = Integer.parseInt(phNo);
}


the answer would be here, http://www.experts-exchange.com/Programming/Languages/Java/J2EE/JSP/Q_21612706.html
but unfortunately you have to pay to see answers here.. this experts-exchange comes up a lot when searching for stuff, it's a bit frustrating..

a lot of entries are different urls for this same thread:
http://mail-archives.apache.org/mod_mbox/jakarta-taglibs-user/200401.mbox/%3Cbt7ibi$7fm$1@localhost.localdomain%3E


thank you..

 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand the reason for the question. The expression language will automatically coerce a string to an integer if it's necessary to do so. You don't have to do anything to make that happen. Here's a reference I found with google keywords "jstl convert string to int".

Are you having a specific problem where this doesn't work, or have you not tried it yet because you thought you had to do something?
 
maya brown
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

oh my gosh, I was hoping my question wouldn't sound too stupid.. what it is is that I'm learning JSTL, and I'm trying to convert all scriptlets in one webapp to JSTL as a learning exercise.. I'm so used to doing back-end with scriptlets, it's a big change... so JSTL will "automatically coerce a string to an integer if it's necessary to do so"?? hmm.... if it's "necessary to do so"?? how does JSTL know if I need to convert a string to an integer?? :~) I saw the url you mention, I probably have seen all JSTL-related pages by now...;~)

here's a conditional I put together yesterday, but it's not working too well...

<!-- example url: ....jsp?pn=9 -- or it might not include the param at all -->

<c:set var='phNo' value='${param.pn}' /> <!-- comes in as string, I assume.. -->
<c:choose>
<c:when test="${phNo == null}">
<c:set var="iPhNo" value="${1}" /> <!-- correct constr for int? -->
</c:when>
<c:otherwise>
<c:set var="iPhNo" value='${param.pn}'/> <!-- so this autom. gets converted to an int?? hmmm..-->
</c:otherwise>
</c:choose>

<c:out value='${iPhNo}'/> <!-- this never prints -->


thank you...




 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please UseCodeTags when posting code or configuration. Unformatted code and configuration is very difficult to read. You can edit your post to include them by using the button.
 
Ranch Hand
Posts: 72
Scala Monad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In EL, It would convert a string into a number if it needs to perform any arithmetic operations on that variable. In your case it does not need to since you are just assigning a request parameter value to a variable. But your last c:out should print the value. Have you added the taglib directive for the "c"(core) tag?
 
reply
    Bookmark Topic Watch Topic
  • New Topic