Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

servlets sending data to jsp

 
Jonathon Stride
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • 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 ?

 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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 ...
 
Ram Para
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64833
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64833
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic