Solved

RequestDispatcher

Posted on 2003-11-17
2
1,329 Views
Last Modified: 2008-02-26
In regards to a previously answered question and a previous answer

Question:

>>ok, I have a jsp called ItemDetails.jsp and a servlet called ItemDetailsServlet, what i want is when the jsp is loaded
>>into a browser I want the servlet to perform a jdbc query and return the data to be displayed to the jsp, but i have
>>absolutely no idea how to do this!

>>Any help appreciated greatly!

Answer:

>Either I have mis-understood this question, or you mis-understandtand what JSP's and Servlets are and do.

>A JSP is never really "loaded into the browser" - it is executed on the server and generates HTML output - that's what >is loaded into the browser.

>I think that what you really want is JSP to perform the data lookup and generate the results. If you already have Servlet >that does this, then you can call the servlet from within the JSP - or, if you have access the the source (or can
>decompile it) - you can write the functional Java into your JSP.

>Use the RequestDispatcher interface to call ("dispatch") from a JSP to a servlet.
>Get a RequestDispatcher by calling the getRequestDispatcher(java.lang.String path) method of ServletRequest


In regards to the last part of this answer I am slightly confused as to which order they should be called

does "dispatch()" take the results of a servlet to a JSP OR does it take the results from a JSP to a servlet?

is getRequestDispatcher(String path) again called from a JSP to a servlet or vice versa?

The exact nature of this problem is that we are creating a eBay type application and have a search JSP which takes in the query, the servlet is then used to query the database for the item, and then we need to send the results to the item details JSP in order to display it, the main problem is that I am unsure of how to "wrap up" the results and return it to the item details page...

so to clarify....is this feasible to use the RequestDispatcher to handle this or am i on the wrong track?
0
Comment
Question by:gingermeatboy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 14

Accepted Solution

by:
kennethxu earned 100 total points
ID: 9764893
>> have a search JSP which takes in the query, the servlet is then used to query the database for the item, and then we need to send the results to the item details JSP in order to display it

That's what you need to do:
1. seach.jsp takes the query and submit to the ItemDetailsServlet, NOT ItemDetails.jsp.
2. ItemDetailsServlet search the database and save the result in a whatever collection object, say a List. then set this List object as request attribute.
3. ItemDetailsServlet forward request to ItemDetails.jsp, remember to get your dispatch() method follow by return statement.
4. ItemDetails.jsp retrieve the List object from request attribute bag and display it.

Let me know if you have further enquires.
0
 
LVL 6

Expert Comment

by:jarasa
ID: 9771880
Hi gingermeatboy.
Just wanted to clarify you that JSP and Servlet is basically the same thing, the JSPs were created to saparate the HTML things from the Java Code but the web server copiles the JSP and makes a Servlet.

Well What I am trying to say is that you don't need a Servlet to make your searches and return the data, you can do that on Normal Java Classes and Dispath them to a JSP or a Servlet or what ever you may have on the other side.

Just make your code work with java and then think about the presentation.

Javier
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
love6 challenge java 31 133
maven j2ee examles 2 64
servlet example 17 72
by zero exception 10 83
This article describes a serious pitfall that can happen when deleting shapes using VBA.
Unified and professional email signatures help maintain a consistent company brand image to the outside world. This article shows how to create an email signature in Exchange Server 2010 using a transport rule and how to overcome native limitations …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question