It's not a secret anymore!
The moose likes JSP and the fly likes Calling Servlets from a jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Calling Servlets from a jsp" Watch "Calling Servlets from a jsp" New topic

Calling Servlets from a jsp

Nagarajan Krishnamurthy

Joined: Sep 26, 2003
Posts: 20
Am currently working in a module in which am calling a servlet from my jsp ,based on some condition the servlet calls a java class.The java class after performing some operations returns a HashMap ,which i need to display in the main jsp from which i called the servlet.
But what happens is jsp is getting executed and displays 0 [If there were no objects found].
But actually speking my class returns the hashmap.

How should i solve this problem?
Nikhil Menon
Ranch Hand

Joined: Nov 22, 2004
Posts: 70
Hi how are you returning the Hashmap from servlet? as an attribute or parameter and in which scope?

Make sure your class has not thrown any exception and is containing elements in hashmap. Please send the servlet and jsp code to verify

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63873

in which am calling a servlet from my jsp

This is a very ambiguous statement and its meaning is not clear. What do you mean by "calling" a servlet?

Are you:
  • Calling the doPost or doGet method directly?
  • Submitting a form to the servlet?
  • Invoking a URL referencing the servlet?
  • Including the servlet output in the JSP via the <jsp:include> action?

  • There really is no "calling a servlet from a JSP" (unless you are calling the doPost() or other servlets method directly, whch would be an egregious thing to do).
    [ April 15, 2005: Message edited by: Bear Bibeault ]

    [Asking smart questions] [About Bear] [Books by Bear]
    Paul Bourdeaux
    Ranch Hand

    Joined: May 24, 2004
    Posts: 783
    You question does seem strange... It sounds as if you are trying to remove the business logic from your jsp page (which is a very good thing to do), but you are doing it backwards. Usually the servlet is invoked first, and then forwards the request to a jsp. Here is an example of putting your busines logic in a servlet... When a user clicks on the "" link...

    1) The container looks at the address in the request, and based on that, finds the appropriate servlet in the DD
    2) This servlet can call outside java classes, do business logic, and prepare that lovely HashMap you wish to use.

    3) The servlet then assigns the HashMap to an attribute (probably in request scope).
    4)The servlet uses a RequestDispatcher to forward the request to the jsp.

    5) Now the jsp can access the HashMap via the attribute.

    “Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook
    I agree. Here's the link:
    subject: Calling Servlets from a jsp
    It's not a secret anymore!