• Post Reply Bookmark Topic Watch Topic
  • New Topic

JSTL to Servlet

 
Alina Grimshaw
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm creating a program that takes in parameters, queries the db for with those fields and returns the results on a table in a jsp page.

The flow that I have in place is that the DAO returns an ArrayList of those results. I then iterate through the arrayList and create a bean for each element in that array list. Each bean is then placed in an array. All of this is taking place in the servlet.

How would I go about accessing that array from my jsp page? I was thinking to do something like

<c:forEach var = "bean" items = ${beanArray} >
<c:out value = ${bean.account}/>
<c:out.... etc>
<c:forEach/>

I guess my question is how can I guarantee that beanArray is the same beanArray I created in my servlet?

Thanks!
Alina

ps. hello java ranch world!
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You set the array as a scoped variable in request scope using request.setAttribute().
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. Be sure that the checkbox labeled "disable similes" is checked when posting code. It'll make your code look less surprised!

(I've done it for you for your first post)
 
Alina Grimshaw
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks so much! i just need to set it as scoped in the servlet right? or do i also have to change the scope of the jsp page?

thanks again!
 
Alina Grimshaw
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For some reason...its not printing the table on my jsp. Is there a small error that I'm not seeing?

JSP------


Servlet----------
 
Mohamed Sanaulla
Bartender
Posts: 3166
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




Is it beanArr.dashBean? or just beanArr? or is it request.beanArr?
 
Alina Grimshaw
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've tried both ${beanArr} and ${beanArr.bookBean} and they don't work.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please be sure to use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information.

Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you transitioning from the servlet to the JSP?
 
Alina Grimshaw
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was assuming that the setAttribute would do that. There really isn't any connecting code.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to be doing something to get to the JSP. If the servlet is the controller for the JSP, then you should be using a request dispatcher to forward to the JSP when the servlet controller has finished setting up the JSP's environment.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alina Grimshaw wrote:I was assuming that the setAttribute would do that. There really isn't any connecting code.

No, set attribute only sets up the scoped variable. It has nothing to do with transitioning to a particular JSP. That's where the request dispatcher comes in.

Maybe this article might be helpful.
 
Alina Grimshaw
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes! The request dispatcher was what I was missing!

Thanks so much.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!