2

I have searched a lot and found no suitable answers.

I have 2 post controller method as

@PostMapping("/saveStudentInfo")
public String saveStudentInfo(@RequestBody Students stud, HttpServletRequest request) {
    students.setId(stud.getId());
    students.setStudentName(stud.getStudentName());
    students.setSchoolInfo(stud.getSchoolInfo());
    
    System.out.println("Hello A= "+request.getSession(false).getId());
    
    return "Saved Sucessfully";
}
    
@PostMapping("/saveSubjectInfo")
public String saveSubjectInfo(@RequestBody Subjects sub, HttpServletRequest request) {
    subject.setSubjectName(sub.getSubjectName());
    subject.setSubjectTeacher(sub.getSubjectTeacher());
    System.out.println("Hello B= "+request.getSession(false).getId());
    
    return "Saved Sucessfully Subject";
}

Now from postman these calls, works successfully, as same JSessionId is generated

==>Problem

When called from react app via browser on 2nd

saveSubjectInfo request I get different JSessionId, as in New-Session was Created

enter image description here

And I have annotated both Students and Subjects as @SessionScoped.

==>Requirement

Need to maintain session, and for that JSessionId needed to be same.

John
  • 276
  • 1
  • 9
  • 29
  • Same discussion can be found here. https://stackoverflow.com/questions/2138245/session-is-lost-and-created-as-new-in-every-servlet-request – RAVI KUMAR SINGH Dec 28 '20 at 15:08
  • Here is another link to similar case, without accepted answer though: https://stackoverflow.com/questions/62802136/spring-react-and-sessions-how-to-keep-session – Roar S. Dec 28 '20 at 17:56
  • @RoarS. Yes this link was quite helpful thanks, but Now there is another problem. Now my JSESSIONID, never updates?? – John Dec 28 '20 at 18:13
  • @John: JSESSIONID should remain unchanged across requests in order to maintain session state, or am I interpreting the case here wrong? – Roar S. Dec 28 '20 at 18:18
  • What I need is, for each user it should be unique. 2 Request from same user from 2 different browser should generate different JSessionId – John Dec 28 '20 at 18:18
  • @John: Yes, of course. Are you getting identical JSESSIONID when testing with two different browsers simultanously, e.g. Edge and Chrome? – Roar S. Dec 28 '20 at 18:20
  • Yes Same JSessionId – John Dec 28 '20 at 18:20
  • Ohh My Bad Sorry, Now JSessionId is unique for different browser request, but same for same browser different tab request. Can this be diffferent for same browser different tab request – John Dec 28 '20 at 18:28
  • 1
    @John: I believe you have some issues with your architecture. Can you redesign your solution in a way where state is maintained by clients only? I can't see how to keep different sessions across tabs in browser unless you are running e.g. Chrome in inognito mode. BR – Roar S. Dec 29 '20 at 14:27

1 Answers1

0

By default browsers support the GET method when API is hit directly from the browser URL bar. Also the POST and PUT method are usually supposed to make request along with some data to the serving method as the body so that it is not viewed in the browser request. For POST or PUT Postman app can be used.

Saket Kumar
  • 113
  • 1
  • 9
  • Please try to understand the problem, read it once more. I am sorry if its not clear. – John Dec 28 '20 at 14:59
  • Yes you are correct. Actually what happens is if there is no session then request.getSession(false) returns null if there is no session. In this case you need to set the session maually.Use the following code snippet HttpsSession session = request.getSeaaion(false); if (session != null) { session.setAttribute("abc", request.getParameter("abc")); } – Saket Kumar Dec 28 '20 at 15:14
  • It is not the problem, main issue is with session maintenance in spring boot using @SessionScope – John Dec 28 '20 at 16:26