JSP Passing Class to another JSP

Posted on 2005-04-11
Last Modified: 2010-04-01
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

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

Question by:waipahu
    LVL 19

    Expert Comment

    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

    Author Comment

    Can you give code example of how I would put my Product object in a session:
    Example in the calling jsp and receiving jsp.

    Author Comment

    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.
    LVL 19

    Expert Comment


    Product p = new Product

    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

    Author Comment

    Thanks! It works, but I need a way to attach the following statement:
    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.

    LVL 19

    Expert Comment

    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...

    Author Comment

    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!
    LVL 19

    Expert Comment

    yes you sure can call the jsp2 from your javascript function...

    Author Comment


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

    LVL 19

    Accepted Solution

    it depends... if you want to submit the page to your jsp then


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

    if you want to open it in the same page then

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Suggested Solutions

    In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
    It can often be challenging to stay relevant in the rapidly evolving world of technology. This can make recruiting talent difficult for companies of all sizes.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now