show HTTP is stateless by Sinatra
In tealeaf week 3, introduce to HTTP.
The most important thing is that, HTTP is stateless.
What’s the influnce by this property? Showing this by Sinatra.
first, follow Sinatra intro to run basic function:
then try this code:
1 | require 'sinatra' |
and this will show a button, click the button will invoke a post request back to set_name
in main.rb:
1 | <h4>hi~ <%= @name %> </h4> |
OK, then what will @name
show in home.erb after click button??
It shows Guest
, not tealeaf
. Why?
The key point is: when 2 redirect to 3, it’s a new request, instance variable like @name will be deleted after redirect, then create a new one.
That’s why @name still shows Guest
.
How about this way:
1 | require 'sinatra' |
Now using erb to embeded home.erb
into the same request(‘/set_name’). Then you’ll see @name
shows tealeaf
!!
This experience shows me how HTTP is stateless.
And how web developers make it like stateness is a long way to go.