• 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

servlets sending data to jsp

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so i have a jsp that calls a servlet to view a database
the servlet will access the database and prepare the resultset(or some variables to reflect the data)

but then what ? how do i send data from my servlet to jsp(a jsp showing the result) ?



1. getServletContext().set and .get ? but what variable should i use ? a double dimensioned array ?
2. cant be done without struts/ejb (more study ) ?
3. using ajax ?

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

so i have a jsp that calls a servlet to view a database



First, a JSP should never call a servlet - at least in a well structured web app. It happens vice versa.

The general approach is sending the request to the servlet which does the business work and forward the result to the JSP for display.

In your case I think you have more than one row that you read from your database.

I would suggest you create a JavaBean that represents a row. As you iterate over each row, you create one object per row and add it to a list.

You set this list as a request parameter (request.setAttribute("DB_RESULT", list)) and then forward using



The JSP then can happily iterate over the list (favor JSTL tags instead of scriptlets) and read the bean one by one (favor EL instead of scriptlets or expressions).
 
Jonathon Stride
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so i need to learn EJB ?

currently im aware of JSP/Servlet concepts and
c/c++ programming and basic core java

but i do get your point. however instead of a bean cant i use an array (2d) and save some work and study :P
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You do not need to learn EJB.

A JavaBean has nothing in common with EJBs.

It is a simple class that has private property fields with accessor and mutator methods.



Also, check the JavaBean Specification ...
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with "Sebastian Janisch" ,
Bean doesn't means EJB.

Bean a simple class having getters and setters. (No need to go in deep)

You can refer some tutorial on DTO's.(How they works)

No need to learn EJB.
 
Sheriff
Posts: 67747
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

Sebastian Janisch wrote:First, a JSP should never call a servlet - at least in a well structured web app.



By "call" (a really poor term to use), the OP probably means submitting from the JSP to the servlet.

 
Bear Bibeault
Sheriff
Posts: 67747
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
Also, if the data is only going to be displayed, the creation of a list of beans may be overkill unless there some other behavior or use for the beans. A String[][] array (or even Object[][]) will work just fine for carrying the data to be displayed from the servlet to the JSP.

It all depends on how the data will be used on the JSP.

What is to be avoided at all costs is passing a resultset from the Java code to the JSP.
 
Jonathon Stride
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ty guys a lot =)

the data is just for viewing , like given a city , the result of all records that have that city and things similar to that
 
Is that a spider in your hair? Here, threaten it with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic