Session management for http requests of applets and session management for applets
Session management for http requests of applets
As a Java Web application development programmer, users like to put the user information (such as user ID and User Name) After user logon into the session for saving, then, when user information is required in the development of business logic, it is easy and convenient to get the value from the session. Recently, when developing a applet, I encountered a problem that the sessionid will be changed every time the applet requests. This will cause the session user information to be stored during logon Failure in subsequent requests, in fact, this problem is also encountered when the front-end is separated and developed. The back-end programmers are responsible for server development and provide interface programs. The front-end programmers are responsible for client development and call the interface programs provided by the back-end programmers to obtain data, at this time, each interface program requested by the front-end programmer also changes the sessionid, because some cannot obtain the user information stored in the session during logon.
The common session persistence method is that when a browser initiates an http request to the server, the server checks whether the cookie parameter in the http header contains the sessionid, if sessionid exists, view the session information stored on the server based on the sessionid. If sessionid does not have a server, it will be assigned to it and written to the cookie field, which will be included when the browser initiates other requests next time.
Therefore, in order to solve the problem I mentioned above, we can do this. When the client first requests the server, that is, it obtains a sessionid during login and stores it locally, then, the sessionid will be included in the header each time the server is requested and written to the cookie field. However, if you store the sessionid locally, it will be the sessionid each time. Therefore, we recommend that you clear the sessionid stored locally each time the program starts, then, send the first request to obtain the new sessionid.
The following is the client code with sessionid in the header during ajax requests:
$.ajax({ type: 'post', headers: { 'Cookie':'JSESSIONID=1k2naixut68f81q5rpr0c3n4vc' }, data: {}, dataType: 'json', timeout: 30000, url: '/test', success: function (res) { alert("success"); }, error: function (e) { alert("false"); } })
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!