0

I make a few requests from a client-side of web application, which are received by servlet.

Here is a code piece in doPost method of my MainServlet:

String requestData = req.getReader().lines().collect(Collectors.joining());
HttpSession session = req.getSession(false);
if (session == null) {
    session = req.getSession(true);
}
System.out.println("Session's id: " + session.getId());
session.setAttribute("jsonData", requestData);

There is also a LoginServlet, which sets attributes to current session.

Then, I send a request again to MainServlet, but when the doPost method is invoked again, every time a new session is created.

As I understand, it should not work like this, since if a session already exists, it should be returned anyway.

I need to preserve a session created during very first doPost call. How can I do that?

EDIT: a js fetch API request:

const sendRequest = async (data) => {
        
await fetch('http://localhost:8080/', {
    method: 'POST',
    body: JSON.stringify(data),
    headers: {
         'Detail': 'requisites',
         'Content-type': 'application/json; charset=UTF-8',
         },
    })
    .catch((err) => console.log(err.message));
}
XterS
  • 11
  • 3
  • That's the client's responsiblity. This can among others happen when the client doesn't support cookies. As long as you don't tell anything about what program exactly the client is, a more tailored answer how to fix that cannot be given. – BalusC Jul 09 '23 at 14:21
  • @BalusC I have included a request into question, and by client cookies-support do you mean specifying it in fetch? Or is it about browser? – XterS Jul 09 '23 at 18:06
  • @BalusC Thank you very much! I looked more into fetch parameters and solved by adding `credentials` field to my request. – XterS Jul 09 '23 at 18:40

0 Answers0