This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes JSP and the fly likes JSP/Servlet combination Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » JSP
Bookmark "JSP/Servlet combination" Watch "JSP/Servlet combination" New topic

JSP/Servlet combination

Napa Sreedhar
Ranch Hand

Joined: Jan 29, 2002
Posts: 62
My JSP page should display an ArrayList of values.
JSP page name = index.jsp

The Arraylist is returned from a servlet.
ServletName =

When I type http://localhost:7001/webapp/index.jsp in a browser, I want this JSP page to talk to the servlet and get the ArrayList of values.
The url should still appear as http://localhost/webapp/index.jsp

What is the best strategy for doing this.

Sreedhar Napa
Anthony Watson
Ranch Hand

Joined: Sep 25, 2003
Posts: 327
Servlets don't return anything. Thus, the servlet must put the ArrayList in some scope such as request scope and have the JSP get the variable out of that scope. The functionality of getting the ArrayList could always be implemented in a regular Java object and you could call a method like getList() on that object from your JSP.

Anthony W.<br />MCP, SCJP 1.4, SCJD, SCWCD 1.3, SCWCD 1.4, SCBCD
Napa Sreedhar
Ranch Hand

Joined: Jan 29, 2002
Posts: 62
I understand that servlets dont return anything.
I have to set attribute in some scope for accessing it through the jsp page.

To be more clear I am following the MVC pattern where in a controller servlet calls model (DAO or beans) for performing some process and then forward the results to a jsp page(view).

My implementation part:
Model: DAO's and Beans
Controller: TestServlet and other servlets
View: index.jsp

I am getting a nonempty arraylist from database through DAO's (Data Access Objects)

My first page happens to be index.jsp.
When I say http://localhost:7001/webapp/index.jsp I want the page to get displayed with the nonempty arraylist from the DAO's

Problem is I dont want to use DAO's directly in my JSP page. I want this to happen through the controller TestServlet.

Please let me know how this can be acheived.

Sreedhar Napa
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63195

Problem is I dont want to use DAO's directly in my JSP page. I want this to happen through the controller TestServlet.

Good practice. But your thinking is a bit backwards -- you are asking a JSP to get info from a servlet rather than the servlet giving info to the JSP. Usually it is the servlet that is invoked, does its processing, and then forwards on to the JSP for processing.

But, from what you are saying, you want the default JSP file, index.jsp, to start this action.

So, what I would do in your case, is to have index.jsp be just a placeholder page that immediately forwards on to your servlet. Check out the <jsp:forward> action to accomplish this.

Your servlet would then gain control, perform its processing (including putting the list on the request as an attribute), and then forward on to a JSP page, perhaps results.jsp that displays the results.

Usually in a Model 2 (MVC) web app, URLs resolve to the servlet controller and not to JSP files. But index.jsp is special case that could be handled as described above.

It's also possible to set up your app such that the 'default' URL is not index.jsp, but rather your servlet, but I've heard that sometimes there are problems doing this in older containers. You may want to investigate this tactic.
[ August 13, 2004: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Napa Sreedhar
Ranch Hand

Joined: Jan 29, 2002
Posts: 62
Thank you very much
Napa Sreedhar
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: JSP/Servlet combination
It's not a secret anymore!