JSP 构架-2种方式:Model I和Model II__教程 |
|
日期:2007-5-20 1:18:54 人气:165 [大 中 小] |
|
|
|
The second case is even more serious, as it can lead to great memory use if Sessions are defined to exist for more than the standard amount of time (30 minutes appears to be the standard). Even in the case of 30 minute Sessions, this Model can lead to disastrous memory leaks in systems under great use. Why? Objects get instantiated, set inside the Session object, and are not removed until the Session ends. Because they still have references (the Session object) pointing to them, they are not garbage-collected. In the Model II pattern, many objects are placed into the Session (either directly or via a JavaBean). As the Session progresses (more pages are accessed) memory-use increases and persists until the client ends the Session or the Session times out. Until the Session is invalidated, the objects placed there cannot be garbage-collected, and thus consume memory that could be of use elsewhere.
One means of addressing this issue is to place the Beans or other variables into the Request object, and use RequestDispatcher.include() rather than RequestDispatcher.forward(). By doing so, the View page has access to the same Request object as the Controller, and the weaknesses of the traditional Model II design are obviated.
One final comment: despite all the above, I have a personal distaste for the Model II paradigm as it is commonly implemented. The creation of a system where the client is sent to an address, but is redirected to a different class, is for some reason abhorrent to me. For this reason, I´ve modified the design in the following manner:
Controller: timeByZone2.jsp As before, the controller uses the Request values to obtain the necessary data and put that data into the Request object. The difference this time is that the View page will call the Controller using RequestDispatcher.include(). In this way, the client is never redirected, and Requests are not "chained". Rather, the class/jsp called asks someone else to do some work for it, then continues.
====================================================================== //the parameter "zone" shall be equal to a number between 0 and 24 (inclusive) TimeZone timeZone = TimeZone.getDefault(); //returns the default TimeZone for the server if (request.getParameterValues("zone") != null) { String timeZoneArg = request.getParameterValues("zone")[0]; timeZone = TimeZone.getTimeZone("GMT+" + timeZoneArg + ":00"); // gets a TimeZone. For this example we´re just going to assume // its a positive argument, not a negative one. } TimeBean timeBean = new TimeBean(); timeBean.setHours = myCalendar.get(Calendar.HOUR_OF_DAY); timeBean.setMinutes = myCalendar.get(Calendar.MINUTE); timeBean.setSeconds = myCalendar.get(Calendar.SECOND); request.setAttribute("tempTimeBean", timeBean); |
|
出处:本站原创 作者:佚名 |
|
|