• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Struts Session Question

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to have session data be instantiated when a page loads - as opposed to on a form action - what is the best way to do this?
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Create a preparatory action that forwards to your JSP and call the action from the URL rather than th JSP. Example:

URL:

http:myserver/myapp/preparePage1.do

This method allows you to put whatever you need in the Session prior to the JSP being displayed.
 
Picus Ameradeus
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah ah makes perfect sense, what about if I wanted to ensure that no matter what page the person landed on, that class would be called?
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The above scenario only works for a single JSP. If you want code that executes before any JSP or action is executed, you should look at writing a Servlet Filter. This is only available if your Application server supports J2EE 1.4 or above (Tomcat 5). See chapter 11 of the Sun J2EE Tutorial
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The session is normally maintained by session cookies. If you close your browser you lose the cookie. If you have disabled cookies on your machine then it also might not work.
In cases such as that you should be using the method response.encodeURL() to maintain the session for you in any hyperlinks you produce. Struts normally handles that for you though if necessary.

Hope this helps,
Bye.
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about using a custom request processor to solve your purpose? Override the processPreprocess which performs session handling and that can be used for all the requests.

Regards,
Ajay.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ajay's solution would work well at performing logic before any action is executed. However, it would not work at performing logic before a JSP is dispalyed. If the user enters a URL such as http://myserver/myapp/mypage.jsp, the request processor is never invoked.
 
Could you hold this puppy for a sec? I need to adjust this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic