|
Hi,
I'm new to servlets...
What i want to do:
In a servlet(LookupCD) i create an instance of
an Enterprise JavaBean(session bean).
The servlet generates an html-page with all info of an entitybean and also a link. When i click on this link it starts up a new servlet but i want to pass that instance of the sessionbean on to this new servlet(AddToBasket) I know that this is possible with the request
dispatcher, and to set Attribute on request
So when i click the link the requestdispatcher
must be called...
I have added some code of the lookupCD servlet. Can you tell me what i have to change in this
code-example???
Best Regards,
Jochen Vastmans
[EMAIL PROTECTED] // CODE public class LookupCompactDisc extends
HttpServlet implements SingleThreadModel{
public void init(ServletConfig config) throws ServletException { super.init(config); initiate(); //creates an instance of the sessionbean - cs -> (CustSession) context = config.getServletContext(); } public void doGet(HttpServletRequest req, HttpServletResponse res){ //delagate to doPost doPost(req,res); } public void doPost(HttpServletRequest req, HttpServletResponse res){ try
{
String artist=req.getParameter("artist").toLowerCase(); String title=req.getParameter("title").toLowerCase(); // Vector with instances of the entitybean - (CompactDisc) Vector compactdiscs=cs.lookupCompactDisc(artist,title); // HERE A ADDED -cs- AS ATTRIBUTE req.setAttribute("CustSession",(CustSession) cs); // this will generate html res.setContentType("text/html"); PrintWriter out = res.getWriter(); if(compactdiscs == null) out.println("No item was found."); else{ out.println("<html><head></head><body>"); for(int
i=0;i<compactdiscs.size();i++){
Functions f = new Functions();
CompactDisc cd = (CompactDisc) compactdiscs.elementAt(i); long id = cd.getID(); String pid = String.valueOf(id); String cdname = f.toUpperCase(cd.getArtist()); String cdtitle = f.toUpperCase(cd.getTitle()); String cdrc = f.toUpperCase(cd.getRecordCompany()); double cdprice = cd.getPriceEuro(); String price = String.valueOf(cdprice);
out.println("<p><b>"+cdname+"</b><br>");
out.println(cdtitle+"<br>"); out.println(cdrc+"<br>"); out.println("Price: "+price+" euro</p>"); // HERE IS MY PROBLEM!!!!
// HERE IS THE LINK TO START THE OTHER SERVLET // I want to pass the sessionbean -cs- on to AddToBasket // when the link is clicked... out.println("<a href="+"http://<my host>/servlets.customer.AddToBasket?id="+pid+">Buy Item</a>"); // I think it should be context.getRequestDispatcher("/servlet.customer.AddToBasket").forward(req,res); }
out.println("</form></body></html>");
} } catch (Exception e) { e.printStackTrace(); } } .... } |
