63357

Can't get the session in java servlet

Question:

I am using servlets for the first time but I made a lot of progress. My servlets are working well. So I decided to put an authentication mechanism, which creates a session, if users give the right password and id's. But sessions are totally new for me. So I don't quite follow the logic but I have started to understand.

As I mentioned before one of my servlets is dedicated for logging in. If password is correct a session is created (I don't store any object/data in sessions) and client (remoteUser) is notified that the password is accepted and session is created. What client does is to reach any other servlet in the same application. Other servlets get the session to check if it is created and valid (not timed out). For that purpose in those other servlets I get the session with:

HttpSession session = req.getSession(false); //false because this is not the place to create a session. sessions should only be created in the login servlet.

But this returns a null. So I have tried:

HttpSession session = req.getSession();

And checked with session.isNew(); and I it was a new session. So the session I have created in login servlet can't be called with req.getSession(); in another servlet.

PS: When session is created in login servlet: session.setMaxInactiveInterval(300); //5 minutes

Thanks a lot for any response!

Answer1:

When using Google App Engine, you have to specifically enable session support. See <a href="http://code.google.com/appengine/docs/java/config/appconfig.html#Enabling_Sessions" rel="nofollow">http://code.google.com/appengine/docs/java/config/appconfig.html#Enabling_Sessions</a>.

Recommend

  • Can't get mongodb record by ObjectId in golang
  • Run code before any django management command
  • Haskell Random Generator … how to make easier to use?
  • Profiling memory leaks with Instruments- huge difference between iPhone 4 and iOS 5 Simulator
  • How to remove space/TAB from command output
  • Matlab structure in Python [duplicate]
  • Ruby: Write to CSV if condition met
  • AngularJs trigger a file download by calling a REST endpoint
  • NextJS auth with an external server
  • .Net how to set IsReadOnly for a property for a usercontrol
  • Find a directory using wildcard in Inno Setup
  • Delete get variables and .php using htaccess
  • How to upload an Image onto localhost server whilst path is stored in mysql database?
  • Advertising Identifier for devices lower than iOS 6.0
  • Create a Terminal-Based Bluetooth Monitor in XCode?
  • How to make infinite threaded comments
  • no endpoints available for service \\“kubernetes-dashboard\\”
  • Python C binding error
  • jqGrid Filter Toolbar and Date and Select filters
  • Get last 15 lines from a large file in SFTP with phpseclib [duplicate]
  • Eric5: The OK button of 'new project' dialog is disable
  • jQuery - Prevent activation on first click
  • Shrinking Bootstrap Navbar with logo on scroll
  • Python ctypes: Prototype with LPCSTR [out] parameter
  • How to modify the way a ForeignKey field is rendered in a Django admin page to avoid browser crash?
  • Issues with converting data into a matrix after running lapply()
  • C++11: Why rvalue reference parameter implicitly converted to lvalue
  • content must have a ListView whose id attribute is 'android.R.id.list'
  • Building JavaFX 2.0 App on Mac, deploying on Windows
  • Request Access Token in Postman for Azure Function App protected by Azure AD B2C
  • Wireshark Display Filter for Unique Source/Destination IP and Protocol
  • Ember.js + JQuery-UI Tooltip - Tooltip does not reflect the model / controller changes
  • Separating definition/instantiation of template classes without 'extern'
  • openpyxl - adding new rows in excel file with merged cell existing
  • How can I ssh into a server that requires 2 password authentication using python's paramiko mod
  • How to specify generic type when the type is only known at runtime?
  • Change cell value based on cell color in google spreadsheet
  • matrix multiplication apache pig
  • Unity3d lost directional light shadows after generate assetBundle (.unity3d file)
  • Codeigniniter insert data through models and controller