Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Passing objects with attributes  RSS feed

 
Brian Coates
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having difficulty passing objects using request attributes. I'm trying to pass from a jsp sittting on a tomcat server, to a jsp within a completely different struts applicaton, sitting on another server.
a.jsp (sitting on the tomcat server)...
request.setAttribute( "object", object ) ;
<a href="/b.jsp">link</a>
b.jsp (in the webapp sitting on the other tomcat server)...
Object o = (Object)request.getAttribute( "object" ) ;
In b.jsp, the getAttribute() returns null.
Can I not do this?
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct, you can not do this.
There are two problems with the approach you have tried.
  • The request context is local to a single web application on a single server.
  • The request context only exists for the duration of a single request (ie, while a page is being built to reply to one URL from a browser, and any fragments included in that page)


  • Placing an attribute in the "request" context simply stores it in an in-memory map on the server which is processing the messages. That context is created when a request arrives at the servlet container, and is destroyed when the resulting page is sent to the browser. In your case, the request context and anything in it will be removed when the page containing your "<a href="/b.jsp">link</a>" is returned to the browser; long before the user can click on the generated link.
    If you want to send information from one machine to another, you have to use some form of network protocol. The easiest way, if you are already building an HTTP application, is usually to pass your information as HTTP request parameters in a GET or POST request.
     
    Brian Coates
    Ranch Hand
    Posts: 42
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Will using HTTP POST's and GET's work for objects? The object I'm trying to pass is a HashMap.
    Thanks..
     
    Frank Carver
    Sheriff
    Posts: 6920
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You can't send an object between machines without converting it to and from some "wire format". An in-memory object includes all sorts of memory addresses and other stuff that only make sense in the originating machine.
    What's in your HashMap? If it's just Strings, you could maybe use the serialization built in to the "Properties" class, or maybe pass each Map entry as a separate parameter.
    If you really have complicated objects stored in it you are going to need "industrial strength" serialization which can follow all the references to send all the objects referenced by any of the Map elements..
     
    Brian Coates
    Ranch Hand
    Posts: 42
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The map is a mapping of a String to another HashMap. That hashmap is a mapping of a String to a String.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!