• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get session in a common file

 
vanlalhmangaiha khiangte
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have a common file which can be accessed from many other files.
I want to store the session here how can i do this?
After I get the session ,I will get the id by using session.getSessionID()
No request is passed to this file from any file


Thanking you in advance !!
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what you're asking.

What do you mean a common file?
What do you mean by "Store Session"?

You don't store a session.
Sessions are created on your behalf by the container.
You can then access it, bind attributes, and retrieve attributes from it.

If you can explain in a little more detail, what you're trying to do, someone should be able to give you a suggestion.
 
vanlalhmangaiha khiangte
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean a common file?


I will have a common java file which can be accessed at any point from the different files.I will do some operations here.
Lets say this file name is Common.java


Sessions are created on your behalf by the container.
You can then access it, bind attributes, and retrieve attributes from it.


This is exactly what is needed.
I do not know how to get the session in my utility file ? i.e Common.java
In my other files , we can get the session from
HttpSession session = request.getSession();
where request is HttpServletRequest.
Since i am not passing any request to my file Common.java so thats why i cannot get the session in this file .
Is there ways to get the session here ??
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a poor idea to pass resources like the request and the session around. I'd suggest extracting the data you need, and pass the data as parameters to the "common" classes.
 
vanlalhmangaiha khiangte
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I need the session ID to create a URL connection in the common file which will do some operations ...
Is there any way to get the sessionId in this util file ?
 
suri jagadish
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The first thing you need to do is the make the method that will prepare the URL static so that your servlet can call that method and pass the sessionID as parameter to that method and that will return the URL.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think we have great confusion over the use of the word "file."

I will have a common java file which can be accessed at any point from the different files.I will do some operations here.
Lets say this file name is Common.java


You appear to be talking about a Java class. Calling it a file is just confusing. As suri suggests, you might use static methods and variables in this class which could be accessed by other servlets in the same context.

Bill
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic