aspose file tools*
The moose likes Servlets and the fly likes Inter servlet communication by means of session Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Inter servlet communication by means of session" Watch "Inter servlet communication by means of session" New topic
Author

Inter servlet communication by means of session

Srikanth Adapa
Ranch Hand

Joined: Aug 21, 2008
Posts: 56
Hi all,

I want to know how the servlet is communicating each other by means of session, jus like me able to discuss with you through this forum


I am aware of the Servlet context object. But I want to know if there is any possibility to carry the contents of one form to another by means of servlet keeping track of session.

Awaiting for your replies..
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


But I want to know if there is any possibility to carry the contents of one form to another by means of servlet keeping track of session

Yes. Anythign you put in the session can be recovered from there, assuming the session has not become invalid.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Srikanth Adapa
Ranch Hand

Joined: Aug 21, 2008
Posts: 56
thank you..

Please let me know, if the class, TestSession can be Serialized, if it have some attributes as follows:

public class TestSession implements Serializable {

public Map mpKeyValue = new HashMap();
private HttpServletRequest request;
private HttpServletResponse response;
private HttpSession session;

..........
..........
..........

}
Here HttpServletRequest, HttpServletResponse are not Serialized !!
But is there any chance to make the class Serialized? So that I want to carry some other attributes through session.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42608
    
  65
It makes no sense to serialize references to request, response or session. What are you trying to do?


Ping & DNS - my free Android networking tools app
Srikanth Adapa
Ranch Hand

Joined: Aug 21, 2008
Posts: 56
Hi ,

I am trying to cluster an application in jboss, for which I am getting a NotSerializable exception as follows:


java.io.NotSerializableException: nextapp.echo2.webrender.Connection
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1527)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1492)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1410)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1168)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1527)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1492)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1410)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1168)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:344)
at java.util.concurrent.ConcurrentHashMap.writeObject(ConcurrentHashMap.java:1284)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:623)
at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:963)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1479)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1410)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1168)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:344)
at org.jboss.web.tomcat.service.session.SessionBasedClusteredSession.writeExternal(SessionBasedClusteredSession.java:175)
at org.jboss.web.tomcat.service.session.JBossCacheService.externalizeSession(JBossCacheService.java:1027)
at org.jboss.web.tomcat.service.session.JBossCacheService.putSession(JBossCacheService.java:316)
at org.jboss.web.tomcat.service.session.JBossCacheClusteredSession.processSessionRepl(JBossCacheClusteredSession.java:121)
at org.jboss.web.tomcat.service.session.JBossCacheManager.processSessionRepl(JBossCacheManager.java:1097)
at org.jboss.web.tomcat.service.session.JBossCacheManager.storeSession(JBossCacheManager.java:652)
at org.jboss.web.tomcat.service.session.InstantSnapshotManager.snapshot(InstantSnapshotManager.java:49)
at org.jboss.web.tomcat.service.session.ClusteredSessionValve.invoke(ClusteredSessionValve.java:98)
at org.jboss.web.tomcat.service.session.JvmRouteValve.invoke(JvmRouteValve.java:84)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
at org.apache.coyote.ajp.AjpProcessor.process(AjpProcessor.java:437)
at org.apache.coyote.ajp.AjpProtocol$AjpConnectionHandler.process(AjpProtocol.java:366)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
at java.lang.Thread.run(Thread.java:675)


I am unable to understand why am I getting this exception stack trace.
Please respond me if you have any idea about this.


Thank you
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42608
    
  65
The error message is very specific: the object is not serializable. Only serializable objects can be put in an HttpSession.

What kind of object is Connection? If it's related to I/O or network, then it most likely doesn't make sense to serialize it.
Srikanth Adapa
Ranch Hand

Joined: Aug 21, 2008
Posts: 56
Hi Ulf Dittmer!!
Thank you for your suggestion.

Basically, I am using the Echo2 jars in my applications, of which the Connection class belongs to those. I have modified that Connection class making it to implement Serializable interface. After replacing that jar, Now, I got a new stacktrace pointing to RequestFacade.java



11:22:51,362 ERROR [JBossCacheService] externalizeSession(): exception occurred externalizing session SessionBasedClusteredSession[id: jFdwj9uBb2SnaFbZjiTSaQ**.node1 lastAccessedTime: 1219729960683 version: 17 lastOutdated: 0]
java.io.NotSerializableException: org.apache.catalina.connector.RequestFacade
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1156)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1509)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1474)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1392)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1150)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1509)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1474)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1392)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1150)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1509)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1474)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1392)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1150)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:326)
at java.util.concurrent.ConcurrentHashMap.writeObject(ConcurrentHashMap.java:1247)
at sun.reflect.GeneratedMethodAccessor96.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:945)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1461)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1392)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1150)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:326)
at org.jboss.web.tomcat.service.session.SessionBasedClusteredSession.writeExternal(SessionBasedClusteredSession.java:175)
at org.jboss.web.tomcat.service.session.JBossCacheService.externalizeSession(JBossCacheService.java:1027)
at org.jboss.web.tomcat.service.session.JBossCacheService.putSession(JBossCacheService.java:316)
at org.jboss.web.tomcat.service.session.JBossCacheClusteredSession.processSessionRepl(JBossCacheClusteredSession.java:121)
at org.jboss.web.tomcat.service.session.JBossCacheManager.processSessionRepl(JBossCacheManager.java:1097)
at org.jboss.web.tomcat.service.session.JBossCacheManager.storeSession(JBossCacheManager.java:652)
at org.jboss.web.tomcat.service.session.InstantSnapshotManager.snapshot(InstantSnapshotManager.java:49)
at org.jboss.web.tomcat.service.session.ClusteredSessionValve.invoke(ClusteredSessionValve.java:98)
at org.jboss.web.tomcat.service.session.JvmRouteValve.invoke(JvmRouteValve.java:84)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
at org.apache.coyote.ajp.AjpProcessor.process(AjpProcessor.java:437)
at org.apache.coyote.ajp.AjpProtocol$AjpConnectionHandler.process(AjpProtocol.java:366)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
at java.lang.Thread.run(Thread.java:619)






And I also found that "org.apache.catalina.connector.RequestFacade" is found at "/jboss-4.2.2.GA-src/thirdparty/jboss/web/lib/jbossweb-src/org/apache/catalina/connector" package. But I am not finding way how to avoid this exception.


Please let me know if any of you have ever tried with clustering jboss!! I hope many of you actively participate in this discussion.


Thank you!!
Sri.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42608
    
  65
(Kind of a continuation for this duplicate thread.)

I'll say it again: It doesn't make sense to put request, response or session references into a session (and neither does it make sense to use AWT/Swing objects on the server, but I assume that's just testing on your part).

Everything in a session needs to be serializable, and a class does not become serializable by adding "implements Serializable" to it - that just asserts that it is serializable. All of its instance variables need to be serializable as well (or be declared to be transient).
Srikanth Adapa
Ranch Hand

Joined: Aug 21, 2008
Posts: 56
Thank you !
I will make sure not to post as duplicate threads.

Please give me a bit clarity that, The class containing HttpServletRequest, HttpServletResponse, HttpSession must not be kept as attributes, while making the class Serializable. Was it the reason that all those interfaces are not Serialized?(As I saw in the source). Any way, I have tried once again to cluster the application by keeping all those 3 objects as transient. But again I got the similar stack trace.

java.io.NotSerializableException: org.apache.catalina.connector.RequestFacade
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1156)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1509)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1474)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1392)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1150)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1509)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1474)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1392)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1150)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1509)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1474)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1392)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1150)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:326)
at java.util.concurrent.ConcurrentHashMap.writeObject(ConcurrentHashMap.java:1247)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:945)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1461)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1392)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1150)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:326)
at org.jboss.web.tomcat.service.session.SessionBasedClusteredSession.writeExternal(SessionBasedClusteredSession.java:175)
at org.jboss.web.tomcat.service.session.JBossCacheService.externalizeSession(JBossCacheService.java:1027)
at org.jboss.web.tomcat.service.session.JBossCacheService.putSession(JBossCacheService.java:316)
at org.jboss.web.tomcat.service.session.JBossCacheClusteredSession.processSessionRepl(JBossCacheClusteredSession.java:121)
at org.jboss.web.tomcat.service.session.JBossCacheManager.processSessionRepl(JBossCacheManager.java:1097)
at org.jboss.web.tomcat.service.session.JBossCacheManager.storeSession(JBossCacheManager.java:652)
at org.jboss.web.tomcat.service.session.InstantSnapshotManager.snapshot(InstantSnapshotManager.java:49)
at org.jboss.web.tomcat.service.session.ClusteredSessionValve.invoke(ClusteredSessionValve.java:98)
at org.jboss.web.tomcat.service.session.JvmRouteValve.invoke(JvmRouteValve.java:84)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
at java.lang.Thread.run(Thread.java:619)



Can you suggest me another alternative for this??


Thank you in advance.

Sri
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14345
    
  22

NotSerializableException: This error message indicates that you have some object in your session (maybe inside the map 'mpKeyValue') that is not serializable.

Carefully check what objects are put in your session object and what objects are linked to it (directly or indirectly).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42608
    
  65
Was it the reason that all those interfaces are not Serialized

Both HTTP request and response are short-lived things - they relate to the current HTTP request. As soon as the request is serviced, both request and response objects become obsolete, because the request to which they pertain no longer exists.

It makes no sense to keep references to request, response or session in an instance variable (which is shared across different threads and users). So instead of making those variables transient, I'd think about why you have those variables in the first place. Whatever they're used for is most likely not going to work.
[ August 26, 2008: Message edited by: Ulf Dittmer ]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12823
    
    5
The class containing HttpServletRequest, HttpServletResponse, HttpSession must not be kept as attributes,


You should NEVER try to keep references to requests, response or sessions longer than a single request-response cycle. These are entities managed by the servlet container and may be objects that are recycled by the container. Hanging on to a reference could result in very odd and hard to reproduce errors.

There are plenty of ways to put the relevant data in a separate (and Serializable) POJO.

Bill
Srikanth Adapa
Ranch Hand

Joined: Aug 21, 2008
Posts: 56
Fine!!

I ve modifie the code as per the suggestions. Now, it gave a problem at the actionPerformed(). Using like this might be like using the field session. But how to check it is set in every branch of the if-else statement? Suggestions are very welcome to be modified in code:





Here is the following exception stack trace, when trying to cluster:

java.lang.NullPointerException
at com.casper.web.SessionTest$CustomAL.actionPerformed(SessionTestServlet.java:139)
at nextapp.echo2.app.button.AbstractButton.fireActionPerformed(AbstractButton.java:135)
at nextapp.echo2.app.button.AbstractButton$1.actionPerformed(AbstractButton.java:100)
at nextapp.echo2.app.button.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:70)
at nextapp.echo2.app.button.DefaultButtonModel.doAction(DefaultButtonModel.java:58)
at nextapp.echo2.app.button.AbstractButton.doAction(AbstractButton.java:121)
at nextapp.echo2.app.button.AbstractButton.processInput(AbstractButton.java:506)
at nextapp.echo2.app.update.ClientUpdateManager.process(ClientUpdateManager.java:116)
at nextapp.echo2.app.update.UpdateManager.processClientUpdates(UpdateManager.java:89)
at nextapp.echo2.webcontainer.ContainerSynchronizeService.renderUpdate(ContainerSynchronizeService.java:472)
at nextapp.echo2.webrender.service.SynchronizeService.service(SynchronizeService.java:279)
at nextapp.echo2.webrender.WebRenderServlet.process(WebRenderServlet.java:273)
at nextapp.echo2.webrender.WebRenderServlet.doPost(WebRenderServlet.java:189)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
at org.jboss.web.tomcat.service.session.ClusteredSessionValve.invoke(ClusteredSessionValve.java:87)
at org.jboss.web.tomcat.service.session.JvmRouteValve.invoke(JvmRouteValve.java:84)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
at org.apache.coyote.ajp.AjpProcessor.process(AjpProcessor.java:437)
at org.apache.coyote.ajp.AjpProtocol$AjpConnectionHandler.process(AjpProtocol.java:366)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
at java.lang.Thread.run(Thread.java:619)



Thank you in advance.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42608
    
  65
You completely lost me. What are GUI components doing in a web app? What are you trying to do?
Srikanth Adapa
Ranch Hand

Joined: Aug 21, 2008
Posts: 56
Hi !!
I am following the echo framework for my application, which provides a very good UI along with the client-server environment(web related too). Please let me send you my complete program:




I ve written the code with reference to the below link:

http://echo.nextapp.com/site/node/32
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42608
    
  65
I see; those aren't AWT/Swing components but classes of this framework.

As has been pointed out repeatedly, you can't put session, request or response object into a session, nor would that make sense. But you still haven't said what you're trying to accomplish by doing so. If you did that we might be able to suggest alternative solutions.
Srikanth Adapa
Ranch Hand

Joined: Aug 21, 2008
Posts: 56
thank you all,

I could finally cluster that sample testcase successfully. As I am on vacation, I could not be in touch with you all..
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
Could you please let us know, what was the problem and how did you resolve it?


Thanks and Regards
Srikanth Adapa
Ranch Hand

Joined: Aug 21, 2008
Posts: 56
I dint understand what exactly do you mean!! Are you working out with clustering support and want to know or like to discuss the current topic???
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
Originally posted by Srikanth Adapa:
I dint understand what exactly do you mean!! Are you working out with clustering support and want to know or like to discuss the current topic???


I wanted to know, how did you resolve the problem you had mentioned under the current topic.
Srikanth Adapa
Ranch Hand

Joined: Aug 21, 2008
Posts: 56
Just by avoiding the Http related objects with in the session. The session must be free of the objects which are not Serialized internally. Just by keeping this in mind, I ve set all the objects being carried by sesion to be Serialized..



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inter servlet communication by means of session