Friday, October 8, 2010

Struts framework in the Session object to create and control



Let me first explain to the session object created in the web development and sessionId generation and returned to the client operating system.

session object when the client first visit, create a new session object. and also generate a sessionId, sessionId in the response will form in response packet back to client browser more memory or a way to rewrite the url returned to the client, to keep the entire conversation, as long as the sever side of the session object is not destroyed, later calling request.getSession () when directly under the client's sessionId to retrieve the generated server-side session object and returns, not again go to New , except in accordance with the sessionId not retrieve session object.

The following is tested in IE, because of a BUG is IE6.0 privacy settings even if IE is to prevent all cookie, they still will be a session cookie to save sessionId. So here are based on the session cookie to discuss

(1) in the server does not shut down, and destroy the object in the session time, when the client again to request the servlet or jsp server side, it will be the first request generated sessionId, and with the information in the request header to the server end send, server-side after receiving sessionId sessionId will go under the search (this process is transparent) server session corresponding to the object and directly back to the session object, this time not to re-establish a new session object.

(2) When the server shut down (prior to the session object generated also disappeared), or the session object after the destruction of time, none of the browser window, and again in the browser window to request the servlet and jsp sever-side when At this point the same will sessionId (server down or destroyed when the session generated sessionId) sent to the server side, server based sessionId find the corresponding session object, but the session object does not exist at this time, at this time will re-generate a new session object, and generate new sessionId and the same will be the new generation of sessionId in response to messages sent to the browser in the form of memory.

(3) When the server does not shut down, and session object in the destruction of time, when the request for a jsp page back to the client, close the browser window, this time his memory sessionId along with it the destruction, to request the re- sever-side of the servlet or jsp, it will re-generate a sessionId to the client browser, and there browse memory.

The above theory is set up to test the servlet, the following talk about the struts framework of the above tests of different places.

Under the first test procedure summary that process:

Client requests index.do--> enter the server side IndexAction ---> Turn login.jsp page -----> request login.do-----> into server-side LoginAction.

First note: IndexAction session object is not to produce, login.jsp set.

(1) Environmental servlet + jsp:

In sevlet + jsp test track, the shift in index.do login.jsp, after entering IndexAction when the browser at this time in memory there is no session cookie, then enter the login.jsp on request login.do LoginAction, the use of request. getCookies () test, its value is null it! result is stable together, because from the beginning had set the final session did not produce it!

(2) Environmental struts + jsp:

In struts + jsp test track, the same as with the above process, began to think the result should be the same, but after debugging and found the result is not thought so. Later in the login.do into LoginActoin with request.getCookies ( ) tests, we found that its value is not null, that he has a name and value, began to not understand, because there is no create-off session object, it would be no session cookie value. But the result, so think of this time here memory devices also should have a session cookie, the problem is here! come from?

Later, after careful consideration, the thought of struts in the characteristics of writing our own struts Action class is the successor of Action, and is the result before the struts ActionServlet the central controller to control the steering, so I think certainly in the process into my own IndexAction written before, struts framework of the code certainly has created a session object and has generated sessionId. So to find relevant books on the workflow and see the ActionServlet calls which class, which really looked after seeing HttpSession session = request. getSession (); this sentence! then answer it is clear which.

We all know that struts the ActionServlet class, receiving our client's request (*. do) after (before a series of initialization will do the work), not directly to deal with our request and calls the corresponding Action (if we write IndexAction), but the processing to the RequestProcessor class, the process method will call the appropriate number of methods to accomplish the request handling and steering operation. a way which attracted my attention, is processLocale () method.

Struts Framework: RequestProcess class processLocale () method of prototype as follows:

Code:

protected void processLocale (HttpServletRequest request,

HttpServletResponse response) (

/ / Are we configured to select the Locale automatically?

if (! moduleConfig.getControllerConfig (). getLocale ()) (

return;

)

/ / Has a Locale already been selected?

HttpSession session = request.getSession ();

if (session.getAttribute (Globals.LOCALE_KEY)! = null) (

return;

)

/ / Use the Locale returned by the servlet container (if any)

Locale locale = request.getLocale ();

if (locale! = null) (

if (log.isDebugEnabled ()) (

log.debug ("Setting user locale '" + locale + "'");

)

session.setAttribute (Globals.LOCALE_KEY, locale);

)

)

Such in the struts-config.xml configuration file in the corresponding configuration items: its default state locale property is true, also would call processLocale method and the first request to create session objects and generate sessionId. but to false after the first request would not reach the ActionServlet calls processLocale method, there could not generate a session object.

The results also come out in the struts application, *. do arrive after the server-side through the ActionServlet, turn to our own IndexAction written before, (the default state ), they have produced a session object and sessionId, which is generated struts framework of the class, even if we write in IndexAction in HttpSession session = request.getSession (); it is RequestProcess class processLocale () method generated At this point the session of the isNew also still true, because it has not returned to the client, which is newly created, then in accordance with the above process, when adopted in the login.jsp login.do into LoginAction, its request.getCookies () of course also has a value! and its value is RequestProcess class processLocale () method produces the generated session object.

If we add in the struts-config.xml when, at this time if another process to track according to the above procedure, and LoginAction with request.getCookies () test, its value is null, of course, write in IndexAction HttpSession session = request.getSession (); when it is time to enter IndexAction newly created, isNew is true. (







Recommended links:



VOB To Zune



Sun rushes to the fore: All FREE



VOB To MPG



Specialist Audio And Multimedia



Thunder mode go from here



ACDSee make the World Cup with a SLIDE



Shop Seasonal - Screen Savers



Why Do We Need SAS?



Identify the black and white ash field, rapid correction of color cast Photos



ASF Converter



how to Remove drm from protected movies and burn



CMMB TD and who needs who more?



How to SHORT-TERM consultants into your team



No comments:

Post a Comment