Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST and HttpSession object

I know that REST is not supposed to use HttpSession. From the other side, the REST service is running within a servlet container. From what I saw, the HttpSession object will be created only when:

HttpSession session = request.getSession(); 

code is executed. Is it always the case? Besides using JSP?


My question is: will be HttpSession objects be created when the REST method is executed or not?

Let's say I use the JAX-RS framework, if it can make any difference.
If such objects are not created, it actually can mean that the size of the server memory may not grow irrespective of how many clients use it the server.

like image 276
Cosigin Avatar asked Nov 04 '22 08:11

Cosigin


1 Answers

HTTP sessions are actually used quite often with REST interfaces, but should never contain anything truly critical. Thus, they can be used to contain the fact that you've authenticated or what your preferred default ordering of some list is; in the former case, you could also support other authentication mechanisms at the same time allowing fully stateless operation, and in the latter you can easily also support explicit overrides. So long as you don't require a session — well, under the assumption that your site was using HTTP BASIC auth for the sake of argument; if you're using OAuth then you need sessions enabled to stop performance from being crippled — then you're still potentially reasonably close to RESTful (in this area for sure; REST is not “don't use sessions” after all).

Is there a concern about how long a session lasts before timing out? Well, maybe but not really. A session is really an object that you've mapped into some database table, and you can configure the expiry policy on them so that they last long enough to support effective use without being over-burdensome. Which depends on how many clients use the site at once, what their usage patterns are, and what hardware resources you've got available (of course).

like image 116
Donal Fellows Avatar answered Nov 09 '22 15:11

Donal Fellows