GeeCON Prague 2014*
The moose likes JSP and the fly likes JSTL -- converting string to integer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSP
Bookmark "JSTL -- converting string to integer" Watch "JSTL -- converting string to integer" New topic
Author

JSTL -- converting string to integer

maya brown
Ranch Hand

Joined: Feb 18, 2009
Posts: 42


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

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Feb 18, 2009
Posts: 42

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>
<ctherwise>
<c:set var="iPhNo" value='${param.pn}'/> <!-- so this autom. gets converted to an int?? hmmm..-->
</ctherwise>
</c:choose>

<cut value='${iPhNo}'/> <!-- this never prints -->


thank you...




David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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.
Marimuthu Madasamy
Ranch Hand

Joined: Jun 07, 2007
Posts: 72

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?


- Marimuthu Madasamy
 
GeeCON Prague 2014
 
subject: JSTL -- converting string to integer