information passed from servlet to JSP

This is a discussion on "information passed from servlet to JSP" within the Other Programming Languages section. This forum, and the thread "information passed from servlet to JSP are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > Other Programming Languages

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Nov 14th, 2007, 15:12
Junior Member
Join Date: Oct 2007
Location: Holland
Posts: 49
Thanks: 0
Thanked 0 Times in 0 Posts
information passed from servlet to JSP

hello,
sorry for cross-posting, but I first put my thread in the JavaScript forum...

so, here is my problem:

In my JSP pages I'd like to use a certain info passed by the controller servlet.

For instance, in my servlet I have the following:
Code: Select all
Animal myAnimal = new Animal();
// Set attributes
request.setAttribute("myAnimal", myAnimal);
getServletContext().getRequestDispatcher("/myJSP.jsp").forward(request, response);
Now in the jstl tags of myJSP.jsp I can use the following:
Code: Select all
<c:forEach items='${myAnimal.body.legs}' var="leg">
   ......
</c:forEach>
BUT I cannot use the following:
Code: Select all
<% for(int i=0; i<myAnimal.getBody().getLegs().length; i++) { %>
   ......
<% } %>
... it complains that myAnimal cannot be resolved

I also tried:
Code: Select all
<%@ page import="com.mycompany.Animal" %> 
<%! Animal jspAnimal = (Animal)request.getAttribute("myAnimal"); %>
...but then I get "request cannot be resolved" for the second line...


Could you please explain why I cannot use "myAnimal" in the plain JSP tags???


Thans a lot ! ! !
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old May 26th, 2008, 18:22
Up'n'Coming Member
Join Date: Aug 2006
Location: Peru
Age: 22
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Re: information passed from servlet to JSP

Hey there:
The reason why you can't use myAnimal within the tag is that that object doesn't exist, thus it cannot be resolved. Now you were close whit this line

<code><%! Animal jspAnimal = (Animal)request.getAttribute("myAnimal"); %></code>

but it should have read:

<code> <% Animal jspAnimal = (Animal)request.getAttribute("myAnimal"); %> </code>

and then, later on:

<code><% for(int i=0; i<jspAnimal.getBody().getLegs().length; i++) { %>
......
<% } %></code>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with my Servlet - retrieving images from database Love2Java Other Programming Languages 2 May 27th, 2008 17:15
Passed Aesir Webforumz Cafe 1 Apr 21st, 2008 19:46
Formatting Passed Value Within ASP Page In Textarea? Monie Classic ASP 15 Nov 28th, 2007 00:57
[SOLVED] AJAX call to a servlet pesho318i JavaScript Forum 9 Oct 28th, 2007 13:40
search results with servlet or JSP twix_ Other Programming Languages 1 Nov 2nd, 2006 22:25


All times are GMT. The time now is 13:11.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved