?
Solved

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

Posted on 2007-07-24
12
Medium Priority
?
349 Views
Last Modified: 2013-11-24
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
0
Comment
Question by:Neeraj08
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 4

Accepted Solution

by:
evnafets earned 172 total points
ID: 19560672
No, it isn't possible in the way you have described.  You have to work within the limitations of HTTP.  

You can't easily pass java objects over HTTP.  Request parameters are string based only.  You could do String -> Object -> String conversions.  Alternatively if you want to get tricky, you can send binary information as you would an attached file (multi-part formdata handler required)

You can have your own Object->String conversion, or you could Serialize the objects, binhex (to convert to String) and then send that as a request parameter.  

Hope this helps,
evnafets

0
 
LVL 4

Assisted Solution

by:doraemon-nolife
doraemon-nolife earned 164 total points
ID: 19562910
In the servlet you may do this:

request.setAttribute("key", yourObject);

The syntax is request.setAttribute(String arg0, Object arg1);

The arg0 is served as the key for your future retrieval. Example: request.getAttribute("key");

Please note that any object retrieved fro request.getAttribute method must be cast back to its original data type before you can use it.

:)
0
 
LVL 28

Assisted Solution

by:rrz
rrz earned 164 total points
ID: 19584998
Here are a couple of JSPs that demostrate passing objects. You could do the same thing from your program.

<%@ page import="java.util.*,java.net.*,java.io.*" %>
<%          
   Vector vSend = new Vector();        
   vSend.add("one");        
   vSend.add("two");        
   vSend.add("three");        
   String urlStr = request.getRequestURL().toString();        
   URL url = new URL(urlStr.substring(0,urlStr.lastIndexOf("/")+1) + "boomerang.jsp");        
   HttpURLConnection conn = (HttpURLConnection)url.openConnection();        
   conn.setDoOutput(true);        
   conn.setDoInput(true);        
   conn.setRequestMethod("POST");        
   conn.setRequestProperty("Content-type", "application/octet-stream");        
   ObjectOutputStream oos = new ObjectOutputStream(conn.getOutputStream());        
   oos.writeObject(vSend);          
   oos.flush();        
   ObjectInputStream in = new ObjectInputStream(conn.getInputStream());        
   Vector vBack = (Vector)in.readObject();        
   in.close();        
   oos.close();%>
   url=<%=url%><br/>
   vBack=<%=vBack%>
--------------------------------------------------------------
and  boomerang.jsp  
----------------------------------------------------------------
<%@ page import="java.net.*,java.io.*" %>
<%        
  ObjectInputStream ois = new ObjectInputStream(request.getInputStream());        
  ObjectOutputStream oos = new ObjectOutputStream(response.getOutputStream());        
  oos.writeObject(ois.readObject());          
  oos.flush();        
  ois.close();        
  oos.close();
%>
0
Independent Software Vendors: 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 27

Expert Comment

by:mrcoffee365
ID: 19885435
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
0
 
LVL 4

Expert Comment

by:evnafets
ID: 19887134
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.



0
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 19887160
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?
0
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 19887169
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.
0
 
LVL 4

Expert Comment

by:evnafets
ID: 19887262
> 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 :-)
0
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 19887588
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.
0
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 19887594
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
0
 
LVL 28

Expert Comment

by:rrz
ID: 19888505
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
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month16 days, 3 hours left to enroll

850 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