JSP Passing Class to another JSP

Example 2 jsp programs.

JSP1.jsp   contains information about  Products. All data is stored in the Products Object.
My html screen looks like below:.   I have the Products in a ArrayList and I display it on screen as follows:
I have this listed in a table with an Edit button.    I want to call another jsp program to display the detail information
about each product.  How do I pass the Product object to the other JSP?  I am able to pass individual fields and I also can
pass the address of the Product Object.    The problem is
request.getParameter("edtButton") returns a string and not an object. HELP.   Also the Input TYpe="Submit"
how do i change the label of the button from using the VALUE. Value is used as a paramter, so I not sure how to change the label.

Program making the call
out.println("<TD VALIGN=" + '"' + "TOP" + '"' + "><h5><INPUT TYPE=" + '"' + "submit" + '"' + " name=" + '"' + "edtButton" + '"' + " VALUE=" + '"' + Product + '"' + "</h5></td>");

Program being called
request.getParameter("edtButton")

         Product     Description           Price
Edit   Tire          Auto Tire             50.00
Edit   Mirror      Rear View Mirror  25.00  
Edit   Muffler      Exhaust              125.00

waipahuAsked:
Who is Participating?
 
KuldeepchaturvediConnect With a Mentor Commented:
it depends... if you want to submit the page to your jsp then

document.forms[0].target="yourjsp.jsp";
document.forms[0].submit();

or if you want it to open as a separate page then you will have to do
window.open("yourjsp.jsp);

if you want to open it in the same page then
window.location="yourjsp.jsp"
0
 
KuldeepchaturvediCommented:
you can not pass a object from HTML page... (jsp once displayed is nothing but a html page)...
though a object can be passed from one jsp to another in different ways..
one is to put the object in session, another one is to set it as attribute of the request..
bur from a submit button all you can get is name value pairs in string form by using request.getParameter();
in your case if user is suppose to edit the values then in the jsp you will have to get all the request parameters again and build your object from scratch
0
 
waipahuAuthor Commented:
Can you give code example of how I would put my Product object in a session:
Example in the calling jsp and receiving jsp.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
waipahuAuthor Commented:
I was wondering if from the form submit button I could call a javascript function
that would set the attributes and then call my other jsp from the javascript function?
Can that be done, if it can how wo uld the code look like.
Thanks
0
 
KuldeepchaturvediCommented:

<%
Product p = new Product
session.setAttribute("MyProduct",p);

to get it out of the session
Product p = (Product)session.getAttrbute("MyProduct");

To your second question, no you can not do it with javascript as javascript is client specefic and it can not access the objects at the server side..
though if your Product is a bean then you can use
<jsp:setProperty> tags to set the values directly in product object from the html page
0
 
waipahuAuthor Commented:
Thanks! It works, but I need a way to attach the following statement:
session.setAttribute("MyProduct",p);
to a detail line in my html SUBMIT line.

I have serveral products
I have a while loop
while (itr.hasNext())
{
    out.println("<TD VALIGN=" + '"' + "TOP" + '"' + "><h5><INPUT TYPE=" + '"'
   + "submit" + '"' + " name=" + '"' + "edtButton" + '"' + "VALUE=" + '"' + "edit" + '"'
  + "</h5></td>");
}
 
What you see on the browser
edit    product1      xxxxx   5.25
edit    product2      xxxxx  12.00
edit    product3      xxxxx   13.50

I need a way to execute the line
session.setAttribute("MyProduct",p); attached to the submit button so I can select
the correct product.



0
 
KuldeepchaturvediCommented:
Again as I said, its not a client side operation and hence it can not be attached to the submit button as is...


what you can do is that to whatever jsp you are submitting the page can get the request parameters and make the product to be placed in the session...

or better still while your making this HTML, you can put in your products in the session with the id as the row number...

now when you submit back to the jsp.. you can get the row number from the request parameter.
and then using this parameter get your product back from the session....

One warning though that your session will be quite heavy with all those products sitting in the session...
0
 
waipahuAuthor Commented:
What I did was use a Button instead of a submit. I then called a javascript function
to set the attribute.
Question can I call my jsp page 2 from my javascript function?
Thanks so much for your help!
0
 
KuldeepchaturvediCommented:
yes you sure can call the jsp2 from your javascript function...
0
 
waipahuAuthor Commented:

How would you call jsp from my javascript function()?

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.