• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Difference between Web Context and Session and ContextListener and SessionListener

 
Ranch Hand
Posts: 80
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone. I know for example that a session is a set of requests made from the same browser to the same webapp in a certain time and it allows to identify an user (it's necessary because of HTTP is stateless). But what I see is that both of them have similar behaviour. For example, I can hold data structures either in a session by httpSessionEvent.getSession().setAttribute() method or in a servletContextEvent.setAttribute() method.

1. Maybe the big difference that I've found so far is that WebContext is shared by all of the threads while a Session is unique by every thread of the servlet instance created by the container.

2. Another thing what I'm not really sure is that when the webapp is deployed by the servet the context is created in that moment, while a session is first created when a request comes to the webapp.

I'm really interested to see guys your point of view because I was looking in google and I didn't find any good answer to my doubt.

Cheers!.
 
Marshal
Posts: 25677
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, yeah, everything you said there is correct. If you want to store information which belongs to the whole application, you put in into the web context. If you want to store information which relates to a user's session, you put in into the session. And there's also the servlet context; if you want to store information which relates specifically to the servlet, you put into the servlet context.

Of course the three things have similar features because they are all for the purpose of storing data which arises at run time. I don't believe that should confuse you.

 
Saloon Keeper
Posts: 22265
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because you got diverted by the technical implementation and missed the abstract definitions.

Objects in a JEE webapp can be stored in one of 4 scopes: application scope, session scope, request scope, and page scope. Actually Page scope is an artefact of JSPs and JavaServer Faces adds some extra scopes, but they're based mostly off session scope.

Application scope is what's stored in the Map for web ApplicationContext. Session scope in the Map for HttpSession. Request scope is carried along with the current HttpServletRequest. Page scope is really just local variables in the servlet that gets compiled from a JSP. Technically, there's also a Global scope, which is based on static class properties, but in the JEE standard I don't think you'll find that one listed. It's kind of dangerous, since it means that you're sharing values between multiple webapp instances, and in theory every webapp is supposed to be self-contained.

You should know these scopes by their abstract names and not their specific implementations, since most of the higher-level frameworks use those names as storage and retrieval contexts rather than invoking the in raw Java. And in this context, even the lowly JSP is "higher level".
 
Pablo Napoli
Ranch Hand
Posts: 80
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys for your replies. They were very helpful for me because in the book from were I'm studying it's not so clear. On the other side, I first decided to learn JavaEE before learning Spring Framework because I think that in the long term is the best way to go through. Anyway I met guys who didn't know what's a Servlet and they were working with different Frameworks like Struts.

So just to show you that I really took into consideration your replies I want to share with you my summary of your replies:

- Objects in a JEE webapp can be stored in one of 4 scopes:
* Application scope:
* Application scope is what's stored in the Map for web ApplicationContext.

* If you want to store information which belongs to the whole application, you put in into the web context.

* Session scope:
* Session scope is what's stored in the Map for HttpSession.

* If you want to store information which relates to a user's session, you put in into the session.

* Request scope:
* Is carried along with the current HttpServletRequest.

* If you want to store information which relates specifically to the servlet, you put into the servlet context.

* Page scope (is an artefact of JSPs) : Is really just local variables in the servlet that gets compiled from a JSP

-> JavaServer Faces adds some extra scopes, but they're based mostly off session scope.

-> Of course the three things have similar features because they are all for the purpose of storing data which arises at run time.

Cheers!, Pablo.
 
I've been selected to go to the moon! All thanks to this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic