• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling Servlets from a jsp

 
Nagarajan Krishnamurthy
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

Nikhil
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64720
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
     
    Paul Bourdeaux
    Ranch Hand
    Posts: 783
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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 "Test.do" 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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic