• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get an integer value from controller to jsp

 
madhavi sastry
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • 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.
 
Shailesh Narkhede
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • 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
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for correcting me Mr.Shailesh Narkhede
 
PrasannaKumar Sathiyanantham
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • 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
 
azhar jodatti
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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
 
Albareto McKenzie
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • 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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65125
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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: 299
  • Mark post as helpful
  • send pies
  • 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
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi madhavi, please paste some sample code so that we can know where the error lies and give suggestions
 
yuvaraj KumarAmudhan
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 65125
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic