aspose file tools*
The moose likes Servlets and the fly likes servlets sending data to jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlets sending data to jsp" Watch "servlets sending data to jsp" New topic
Author

servlets sending data to jsp

Jonathon Stride
Ranch Hand

Joined: Aug 06, 2009
Posts: 34
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 ?


2 interviews failed cause of not having SCJP ( and counting...)
not anymore !

SCJP 6 (70%)

now ready to count other reasons :P...
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
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).


JDBCSupport - An easy to use, light-weight JDBC framework -
Jonathon Stride
Ranch Hand

Joined: Aug 06, 2009
Posts: 34
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

Joined: Feb 23, 2009
Posts: 1183
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

Joined: Jul 09, 2008
Posts: 50
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.


Ram Parashar
http://doinfinite.com
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61202
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61202
    
  66

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

Joined: Aug 06, 2009
Posts: 34
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: servlets sending data to jsp