Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSP/Servlet combination

 
Napa Sreedhar
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My JSP page should display an ArrayList of values.
JSP page name = index.jsp

The Arraylist is returned from a servlet.
ServletName = TestServlet.java

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.

Thanks,
Sreedhar Napa
 
Anthony Watson
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Napa Sreedhar
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand that servlets dont return anything.
I have to set attribute in some scope for accessing it through the jsp page.

//Query
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.

Thanks
Sreedhar Napa
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65223
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Napa Sreedhar
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much
Napa Sreedhar
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic