aspose file tools*
The moose likes JSP and the fly likes How to get an integer value from controller to jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "How to get an integer value from controller to jsp" Watch "How to get an integer value from controller to jsp" New topic
Author

How to get an integer value from controller to jsp

madhavi sastry
Greenhorn

Joined: Oct 28, 2009
Posts: 3
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
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 800

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.
Shailesh Narkhede
Ranch Hand

Joined: Jul 10, 2008
Posts: 368
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..



Thanks,
Shailesh
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 800

thanks for correcting me Mr.Shailesh Narkhede
PrasannaKumar Sathiyanantham
Ranch Hand

Joined: Nov 12, 2009
Posts: 110
you can use sessions to forward your necessary output from the servlet to jsp and there use custom tags to get the output


To err is human,
To forgive is not company policy
azhar jodatti
Greenhorn

Joined: Oct 18, 2009
Posts: 12
[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


..FEEL IT..
Albareto McKenzie
Ranch Hand

Joined: Apr 08, 2009
Posts: 298
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
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61043
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
madhavi sastry
Greenhorn

Joined: Oct 28, 2009
Posts: 3
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

Joined: Apr 08, 2009
Posts: 298
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

Joined: Sep 15, 2009
Posts: 800

Hi madhavi, please paste some sample code so that we can know where the error lies and give suggestions
yuvaraj KumarAmudhan
Ranch Hand

Joined: Aug 22, 2009
Posts: 110
madhavi!

just convert that object into an Integer object.

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

it may give you the answer!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61043
    
  66

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.
 
 
subject: How to get an integer value from controller to jsp