Link to home
Start Free TrialLog in
Avatar of Neeraj08
Neeraj08

asked on

invoking jsp from a java program with java objects as passed attributes

We have a java program that forms a url
  String urlString = http://serverInfo/x.jsp?param1=zzz
  URL url = new URL(urlString);
It then opens connection to this url, gets its contents as String and uses them as needed.

x.jsp in turn uses request.getParameter("param1') to get the parameter value and uses it to render the page.

Now instead of passing a String parameter "param1" to jsp, we want to pass an arbitrary java object. The idea is that jsp should be able to get this object using code like request.getAttribute("myObject");

My question is, is it possible to grab the request object and do request.setAttribute() in the java program, before getting the contents of url. If not, is there any way to get the same effect, that, jsp being able to use request.getAttribute('myObject") to grab the object.

thanks
ASKER CERTIFIED SOLUTION
Avatar of evnafets
evnafets

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of rrz
rrz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I will leave the following recommendation for this question in the Cleanup Zone:
Split between rrz@871311 and doraemon-nolife.

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

mrcoffee365 Experts Exchange Cleanup Volunteer
Avatar of evnafets
evnafets

Objection to the recommendation:
IMO doraemon-nolife's solution is not correct.
The OP is talking about making an HTTP request from a "client" using the java.net classes.
Thus only request parameters can be used.  request.setAttribute is not applicable in this scenario.

Points to rrz@871311- correct solution + example code.



You could be right.  I thought that it was a reasonable interpretation that the asker wanted to know how to have the object available to 2 different pages on the server, and doraemon-nolife's answer showed how to save an object in the session on the server and get it.  But it's open to discussion.  Do you think my alternate interpretation is possible?
And actually, I think possibly all 3 answers should have the points split -- yours was right as well, and was first, if the user wants to pass in java objects from the client.
> I thought that it was a reasonable interpretation that the asker wanted to know how to have the object
>available to 2 different pages on the server,
I don't read the question that way at all.  Its using a URLConnection, and passing a parameter.  
The OP is specifically asking if/how he can get the request object to call set attribute on it.

>doraemon-nolife's answer showed how to save an object in the session on the server and get it.
He didn't mention session, only request attributes.  If we were talking about pages on the same server, yes you could use request attributes, and you would use the requestDispatcher to transfer control (standard servlet/jsp).  

My main aim was to prevent someone reading this post later from trying what I think is a wrong approach.  That objective has been achieved by posting my opinion here :-)
You could definitely be right, and I see where you got your interpretation.   But it also seems that the asker has access to both the client and the server, and could possibly solve their problem with data generated on the server and kept there in the session.  They're very unlikely to create a Java object in their client program and pass it to the server -- despite your and rrz@871311's explanations of how one might do that.  Without more input from the asker, I'm going to go with both interpretations, even if the weight tips more toward yours.
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I will leave the following recommendation for this question in the Cleanup Zone:
Split between rrz@871311 {19584998} , evnafets {19560672}, and doraemon-nolife {19562910}.

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

mrcoffee365 Experts Exchange Cleanup Volunteer
A 3 way split is good.
The question was ill-conceived.  
>invoking jsp from a java program with java objects as passed attributes
The author didn't specify what kind of java program. I just assumed he meant a stand alone application. It would not have access to the request object in the JSP.      rrz