aspose file tools*
The moose likes JSP and the fly likes How to display sum of numbers from a java class to a jsp using custom tag? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "How to display sum of numbers from a java class to a jsp using custom tag?" Watch "How to display sum of numbers from a java class to a jsp using custom tag?" New topic
Author

How to display sum of numbers from a java class to a jsp using custom tag?

ashley Jug
Ranch Hand

Joined: Sep 02, 2011
Posts: 60
Hello,

i have a jsp page that takes 2 numbers.A servlet that initialiase the two number based on data sent from the jsp as follows:


Then the servlet call a method as follows to add the two numbers:


My question is, is it possible to display the total variable in a jsp using a custom tag?
If it is possible how do i initialise the custom tag to take the variable total?

Thanks a lot.





Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

I guess it might be, given a suitable interpretation of your question. For example: what does it mean to you to "display a variable in a JSP"? I'm finding your terminology a bit hard to follow.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

Why do need a custom tag? Just have the servlet set the value as a scoped variable and use the EL to display it. Why make things one iota more complicated than they need to be?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
ashley Jug
Ranch Hand

Joined: Sep 02, 2011
Posts: 60
Hello,

thanks a lot its all new to me, i will use scope variable and jstl.

Do you know any online resources where i can get turorial of using scoped variable with jstl please?

Thanks again.
Piyush Joshi
Ranch Hand

Joined: Jun 10, 2011
Posts: 207

Execuse me for diverting from the topic. But there is a bug in your code:
ashley Jug wrote:

If num1 or num2 were null then how you will be able to call equals() on them, causing NullPointerException!
Please correct this


Piyush
ashley Jug
Ranch Hand

Joined: Sep 02, 2011
Posts: 60
Hello,

Thanks to point out the null pointer check...but i am using jstl now to display the variable total but no data is displayed.
My jsp file to display the total is :


My jsp file to input the numbers is as follows:


My servlet MathUtilsController is as follows:


Java class is as follows:


Not sure what i am missing here.

Any suggestion please why the variable total is not displayed?

thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67



and



do not match.

Why the extraneous prefix?

(Also, use lowercase for HTML -- all uppercase looks so 1998.)
ashley Jug
Ranch Hand

Joined: Sep 02, 2011
Posts: 60
Hello,

opps sorry for the prefix i know its not being used. Thanks a lot now its being displayed.

Quick question:

I was going through an on line tutorial to display the data on a jsp they use:

<td>${Classname.variable}</td>

For instance:

<td>${MathUtilsController.total}</td>

When do we use the above format to display data in a jsp?

For the html you mean use lowercase to define the html tag?

Thanks again learning a lot from this forum.
Thanks again.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

${xyz.abc} when abc is a property of bean abc.

I mean <td> and not <TD>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to display sum of numbers from a java class to a jsp using custom tag?