Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

How to get session in a common file

 
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 !!
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ??
 
Sheriff
Posts: 67618
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic