How to make a java web application fully stateless

2019-01-29 01:56发布

问题:

This question already has an answer here:

  • How do I make my Web Application stateless yet still do something useful? 5 answers

I know that there are many discussions on difference between stateful app and stateless app, and that stateless is what function programming language does, every function call with the same args will return the same value.

Does it mean object-oriented language is not able to make a fully stateless application,since every object will typically have state.

Also, in Java web application, we typically need to trace user state,and which is solved by session. But how to do that in distributed system with in stateless way?

When one system dies, and we need the session can be recognized in some way by another server so that the user state will be transferred. Do we need to put the session in the central database(cache)?But is this way stateless?

What will be some things need to be concerned except session) to before we make a java web application stateless?

回答1:

You don't store any client state information on the server, you pass it around to everywhere that needs it and you pass it back to the client when the server needs something changed. That is what stateless means, no state. The server should not have any clue about what state the client is in until the client tells it what the state is.



回答2:

The most simple way is to store the session information in some key-value store. Have a look here: Tomcat: Store session in database

To go "real" stateless, there must be NO session at all. This means: The user will authenticate with every request, and with every request you've got to check the credentials. (It's pretty similar to a session database)