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

lifetime of pageContext?  RSS feed

 
Neeraj Macker
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Integer count = new Integer("0");
pageContext.setAttribute("COUNT", count, PageContext.SESSION_SCOPE);

this is what i do in a JSP. in other JSPs i get this attribute. everything works fine. but yesterday i left my web application running. in the morning when i came and worked through some pages of the web applications, it gave null pointer exception for the code that gets this "COUNT" object from the pageContext. Why? please help, as i need to let my application run over months together.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66203
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You did not place your variable in page context. You used the page context to place your variable in the session context. The session times out after a period set for it in the server configuration.
 
Alicia Logan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use this
pageContext.setAttribute("count", count, pageContext.APPLICATION_SCOPE)
 
Neeraj Macker
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i fail to understand this SESSION_SCOPE thing. i made a test jsp which had NO HttpSession. i just added a string to a pageContext.SESSION_SCOPE and then read it. i did get back the value. i dont understand from where does the pageContext gets the http session? does the container create a http session implicitly?and where can i set the maxinactiveinterval setting for this jsp session. in the <session-timeout> in web.xml. please explain this session thing.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66203
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
does the container create a http session implicitly?


Yes.
 
Deepa Korecherla
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
setMaxinactiveinter..() & session timeout both are for sessions only. But if the former is used in the code and the later in the deplyment descp. To neverr end the session use negative or 0 as the value.
Note: the former taks in secs the later in mins
If this helped ..do reply
 
Neeraj Macker
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Deepa and Bear. i opened the _jsp.java file in tomcat work directory and found the method used by tomcat servlet container to create a new PageContext. it creates a session for every page context. and hence i now understand. thanks a lot.
 
Neeraj Macker
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one more question:

i have set the max inactive interval in DD to -1 i.e. infinite. But what i actually want is that the session exists as long as the IE exists and the session invalidates when i close the IE. but this does not happen. what should i do?is there any other option?
 
Neeraj Macker
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
someone please reply.
 
Muhammad Imad Qureshi
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you can write a javascript function and then in an scriplet you can write something like onUnload <% session.invalidate() %>

I am not sure if this is the correct syntax but I think this is one of the solutions. hope this helps
 
Deepa Korecherla
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This mite be the very crude solution...try this
when u close the window (in the window close method) take the time ( in mins...diff. of the initial time & window close time) and put that mins number in the setmaxinactive..(mins). This may not be the sol. I'm sure there is another easier sol.
do reply ..what u've done
 
Deepa Korecherla
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Qureshi is right...my idea of taking time in mins may not work
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66203
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you can write a javascript function and then in an scriplet you can write something like onUnload <% session.invalidate() %>


This will not work. All Java is executed on the server prior to the page being sent to the browser. Emedding a scriptlet in a Javascript function will not cause that scriptlet to execute when the Javascript function is triggered.
 
Neeraj Macker
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:


This will not work. All Java is executed on the server prior to the page being sent to the browser. Emedding a scriptlet in a Javascript function will not cause that scriptlet to execute when the Javascript function is triggered.


please suggest a solution...
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!