⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 0192.htm

📁 JspServlet教程专栏 对javaservlet讲述的非常详细
💻 HTM
📖 第 1 页 / 共 3 页
字号:
Controller:&nbsp; timeByZone.jsp<br>
The&nbsp; controller&nbsp; can&nbsp; be&nbsp; a&nbsp; servlet&nbsp; or&nbsp; a&nbsp; jsp&nbsp; file.&nbsp; I&nbsp; prefer&nbsp; to&nbsp; use&nbsp; JSP,&nbsp; as&nbsp; I&nbsp; don't&nbsp; have&nbsp; to&nbsp; worry&nbsp; about&nbsp; compiling&nbsp; the&nbsp; class&nbsp; each&nbsp; time&nbsp; I&nbsp; make&nbsp; changes.&nbsp; However,&nbsp; you&nbsp; lose&nbsp; granularity&nbsp; this&nbsp; way,&nbsp; and&nbsp; make&nbsp; it&nbsp; more&nbsp; difficult&nbsp; to&nbsp; extend&nbsp; this&nbsp; class&nbsp; later&nbsp; (we'll&nbsp; review&nbsp; this&nbsp; in&nbsp; Advanced&nbsp; JSP&nbsp; Programming).&nbsp; <br>
<br>
======================================================================<br>
&lt;xml&nbsp; version="1.0"&nbsp; ?><br>
&lt;!--Worker&nbsp; Class,&nbsp; nobody&nbsp; should&nbsp; see&nbsp; me--><br>
&lt;jsp:scriptlet><br>
//the&nbsp; parameter&nbsp; "zone"&nbsp; shall&nbsp; be&nbsp; equal&nbsp; to&nbsp; a&nbsp; number&nbsp; between&nbsp; 0&nbsp; and&nbsp; 24&nbsp; (inclusive)<br>
TimeZone&nbsp; timeZone&nbsp; =&nbsp; TimeZone.getDefault();&nbsp; //returns&nbsp; the&nbsp; default&nbsp; TimeZone&nbsp; for&nbsp; the&nbsp; server<br>
if&nbsp; (request.getParameterValues("zone")&nbsp; !=&nbsp; null)<br>
{<br>
String&nbsp; timeZoneArg&nbsp; =&nbsp; request.getParameterValues("zone")[0];<br>
timeZone&nbsp; =&nbsp; TimeZone.getTimeZone("GMT+"&nbsp; +&nbsp; timeZoneArg&nbsp; +&nbsp; ":00");&nbsp; <br>
//&nbsp; gets&nbsp; a&nbsp; TimeZone.&nbsp; For&nbsp; this&nbsp; example&nbsp; we're&nbsp; just&nbsp; going&nbsp; to&nbsp; assume&nbsp; <br>
//&nbsp; its&nbsp; a&nbsp; positive&nbsp; argument,&nbsp; not&nbsp; a&nbsp; negative&nbsp; one.<br>
}<br>
TimeBean&nbsp; timeBean&nbsp; =&nbsp; new&nbsp; TimeBean();<br>
timeBean.setHours&nbsp; =&nbsp; myCalendar.get(Calendar.HOUR_OF_DAY);<br>
timeBean.setMinutes&nbsp; =&nbsp; myCalendar.get(Calendar.MINUTE);<br>
timeBean.setSeconds&nbsp; =&nbsp; myCalendar.get(Calendar.SECOND);<br>
HttpSession&nbsp; mySession&nbsp; =&nbsp; request.getSession();<br>
mySession.putValue("tempTimeBean",&nbsp; timeBean);<br>
<br>
&lt;/jsp:scriptlet><br>
&lt;jsp:forward&nbsp; page="displayTime.jsp"&nbsp; /><br>
======================================================================<br>
View:&nbsp; displayTime.jsp<br>
Again,&nbsp; the&nbsp; view&nbsp; can&nbsp; be&nbsp; either&nbsp; a&nbsp; servlet&nbsp; or&nbsp; a&nbsp; jsp&nbsp; file.&nbsp; Here&nbsp; we'll&nbsp; get&nbsp; the&nbsp; Bean&nbsp; from&nbsp; the&nbsp; Session,&nbsp; and&nbsp; display&nbsp; its&nbsp; values.&nbsp; We'll&nbsp; actually&nbsp; do&nbsp; this&nbsp; twice,&nbsp; to&nbsp; illustrate&nbsp; again&nbsp; how&nbsp; Beans&nbsp; are&nbsp; used.<br>
<br>
<br>
======================================================================<br>
&lt;xml&nbsp; version="1.0"&nbsp; ?><br>
&lt;H1>Time&nbsp; JSP&lt;/H1><br>
&lt;jsp:useBean&nbsp; class="TimeBean"&nbsp; id="tempTimeBean"&nbsp; scope="session"&nbsp; />&nbsp; <br>
&lt;jsp:getProperty&nbsp; name="tempTimeBean"&nbsp; property="hours">:<br>
&lt;jsp:getProperty&nbsp; name="tempTimeBean"&nbsp; property="minutes">:<br>
&lt;jsp:getProperty&nbsp; name="tempTimeBean"&nbsp; property="seconds"><br>
&lt;!--&nbsp; these&nbsp; would&nbsp; have&nbsp; printed&nbsp; "null"&nbsp; if&nbsp; tempTimeBean&nbsp; was&nbsp; not&nbsp; instantiated&nbsp; by&nbsp; timeByZone.jsp&nbsp; --><br>
<br>
&lt;jsp:scriptlet><br>
HttpSession&nbsp; mySession&nbsp; =&nbsp; request.getSession();<br>
TimeBean&nbsp; timeBean&nbsp; =&nbsp; mySession.getValue("tempTimeBean");<br>
if&nbsp; (timeBean&nbsp; !=&nbsp; null)<br>
{&nbsp; //&nbsp; check&nbsp; to&nbsp; make&nbsp; sure&nbsp; its&nbsp; not&nbsp; null,&nbsp; to&nbsp; avoid&nbsp; NullPointerExceptions<br>
out.print(timeBean.getHours());<br>
out.print(":");<br>
out.print(timeBean.getMinutes());<br>
out.print(":");<br>
out.print(timeBean.getSeconds());<br>
}<br>
else<br>
{<br>
out.println("Press&nbsp; your&nbsp; Back&nbsp; button&nbsp; and&nbsp; select&nbsp; a&nbsp; TimeZone");<br>
}<br>
&lt;/jsp:scriptlet><br>
======================================================================<br>
The&nbsp; second&nbsp; method&nbsp; (using&nbsp; code&nbsp; inside)&nbsp; may&nbsp; be&nbsp; more&nbsp; cumbersome,&nbsp; but&nbsp; allows&nbsp; the&nbsp; developer&nbsp; to&nbsp; ensure&nbsp; against&nbsp; ugly&nbsp; output&nbsp; (such&nbsp; as&nbsp; "null:null:null&nbsp; null")&nbsp; if&nbsp; the&nbsp; Session&nbsp; bean&nbsp; has&nbsp; not&nbsp; been&nbsp; instantiated&nbsp; &&nbsp; had&nbsp; its&nbsp; values&nbsp; set.&nbsp; This&nbsp; would&nbsp; likely&nbsp; only&nbsp; happen&nbsp; if&nbsp; the&nbsp; client&nbsp; somehow&nbsp; called&nbsp; the&nbsp; View&nbsp; page&nbsp; directly.&nbsp; The&nbsp; point&nbsp; is&nbsp; that&nbsp; using&nbsp; scriptlets&nbsp; allows&nbsp; for&nbsp; greater&nbsp; control.&nbsp; If&nbsp; you&nbsp; are&nbsp; certain&nbsp; you&nbsp; can&nbsp; control&nbsp; url&nbsp; access,&nbsp; the&nbsp; bean&nbsp; approach&nbsp; certainly&nbsp; eases&nbsp; development,&nbsp; and&nbsp; makes&nbsp; the&nbsp; View&nbsp; page&nbsp; easier&nbsp; for&nbsp; HTML&nbsp; designers&nbsp; to&nbsp; work&nbsp; with.&nbsp; <br>
<br>
The&nbsp; above&nbsp; is&nbsp; the&nbsp; "traditional"&nbsp; Model&nbsp; II&nbsp; design.&nbsp; You'll&nbsp; note&nbsp; that&nbsp; all&nbsp; the&nbsp; variables&nbsp; are&nbsp; wrapped&nbsp; up&nbsp; and&nbsp; placed&nbsp; into&nbsp; the&nbsp; Session&nbsp; object.&nbsp; This&nbsp; has&nbsp; two&nbsp; weaknesses:&nbsp; 1)&nbsp; no&nbsp; Session&nbsp; is&nbsp; availabe&nbsp; because&nbsp; the&nbsp; client&nbsp; has&nbsp; refused&nbsp; to&nbsp; participate,&nbsp; 2)&nbsp; unless&nbsp; the&nbsp; Session&nbsp; variable&nbsp; is&nbsp; explicitly&nbsp; removed&nbsp; it&nbsp; will&nbsp; continue&nbsp; to&nbsp; exist&nbsp; until&nbsp; the&nbsp; Session&nbsp; is&nbsp; destroyed&nbsp; or&nbsp; expires.&nbsp; <br>
<br>
The&nbsp; first&nbsp; case&nbsp; is&nbsp; most&nbsp; likely&nbsp; to&nbsp; happen&nbsp; when&nbsp; cookies&nbsp; are&nbsp; used&nbsp; as&nbsp; the&nbsp; State&nbsp; mechanism&nbsp; and&nbsp; the&nbsp; developers&nbsp; have&nbsp; failed&nbsp; to&nbsp; provide&nbsp; for&nbsp; the&nbsp; alternative&nbsp; form&nbsp; of&nbsp; State&nbsp; maintenance,&nbsp; URL&nbsp; rewriting.&nbsp; <br>
<br>
The&nbsp; second&nbsp; case&nbsp; is&nbsp; even&nbsp; more&nbsp; serious,&nbsp; as&nbsp; it&nbsp; can&nbsp; lead&nbsp; to&nbsp; great&nbsp; memory&nbsp; use&nbsp; if&nbsp; Sessions&nbsp; are&nbsp; defined&nbsp; to&nbsp; exist&nbsp; for&nbsp; more&nbsp; than&nbsp; the&nbsp; standard&nbsp; amount&nbsp; of&nbsp; time&nbsp; (30&nbsp; minutes&nbsp; appears&nbsp; to&nbsp; be&nbsp; the&nbsp; standard).&nbsp; Even&nbsp; in&nbsp; the&nbsp; case&nbsp; of&nbsp; 30&nbsp; minute&nbsp; Sessions,&nbsp; this&nbsp; Model&nbsp; can&nbsp; lead&nbsp; to&nbsp; disastrous&nbsp; memory&nbsp; leaks&nbsp; in&nbsp; systems&nbsp; under&nbsp; great&nbsp; use.&nbsp; Why?&nbsp; Objects&nbsp; get&nbsp; instantiated,&nbsp; set&nbsp; inside&nbsp; the&nbsp; Session&nbsp; object,&nbsp; and&nbsp; are&nbsp; not&nbsp; removed&nbsp; until&nbsp; the&nbsp; Session&nbsp; ends.&nbsp; Because&nbsp; they&nbsp; still&nbsp; have&nbsp; references&nbsp; (the&nbsp; Session&nbsp; object)&nbsp; pointing&nbsp; to&nbsp; them,&nbsp; they&nbsp; are&nbsp; not&nbsp; garbage-collected.&nbsp; In&nbsp; the&nbsp; Model&nbsp; II&nbsp; pattern,&nbsp; many&nbsp; objects&nbsp; are&nbsp; placed&nbsp; into&nbsp; the&nbsp; Session&nbsp; (either&nbsp; directly&nbsp; or&nbsp; via&nbsp; a&nbsp; JavaBean).&nbsp; As&nbsp; the&nbsp; Session&nbsp; progresses&nbsp; (more&nbsp; pages&nbsp; are&nbsp; accessed)&nbsp; memory-use&nbsp; increases&nbsp; and&nbsp; persists&nbsp; until&nbsp; the&nbsp; client&nbsp; ends&nbsp; the&nbsp; Session&nbsp; or&nbsp; the&nbsp; Session&nbsp; times&nbsp; out.&nbsp; Until&nbsp; the&nbsp; Session&nbsp; is&nbsp; invalidated,&nbsp; the&nbsp; objects&nbsp; placed&nbsp; there&nbsp; cannot&nbsp; be&nbsp; garbage-collected,&nbsp; and&nbsp; thus&nbsp; consume&nbsp; memory&nbsp; that&nbsp; could&nbsp; be&nbsp; of&nbsp; use&nbsp; elsewhere.&nbsp; <br>
<br>
One&nbsp; means&nbsp; of&nbsp; addressing&nbsp; this&nbsp; issue&nbsp; is&nbsp; to&nbsp; place&nbsp; the&nbsp; Beans&nbsp; or&nbsp; other&nbsp; variables&nbsp; into&nbsp; the&nbsp; Request&nbsp; object,&nbsp; and&nbsp; use&nbsp; RequestDispatcher.include()&nbsp; rather&nbsp; than&nbsp; RequestDispatcher.forward().&nbsp; By&nbsp; doing&nbsp; so,&nbsp; the&nbsp; View&nbsp; page&nbsp; has&nbsp; access&nbsp; to&nbsp; the&nbsp; same&nbsp; Request&nbsp; object&nbsp; as&nbsp; the&nbsp; Controller,&nbsp; and&nbsp; the&nbsp; weaknesses&nbsp; of&nbsp; the&nbsp; traditional&nbsp; Model&nbsp; II&nbsp; design&nbsp; are&nbsp; obviated.&nbsp; <br>
<br>
One&nbsp; final&nbsp; comment:&nbsp; despite&nbsp; all&nbsp; the&nbsp; above,&nbsp; I&nbsp; have&nbsp; a&nbsp; personal&nbsp; distaste&nbsp; for&nbsp; the&nbsp; Model&nbsp; II&nbsp; paradigm&nbsp; as&nbsp; it&nbsp; is&nbsp; commonly&nbsp; implemented.&nbsp; The&nbsp; creation&nbsp; of&nbsp; a&nbsp; system&nbsp; where&nbsp; the&nbsp; client&nbsp; is&nbsp; sent&nbsp; to&nbsp; an&nbsp; address,&nbsp; but&nbsp; is&nbsp; redirected&nbsp; to&nbsp; a&nbsp; different&nbsp; class,&nbsp; is&nbsp; for&nbsp; some&nbsp; reason&nbsp; abhorrent&nbsp; to&nbsp; me.&nbsp; For&nbsp; this&nbsp; reason,&nbsp; I've&nbsp; modified&nbsp; the&nbsp; design&nbsp; in&nbsp; the&nbsp; following&nbsp; manner:&nbsp; <br>
<br>
Controller:&nbsp; timeByZone2.jsp<br>
As&nbsp; before,&nbsp; the&nbsp; controller&nbsp; uses&nbsp; the&nbsp; Request&nbsp; values&nbsp; to&nbsp; obtain&nbsp; the&nbsp; necessary&nbsp; data&nbsp; and&nbsp; put&nbsp; that&nbsp; data&nbsp; into&nbsp; the&nbsp; Request&nbsp; object.&nbsp; The&nbsp; difference&nbsp; this&nbsp; time&nbsp; is&nbsp; that&nbsp; the&nbsp; View&nbsp; page&nbsp; will&nbsp; call&nbsp; the&nbsp; Controller&nbsp; using&nbsp; RequestDispatcher.include().&nbsp; In&nbsp; this&nbsp; way,&nbsp; the&nbsp; client&nbsp; is&nbsp; never&nbsp; redirected,&nbsp; and&nbsp; Requests&nbsp; are&nbsp; not&nbsp; "chained".&nbsp; Rather,&nbsp; the&nbsp; class/jsp&nbsp; called&nbsp; asks&nbsp; someone&nbsp; else&nbsp; to&nbsp; do&nbsp; some&nbsp; work&nbsp; for&nbsp; it,&nbsp; then&nbsp; continues.<br>
<br>
======================================================================<br>
&lt;xml&nbsp; version="1.0"&nbsp; ?><br>
&lt;!--Worker&nbsp; Class,&nbsp; nobody&nbsp; should&nbsp; see&nbsp; me--><br>
&lt;jsp:scriptlet><br>
//the&nbsp; parameter&nbsp; "zone"&nbsp; shall&nbsp; be&nbsp; equal&nbsp; to&nbsp; a&nbsp; number&nbsp; between&nbsp; 0&nbsp; and&nbsp; 24&nbsp; (inclusive)<br>
TimeZone&nbsp; timeZone&nbsp; =&nbsp; TimeZone.getDefault();&nbsp; //returns&nbsp; the&nbsp; default&nbsp; TimeZone&nbsp; for&nbsp; the&nbsp; server<br>
if&nbsp; (request.getParameterValues("zone")&nbsp; !=&nbsp; null)<br>
{<br>
String&nbsp; timeZoneArg&nbsp; =&nbsp; request.getParameterValues("zone")[0];<br>
timeZone&nbsp; =&nbsp; TimeZone.getTimeZone("GMT+"&nbsp; +&nbsp; timeZoneArg&nbsp; +&nbsp; ":00");&nbsp; <br>
//&nbsp; gets&nbsp; a&nbsp; TimeZone.&nbsp; For&nbsp; this&nbsp; example&nbsp; we're&nbsp; just&nbsp; going&nbsp; to&nbsp; assume&nbsp; <br>
//&nbsp; its&nbsp; a&nbsp; positive&nbsp; argument,&nbsp; not&nbsp; a&nbsp; negative&nbsp; one.<br>
}<br>
TimeBean&nbsp; timeBean&nbsp; =&nbsp; new&nbsp; TimeBean();<br>
timeBean.setHours&nbsp; =&nbsp; myCalendar.get(Calendar.HOUR_OF_DAY);<br>
timeBean.setMinutes&nbsp; =&nbsp; myCalendar.get(Calendar.MINUTE);<br>
timeBean.setSeconds&nbsp; =&nbsp; myCalendar.get(Calendar.SECOND);<br>
request.setAttribute("tempTimeBean",&nbsp; timeBean);<br>
&lt;/jsp:scriptlet><br>
======================================================================<br>
View:&nbsp; displayTime2.jsp<br>
Much&nbsp; like&nbsp; displayTime.jsp,&nbsp; however&nbsp; you'll&nbsp; see&nbsp; that&nbsp; timeByZone2.jsp&nbsp; is&nbsp; called&nbsp; at&nbsp; the&nbsp; top&nbsp; of&nbsp; the&nbsp; page.&nbsp; Notice&nbsp; that&nbsp; the&nbsp; scope&nbsp; of&nbsp; &lt;jsp:useBean&nbsp; />&nbsp; has&nbsp; changed&nbsp; to&nbsp; "request".<br>
<br>
======================================================================<br>
&lt;xml&nbsp; version="1.0"&nbsp; ?><br>
&lt;H1>Time&nbsp; JSP&lt;/H1><br>
<br>
&lt;jsp:include&nbsp; page="timeByZone2.jsp"&nbsp; /><br>
<br>
&lt;jsp:useBean&nbsp; class="TimeBean"&nbsp; id="tempTimeBean"&nbsp; scope="request"&nbsp; />&nbsp; <br>
&lt;jsp:getProperty&nbsp; name="tempTimeBean"&nbsp; property="hours">:<br>
&lt;jsp:getProperty&nbsp; name="tempTimeBean"&nbsp; property="minutes">:<br>
&lt;jsp:getProperty&nbsp; name="tempTimeBean"&nbsp; property="seconds"><br>
&lt;!--&nbsp; these&nbsp; would&nbsp; have&nbsp; printed&nbsp; "null"&nbsp; if&nbsp; tempTimeBean&nbsp; was&nbsp; not&nbsp; instantiated&nbsp; by&nbsp; timeByZone2.jsp&nbsp; --><br>
<br>
======================================================================<br>
In&nbsp; a&nbsp; system&nbsp; currently&nbsp; under&nbsp; construction,&nbsp; we've&nbsp; made&nbsp; use&nbsp; of&nbsp; this&nbsp; method&nbsp; to&nbsp; create&nbsp; chains&nbsp; of&nbsp; classes,&nbsp; each&nbsp; responsible&nbsp; for&nbsp; its&nbsp; own&nbsp; processing.&nbsp; By&nbsp; identifying&nbsp; common&nbsp; presentation&nbsp; formats,&nbsp; we've&nbsp; created&nbsp; View&nbsp; objects&nbsp; that&nbsp; can&nbsp; be&nbsp; reused&nbsp; in&nbsp; yet&nbsp; higher&nbsp; level&nbsp; JavaServer&nbsp; Pages.&nbsp; Our&nbsp; goal&nbsp; is&nbsp; to&nbsp; create&nbsp; pages&nbsp; that&nbsp; are&nbsp; designed&nbsp; for&nbsp; reuse,&nbsp; and&nbsp; to&nbsp; reduce&nbsp; the&nbsp; number&nbsp; of&nbsp; presentation&nbsp; classes.&nbsp; <br>
<br>
Single&nbsp; Servlet&nbsp; Model&nbsp; (A&nbsp; Model&nbsp; II&nbsp; Design)<br>
When&nbsp; I've&nbsp; had&nbsp; time&nbsp; to&nbsp; adequately&nbsp; research&nbsp; and&nbsp; implement&nbsp; this&nbsp; idea,&nbsp; I'll&nbsp; post&nbsp; something&nbsp; here

  </table>
<p align="center"><script src="../../2.js"></script></a>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -