Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Struts Session Scope Listener

 
Jayaraj Jaganathan
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added Session Attribute Listener in web.xml to check session attributes added,removed and replaced function.

while listening
my struts form bean is added to session without any user entered data, session replaced also not called.

i don't know why this strange thing happening ?

 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe struts adds the form bean to the session before it transfers user data to the form bean.

session replaced also not called


Why do you want session replaced to be called. Did you call the same action twice so that the form bean is added to the session for the second time and replace method will be called??
 
Jayaraj Jaganathan
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:Maybe struts adds the form bean to the session before it transfers user data to the form bean.

session replaced also not called


Why do you want session replaced to be called. Did you call the same action twice so that the form bean is added to the session for the second time and replace method will be called??


form data is added to session at http://www.docjar.com/html/api/org/apache/struts/action/RequestProcessor.java.html line number 202,

form data in session is retived at http://www.docjar.com/html/api/org/apache/struts/util/RequestUtils.java.html line number 196

NOTE: empty form object stored in session, while retrieving we get form object with user entered values.

HOW is happening ? how the session data is replaced ?
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jayaraj Jaganathan wrote:while listening
my struts form bean is added to session without any user entered data, session replaced also not called.

This happens because you declared the action form in "session" scpe in action mapping.
See you struts-config.xml file for the same action, you'll find something like this:



If this is how your struts-config looks like, then struts created one object of your action form in given scope(session) when you submit the FORM related with that action(here it is "SomeAction).

HTH

[Edited my mistake]
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jayaraj you are right. Parameters are transferred to the form bean before it is stored into the session (or request) scope. Did you check the action to see if the parameters are actually transferred to the form bean?? Maybe this is what you mean by this but I just want to confirm

NOTE: empty form object stored in session, while retrieving we get form object with user entered values.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I need one information to make sure my above last paragraph:

When do you get session scope object of the form (bean) ?
before the action is submitted OR when the page which have that FORM is called..
 
Jayaraj Jaganathan
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sagar Rohankar wrote:
Jayaraj Jaganathan wrote:while listening
my struts form bean is added to session without any user entered data, session replaced also not called.

This happens because you declared the action form in "session" scpe in action mapping.
See you struts-config.xml file for the same action, you'll find something like this:



If this is how your struts-config looks like, then struts created one object of your action form in given scope(session) when you access the FORM related with that action(here it is "SomeAction).

HTH






<form-bean name="GroupForm" type="org.apache.struts.action.DynaActionForm" >
<form-property name="group_id" type="java.lang.String"/>
</form-bean>

<action path="/show_filter"
type="org.apache.struts.actions.ForwardAction"
name="GroupForm" scope="session"
input="/jsp/GroupDetails.jsp"
parameter="/filter.jsp" />


<form method="POST" action="/score/show_filter.do">
<input type="hidden" name="group_id" value="243">
<input type="submit">
<form>

public class SessionPersistenceListener implements HttpSessionAttributeListener{

private void show(String message, HttpSessionBindingEvent event) {
System.out.println(message + ">" + event.getName() + "==" + event.getValue());
}

public void attributeAdded(HttpSessionBindingEvent event) {
show("ADDED", event);
}

public void attributeRemoved(HttpSessionBindingEvent event) {
show("REMOVED", event);
}

public void attributeReplaced(HttpSessionBindingEvent event) {
show("REPLACED", event);
}
}

when i submit my page . i can see the sop from listener that session added and in my sop i see that group_id (243) is not in form
then when
i get the session attribute in filter.jsp . i get the group_id 243

how ?
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jayaraj Jaganathan wrote:when i submit my page . i can see the sop from listener that session added and in my sop i see that group_id (243) is not in form
then when
i get the session attribute in filter.jsp . i get the group_id 243

how ?

First thing, it took me 5 minute to configure what "sop" means ? which is bad thing, don't make us think !!

Now, you want to see whether the form properties has been initialized with the form values in session listener, is someting, I suppose is impossible, because session is created, but not initialized,
This is what ActionForm JavaDocs says

An ActionForm is a JavaBean optionally associated with one or more ActionMappings. Such a bean will have had its properties initialized from the corresponding request parameters before the corresponding Action.execute method is called..

So its the request processor who assign FORM values to the bean property.
 
Jayaraj Jaganathan
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sagar Rohankar wrote:
Jayaraj Jaganathan wrote:when i submit my page . i can see the sop from listener that session added and in my sop i see that group_id (243) is not in form
then when
i get the session attribute in filter.jsp . i get the group_id 243

how ?

First thing, it took me 5 minute to configure what "sop" means ? which is bad thing, don't make us think !!

Now, you want to see whether the form properties has been initialized with the form values in session listener, is someting, I suppose is impossible, because session is created, but not initialized,
This is what ActionForm JavaDocs says

An ActionForm is a JavaBean optionally associated with one or more ActionMappings. Such a bean will have had its properties initialized from the corresponding request parameters before the corresponding Action.execute method is called..

So its the request processor who assign FORM values to the bean property.


i am sorry sop means System.out.println

i get output like this
[8/7/09 18:44:20:385 IST] 0000002d SystemOut O ADDED>GroupForm==DynaActionForm[dynaClass=GroupForm,group_id=]

but in filter.jsp i get 243 as output

how ? i am too confused ?
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't reply by qouting out whole reply, you can anytime quote a part of reply or simply "reply", which look nice and better.

Jayaraj Jaganathan wrote:i get output like this
[8/7/09 18:44:20:385 IST] 0000002d SystemOut O ADDED>GroupForm==DynaActionForm[dynaClass=GroupForm,group_id=]

This ADDED shows, that in session, object of Action Form(bean) is get added, like something this happens when you submit the form


Jayaraj Jaganathan wrote:
but in filter.jsp i get 243 as output

Because when the Action redirect to the page "filter.jsp", the "Request Processor" has initialized the form instance with the HTML FORM values, i.e with 243

The bottom line will be:
First session scope form object gets created(hence called attributeAdded() method) and then initialized it with values, before passing the form to Action#execute().
 
Jayaraj Jaganathan
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because when the Action redirect to the page "filter.jsp", the "Request Processor" has initialized the form instance with the HTML FORM values, i.e with 243


you are right

DynaActionForm _grpFrm = new DynaActionForm()
session.setAttribute(_grpFrm);

initialized the form instance with the HTML FORM values, i.e with 243


aging a setAttribute must be called to put the object into session

NOTE: without setting the initialized values in session. we get the initialized value in filter.jsp

<%@ page import="org.apache.struts.action.DynaActionForm"%>


<%DynaActionForm df = (DynaActionForm)session.getAttribute("GroupForm");%>
S:<%=df.get("group_id")%>


?
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jayaraj, the form bean object will not be stored in the session again. The session stores reference to attributes so if you make changes to the attribute object after it is added to the session, the object in the session will reflect the changes...
 
Jayaraj Jaganathan
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:Jayaraj, the form bean object will not be stored in the session again. The session stores reference to attributes so if you make changes to the attribute object after it is added to the session, the object in the session will reflect the changes...


you mean to say for example



StringBuffer sbf= new StringBuffer("DEFAULT");
session.setAttribute("NAME",sbf);
sbf.append("NEW");
StringBuffer sbf_new = (StringBuffer )session.getAttribute("NAME");
System.out.println(sbf_new.toString());


i get output DEFAULT only.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jayaraj Jaganathan wrote:
initialized the form instance with the HTML FORM values, i.e with 243


aging a setAttribute must be called to put the object into session

Wrong. It listens to change of object added, removed from session, but not the the objects instance variable's value change.
 
Jayaraj Jaganathan
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

StringBuffer sbf= new StringBuffer("DEFAULT");
session.setAttribute("NAME",sbf);
sbf.append("NEW");
StringBuffer sbf_new = (StringBuffer )session.getAttribute("NAME");
System.out.println(sbf_new.toString());


i am sorry i have given a wrong information . i get DEFAULTNEW as my output.

let me explain about my task :
1.my aim is use files in [HDD] instead of session attribute in [RAM].
2.to do this i use request wrapper and session wrapper.
3.when a attribute is added . it create a file and store the object inside the file using ObjectOutputStream.
4.when a attribute is retrived. it read the data from the file using ObjectInputStream.
5.my ultimate aim is to escape from OutOfMemoryError by doing this.

now:
i am unable to proceed with the above approach because of session mechanism used in struts

please help me
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jayaraj Jaganathan wrote:
3.when a attribute is added . it create a file and store the object inside the file using ObjectOutputStream.
4.when a attribute is retrived. it read the data from the file using ObjectInputStream.

What is "attribute" here? If you want to say a bean property, then you can call ObjectOutputStreamin setter and while retrieving file using ObjectInputStreamyou can use getter method.
Jayaraj Jaganathan wrote:
5.my ultimate aim is to escape from OutOfMemoryError by doing this.

OutOfMemory generally the result of unclosed stream, its not the problem unless you have file size in MB.
 
Jayaraj Jaganathan
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What is "attribute" here? If you want to say a bean property, then you can call ObjectOutputStreamin setter and while retrieving file using ObjectInputStreamyou can use getter method.


attribute means session attribute


OutOfMemory generally the result of unclosed stream, its not the problem unless you have file size in MB.


think about if there are million user accessing my web application. then session value [RAM] MB goes to GB .
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. If its session attribute, then you have session attribute listener, like

2. And those million users are closing down the files too, OK, I'm not forcing to use memory for files, you can decide what is good for your application.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic