• 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

How to get an integer value from controller to jsp

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have a list of size 3

in my controller i say

int noofcards=list1.size(); which returns me the value 3

i want to print this value in my jsp. How do i do it ?

i tried this way

Controller
request.setAttribute("noofcards",noofcards);

JSP
<%= request.getAttribute("noofcards") %>

Thanks in Advance
 
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Madhavi, I think in the controller you are using a link to go to another jsp page, or you are redirecting the page to the next jsp page.

Once you are redirecting to another page a new request is created, so the attribute in the previous request is lost. In this case you have to forward your request to the next page and get it done. You must use request dispatcher to do so.

Sample code:




Note tha noofcars is an object

If you don't want to use request dispatcher you have to go with html forms to accomplish this task.

Have a nice day.
 
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi madhavi ,

request.setAttribute("noofcards",noofcards);



In above code "noofcards" variable is int as you shown, it should give translation error here,
because request.setAttribute takes java.lang.Object as second parameter..


 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for correcting me Mr.Shailesh Narkhede
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can use sessions to forward your necessary output from the servlet to jsp and there use custom tags to get the output
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[you need to set the code like

in controller.....

setAttribute method takes String , Object as argumet for make it sure...

after this you just forward the request to the view(JSP) using requestDispatcher....

and write a following code in your jsp..



it will give you value as int...

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

azhar jodatti wrote:

it will give you value as int...

hope it will help you



Or use EL and you only have to write:



in the JSP
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The session is not required. Be sure to forward rather than redirect to the JSP. Scriptlets are no longer using JSP pages -- be sure to use the EL.
 
madhavi sastry
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm still not able to print the value in my jsp

I tried <%int value=Integer.parseInt((String)request.getAttribute("noofcards"));%>

It shows NumberFormatException

I tried ${noofcards} also. It is not printing any value

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

madhavi sastry wrote:I'm still not able to print the value in my jsp

I tried <%int value=Integer.parseInt((String)request.getAttribute("noofcards"));%>

It shows NumberFormatException

I tried ${noofcards} also. It is not printing any value



For trying ${noofcards} you need to have the jstl 1.2 library in your system, if you have it and you don't see the value is because that attribute is null or an empty string, if you get a NumberFormatException is because request.getAttribute is returning something that cannot be cast to a number, first of all you have to know what are you receiving in the attribute, do a System.out.println("myAttribute: [" + (String)request.getAttribute("noofcards") + "]"); and look in the console what you are getting.
 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi madhavi, please paste some sample code so that we can know where the error lies and give suggestions
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
madhavi!

just convert that object into an Integer object.

int i = (Integer) request.getAttribute("noofcards");

it may give you the answer!
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Albareto McKenzie wrote:For trying ${noofcards} you need to have the jstl 1.2 library in your system.


Not correct. The EL does not require the JSTL to operate. The web app must be properly configured.

What the OP is trying to do is simple, but apparently his web app is misconfigured. Until he provides enough info to diagnose the problem, the problem will persist.

All this nonsense with casting and such will not help.

So.... is a forward or a redirect being used? and how is the web app being configured in the web.xml? These are the relevant questions that must be answered.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic