views:

73

answers:

2

Hi! I am creating a web application using EJBs and servlets. I have a page which displays a list of all items in the database. I would like to provide an option for the user to click on one of these items and this opens the SHOW servlet which gathers info regarding the item onto the page. I do not want to create a page for every single item. Instead I would like to create ONE SHOW servlet which can be used for all items. I am not sure how to provide this option through clicking on the name of an item, and also how to send the parameters...since it depends on what item the user chose.

Can someone help me please?

Thank you

A: 

Pass the unique ID of the item into the SHOW servlet. Then get that item's data from the DB and create your new page with that data.

Try having the show link point to your show servlet like this: "/ShowServlet?itemID="+itemID

Zack
+3  A: 

When you generate the product listing, you can just make the IDs of all the database items parameters in the link.

<a href="/ShowProduct?productID=Q85349">Product Foo</a>

Then in the doGet() method of your ShowProduct servlet, you can call the HttpServletRequest.getParameterValues() method to get that parameter's values and do the lookup in your database.

e.g.

public void doGet(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException
{
    String[] params = request.getParameterValues("productID");
    String productID = params[0];
    ...
}
Mitch Flax
Thank you so much. This is exactly what I was looking for!
Lily