📄 chapter8.htm
字号:
different request types.</I></FONT></P>
<P><B>Add a method named procesRequest with the following signature: </B></P><PRE><FONT face=Sans><FONT size=3>protected void processRequest (HttpServletRequest request, HttpServletResponse response)</FONT></FONT>
<FONT size=3><FONT face=Sans>throws ServletException, IOException </FONT></FONT></PRE>
<P><B>Delegate request from doGet and doPost method to this method as shown
below</B>.</P>
<P><IMG height=317 src="chapter8.files/doGetAndDoPost.png" width=299 align=left
border=0 name=Graphic14><BR clear=left><B>Now implement the processRequest()
method. </B></P>
<P><B>This method is structured such that it will check for the parameter
useraction within the request object. If the useraction parameter is empty then
it will build a url for the login screen and generate a login screen page. If
useraction has some value then it examines it. If it finds the request is to
display items then it builds the url for that and generates a page displaying
all the items. Finally, for errors a page is generated to display the
error.</B></P>
<P>Add following attributes shown below, for building different urls.</P>
<P><IMG height=102 src="chapter8.files/Attributes.png" width=464 align=left
border=0 name=Graphic18><BR clear=left><B>Create a session object and get value
from useraction parameter of request object as shown below in code
snippet.</B></P>
<P><IMG height=578 src="chapter8.files/ProcessRequest.png" width=822 align=left
border=0 name=Graphic17><BR clear=left><B>If useraction is empty, as it will be
initially, then build the url for the login screen, and generate the login
screen by invoking the method displayLoginScreen. </B>Code snippet for
displayLoginScreen is shown below.</P>
<P><IMG height=244 src="chapter8.files/DisplayLoginScreen.png" width=502
align=left border=0 name=Graphic19><BR clear=left><B>This method calls
displayLoginDataFields to generate the data fields for form submission as shown
below.</B></P>
<P><IMG height=198 src="chapter8.files/DisplayLoginDataFields.png" width=576
align=left border=0 name=Graphic20><BR clear=left><B>Once this form is
submitted, it checks to see whether validation is successful by invoking the
method loginUser, which invokes the loginUser method in the StoreAccess Bean as
shown below.</B></P>
<P><IMG height=232 src="chapter8.files/LoginUser.png" width=542 align=left
border=0 name=Graphic25><BR clear=left><B>Once that is finished, as shown above
in the processRequest method code snippet, it builds url's for displaying items
and error. Code snippet for the method displayAllItems is shown below.</B></P>
<P><IMG height=568 src="chapter8.files/DisplayAllItems.png" width=556 align=left
border=0 name=Graphic3><BR clear=left><B>Code snippet for
displayLoginErrorScreen shown below.</B> </P>
<P style="FONT-STYLE: normal" align=left><IMG height=234
src="chapter8.files/DisplayLoginError.png" width=570 align=left border=0
name=Graphic22><BR clear=left><FONT color=#000000><B>All these helper method are
being used by the processRequest method, which is acting as a Controller and
dispatches the request to the appropriate view, such as the login screen or
display items screen.</B></FONT></P>
<P>Now our servlet is complete, as implementation of the rest of the methods is
left as an exercise.</P>
<H3>Deploy AccessController Servlet :</H3>
<P><B>In order to deploy your servlet, go to LombozJ2EE view</B>.</P>
<P><B>Select web module OnlineStore > right click > select Deploy module
as shown below , make sure the server is running</B>.</P>
<P><IMG height=360 src="chapter8.files/DeployWebModule.png" width=296 align=left
border=0 name=Graphic23><BR clear=left><B>Deployment status appears on the
console as shown below. If deployment is successful then test your
servlet.</B></P>
<P><IMG height=226 src="chapter8.files/ConsolesDeployView.png" width=844
align=left border=0 name=Graphic24><BR clear=left><BR><BR></P>
<H3>Test your Servlet :</H3>
<P><B>Go to your browser and access the servlet using following URL,
'http://localhost:8080/OnlineStore/access'</B></P>
<P><B>where 'access' is the url mapping that was assigned while creating the
servlet using the Servlet Creation Wizard, and OnlineStore is the web
module,where this servlet resides.</B></P>
<P><B>The login screen will be displayed. Enter username as 'ANDY' and password
as 'PASSWD'. The next screen will be list of items in MyStore as shown
below.</B></P>
<P><IMG height=291 src="chapter8.files/ServletResult.png" width=601 align=left
border=0 name=Graphic26><BR clear=left><BR><BR></P>
<P><B>We have successfully created a servlet, now let's access this catalogue
via a JSP page.</B></P>
<H3>Create JSP Page :</H3>
<P style="MARGIN-BOTTOM: 0in"><B>Go To Package Explorer > Expand Web Module
node (OnlineStore) > right click and a menu will pop up.</B></P>
<P><B>On the pop up menu > New > Select Lomboz JSP Wizard as shown
below.</B></P>
<P><IMG height=290 src="chapter8.files/LombozJSPWizard.png" width=610 align=left
border=0 name=Graphic28><BR clear=left><B>Add file name showItems.jsp as shown
below.</B></P>
<P><IMG height=586 src="chapter8.files/CreateJSPWizardFirst.png" width=628
align=left border=0 name=Graphic4><BR clear=left><B>Press Next > Menu, the
form Set JSP details will appear > Select Add.. under Beans section as shown
below.</B></P>
<P><B>The menu for Select Beans will appear > Add ID as itemData, Scope as
page and Class as ItemData. </B></P>
<P><IMG height=504 src="chapter8.files/CreateJSPWizardSecond.png" width=804
align=left border=0 name=Graphic5><BR clear=left><B>Press Ok. Now selected JSP
details can be seen after selecting various options as shown below.</B></P>
<P style="MARGIN-BOTTOM: 0in"><IMG height=540
src="chapter8.files/CreateJSPWizardThird.png" width=556 align=left border=0
name=Graphic21><BR clear=left><BR></P>
<P style="MARGIN-BOTTOM: 0in"><B>Press Finish. A new file named showItems.jsp
will be created under the web module OnlineStore as shown below</B>.</P>
<P><BR><BR></P>
<P><IMG height=560 src="chapter8.files/ShowItemsCreated.png" width=726
align=left border=0 name=Graphic27><BR clear=left><BR><BR></P>
<H3>Modify Method processRequest in Servlet AccessController : </H3>
<P>Now, in order to display all items of MyStore from a JSP page, we have to
modify the proccessRequest method in servlet AccessController. </P>
<P><B>Modify String ITEMS_SCREEN to "/showItems.jsp" as shown below.</B></P>
<P><IMG height=44 src="chapter8.files/StringShowItems.png" width=386 align=left
border=0 name=Graphic31><BR clear=left><B>Comment out the call to method
displayAllItems in processRequest.</B></P>
<P><B>Add a Session attribute named itemsList with values of listItems.</B></P>
<P><B>Add a Request Dispatcher and pass buildUrl as an argument, which has the
url for the showItems.jsp page.</B></P>
<P><B>Forward to that request dispatcher to draw the JSP page.</B> </P>
<P>Code snippet of the modified processRequest method is shown below.</P>
<H3 style="MARGIN-TOP: 0in; MARGIN-BOTTOM: 0in"><IMG height=560
src="chapter8.files/ProcessRequest.Modified.png" width=510 align=left border=0
name=Graphic33><BR clear=left><BR></H3>
<H3>Add Html and JSP Tags :</H3>
<P><B>First import the following packages: au.com.tusc.cmp.*,
java.util.ArrayList and java.util.Iterator.</B></P>
<P><B>Now add the necessary HTML and JSP tags to access items of
MyStore.</B></P>
<P><B>Get itemsList from our session attribute set in servlet
AccessController.</B></P>
<P><B>Set itemData as a page attribute via pageContext.</B></P>
<P><B>Now display the attributes of itemData using JSP property access tags.
Code snippet for the showItems page is shown below.</B></P>
<P style="MARGIN-BOTTOM: 0in"><IMG height=592
src="chapter8.files/ShowItemsImplemented.png" width=600 align=left border=0
name=Graphic29><BR clear=left><FONT color=#800000><I>Note : This JSP Page is a
view in the Front Controller Pattern discussed above.</I></FONT></P>
<P><BR><BR></P>
<P><B>Now the JSP page is done.</B></P>
<P><B>Go to OnlineStore (web module) node > right click > select Lomboz
J2EE.. on pop up menu > Check All JSP Syntax.</B></P>
<P><B>See if there are any errors.</B></P>
<P><B>Go to OnlineStore node > right click > select Lomboz J2EE.. on pop
up menu > Add WEB-INF/lib JARs to the Classpath as shown below.</B></P>
<P><IMG height=391 src="chapter8.files/AddWeb-INF.png" width=502 align=left
border=0 name=Graphic30><BR clear=left><BR><BR></P>
<H3>Deploy Module OnlineStore :</H3>
<P><B>In order to deploy, go to LombozJ2EE view</B>.</P>
<P><B>Select web module OnlineStore > right click > select Deploy module,
make sure the server is running.</B></P>
<P><B>Deployment status will be visible on the console. If deployment is
successful then test your JSP page.</B></P>
<H3>Test your JSP Page :</H3>
<P><B>Go to your browser and access the servlet using the following web address,
'http://localhost:8080/OnlineStore/access'</B></P>
<P><B>where 'access' is the url mapping that was assigned whilst creating the
servlet in the Servlet Creation Wizard, and OnlineStore is the web module where
the servlet AccessController resides.</B></P>
<P><B>The login screen will be displayed. Enter username as 'ANDY' and password
as 'PASSWD'. The next screen will be the list of items in MyStore as shown
below.</B></P>
<P><IMG height=251 src="chapter8.files/RunShowItemJSP.png" width=404 align=left
border=0 name=Graphic32><BR clear=left><BR><BR></P>
<P><B>You have successfully created a JSP page using all the J2EE components,
and they have been created and deployed successfully. Congratulations!</B></P>
<P style="MARGIN-BOTTOM: 0in"><BR></P>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<THEAD>
<TR vAlign=top>
<TD width="33%"><A href="http://www.tusc.com.au/tutorial/html/chap7.html"
name=Chapter7><IMG height=35 src="chapter8.files/PrevArrow.png" width=51
align=left border=0 name=Graphic15><BR clear=left></A></TD>
<TD width="33%">
<P align=center><A name="Table of Contents"></A><A
href="http://www.tusc.com.au/tutorial/html/index.html"
name="Table of Contents"><IMG height=34 src="chapter8.files/TOC.png"
width=51 align=bottom border=0 name=Graphic2></A></P></TD>
<TD width="33%"><A href="http://www.tusc.com.au/tutorial/html/chap9.html"
name=Chap9><IMG height=36 src="chapter8.files/NextArroww.png" width=49
align=right border=0 name=Graphic34><BR clear=right></A></TD></TR></THEAD>
<TBODY></TBODY></TABLE>
<P align=center><IMG height=2 src="chapter8.files/blurulr6.gif" width=796
align=bottom border=0 name=Graphic56></P>
<TABLE cellSpacing=0 cellPadding=4 width="100%" bgColor=#0a6a91 border=0>
<COLGROUP>
<COL width=51>
<COL width=74>
<COL width=69>
<COL width=62>
<TBODY>
<TR>
<TD width="20%">
<P><!-- Footer --><A href="http://www.tusc.com.au/"><IMG height=73
alt=TUSC src="chapter8.files/logo.gif" width=149 align=bottom border=0
name=Graphic1></A> </P></TD>
<TD width="29%">
<P align=left><IMG height=73 alt="Reliable, On-Time Delivery."
src="chapter8.files/reliable.gif" width=267 align=bottom border=0
name=Graphic2> </P></TD>
<TD width="27%">
<P align=left><!--copyright details--><FONT color=#ffffff>Copyright 2003
TUSC Pty. Ltd.</FONT> </P></TD>
<TD width="24%"></TD></TR></TBODY></TABLE>
<P><!-- footer finishes --><BR><BR></P></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -