?
Solved

JSP Passing Class to another JSP

Posted on 2005-04-11
10
Medium Priority
?
219 Views
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
request.getParameter("edtButton")

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

0
Comment
Question by:waipahu
  • 5
  • 5
10 Comments
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 13758891
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
 

Author Comment

by:waipahu
ID: 13765548
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
 

Author Comment

by:waipahu
ID: 13765604
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 13765889

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

Author Comment

by:waipahu
ID: 13767430
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
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 13767488
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
 

Author Comment

by:waipahu
ID: 13767717
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
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 13767748
yes you sure can call the jsp2 from your javascript function...
0
 

Author Comment

by:waipahu
ID: 13768327

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

0
 
LVL 19

Accepted Solution

by:
Kuldeepchaturvedi earned 500 total points
ID: 13773222
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

I came across an unsolved Outlook issue and here is my solution.
Tech giants such as Amazon and Google have sold Alexa and Echo to such an extent that they have become household names. And soon they are expected to be used by commoners in their homes, ordering takeout, picking out a song, answering trivia questio…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

809 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