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

📄 8.mht

📁 Enterprise Java Bean的PPT文档,很好的东西.
💻 MHT
📖 第 1 页 / 共 5 页
字号:
src=3D"http://www.tusc.com.au/tutorial/images/chap8/GenertedWebModule.png=
"=20
width=3D202 align=3Dleft border=3D0 name=3DGraphic12><BR =
clear=3Dleft>Files of interest=20
include web.xml, where all the deployment descriptors will be placed (as =

discussed above), and targets.xml, which contains information about the =
server=20
in which it will be deployed (in this case JBOSS). See the code snippet =
below=20
from targets.xml.</P>
<P><IMG height=3D54=20
src=3D"http://www.tusc.com.au/tutorial/images/chap8/targetsxml.png" =
width=3D320=20
align=3Dleft border=3D0 name=3DGraphic13><BR clear=3Dleft><BR><BR></P>
<H3>Add Business Method :</H3>
<P><B>Before we start implementing our servlet, add one more business =
method to=20
StoreAccess Bean called getAllItems() with the following =
signature:</B></P><PRE style=3D"MARGIN-BOTTOM: 0.2in"><FONT =
face=3DSans><FONT size=3D3><FONT color=3D#000000>public</FONT> <FONT =
color=3D#000000>java.util.ArrayList</FONT> <FONT =
color=3D#000000>getAllItems()</FONT></FONT></FONT></PRE>
<P><FONT color=3D#000000><B>This method will return all the items in =
MyStore by=20
invoking the finder method in ItemsLocalHome named findAll, as shown =
below (code=20
snippet from StoreAccess bean):</B></FONT></P>
<P><IMG height=3D390=20
src=3D"http://www.tusc.com.au/tutorial/images/chap8/GetAllItems.png" =
width=3D532=20
align=3Dleft border=3D0 name=3DGraphic16><BR clear=3Dleft><BR><BR></P>
<P>Now let's start implementing the generated methods in the =
servlet.</P>
<H3>Implement init method :</H3>
<P><B>This method is responsible for initializing servlets, so it is =
invoked=20
when the servlet is first created and is <U>not</U> called again for =
each user=20
request.</B></P>
<P><B>We will cache the references for our StoreAccess Bean in this =
method, as=20
all the client interfaces available are exposed in StoreAccess.</B></P>
<P><B>So, first create a context. Then get a reference to a StoreAccess =
object=20
by looking up the StoreAccess bean via the JNDI API.</B></P>
<P><B>Add a variable storeAccessHome of type StoreAccessHome to store =
the=20
reference obtained by narrowing the object.</B></P>
<P><FONT color=3D#000000><B>Create helper methods for getting Context, =
Home and=20
assigning the reference to storeAccessHome as shown below</B>. =
</FONT></P>
<P><IMG height=3D580 =
src=3D"http://www.tusc.com.au/tutorial/images/chap8/init.png"=20
width=3D582 align=3Dleft border=3D0 name=3DGraphic15><BR =
clear=3Dleft><FONT=20
color=3D#800000><I>Note : We have to narrow this object because we are =
accessing a=20
remote interface. If it was a local interface, we wouldn't need to =
narrow the=20
object.</I></FONT></P>
<H3>Implement methods doGet and doPost :</H3>
<P><B>In order to implement these two methods we will create a helper =
method to=20
provide the functionality for both of them. The request is delegated to =
this=20
method where all processing of information takes place. Once this =
business logic=20
processing is complete it dispatches the request to the appropriate view =
for=20
display, ie. JSP pages. </B></P>
<P><FONT color=3D#800000><I>Note : This approach is based on the Front =
Controller=20
pattern, where the controller acts as a central point of contact for =
handling=20
requests, delegating business processing, and coordinating with =
dispatcher=20
components, whilst dispatchers are responsible for view management and=20
navigation. This pattern suggests centralizing the handling of all =
requests in=20
this way, but also allows for different handler methods to be used in =
processing=20
different request types.</I></FONT></P>
<P><B>Add a method named procesRequest with the following signature: =
</B></P><PRE><FONT face=3DSans><FONT size=3D3>protected void =
processRequest (HttpServletRequest request, HttpServletResponse =
response)</FONT></FONT>
                                 <FONT size=3D3><FONT face=3DSans>throws =
ServletException, IOException </FONT></FONT></PRE>
<P><B>Delegate request from doGet and doPost method to this method as =
shown=20
below</B>.</P>
<P><IMG height=3D317=20
src=3D"http://www.tusc.com.au/tutorial/images/chap8/doGetAndDoPost.png" =
width=3D299=20
align=3Dleft border=3D0 name=3DGraphic14><BR clear=3Dleft><B>Now =
implement the=20
processRequest() method. </B></P>
<P><B>This method is structured such that it will check for the =
parameter=20
useraction within the request object. If the useraction parameter is =
empty then=20
it will build a url for the login screen and generate a login screen =
page. If=20
useraction has some value then it examines it. If it finds the request =
is to=20
display items then it builds the url for that and generates a page =
displaying=20
all the items. Finally, for errors a page is generated to display the=20
error.</B></P>
<P>Add following attributes shown below, for building different =
urls.</P>
<P><IMG height=3D102=20
src=3D"http://www.tusc.com.au/tutorial/images/chap8/Attributes.png" =
width=3D464=20
align=3Dleft border=3D0 name=3DGraphic18><BR clear=3Dleft><B>Create a =
session object and=20
get value from useraction parameter of request object as shown below in =
code=20
snippet.</B></P>
<P><IMG height=3D578=20
src=3D"http://www.tusc.com.au/tutorial/images/chap8/ProcessRequest.png" =
width=3D822=20
align=3Dleft border=3D0 name=3DGraphic17><BR clear=3Dleft><B>If =
useraction is empty, as=20
it will be initially, then build the url for the login screen, and =
generate the=20
login screen by invoking the method displayLoginScreen. </B>Code snippet =
for=20
displayLoginScreen is shown below.</P>
<P><IMG height=3D244=20
src=3D"http://www.tusc.com.au/tutorial/images/chap8/DisplayLoginScreen.pn=
g"=20
width=3D502 align=3Dleft border=3D0 name=3DGraphic19><BR =
clear=3Dleft><B>This method calls=20
displayLoginDataFields to generate the data fields for form submission =
as shown=20
below.</B></P>
<P><IMG height=3D198=20
src=3D"http://www.tusc.com.au/tutorial/images/chap8/DisplayLoginDataField=
s.png"=20
width=3D576 align=3Dleft border=3D0 name=3DGraphic20><BR =
clear=3Dleft><B>Once this form is=20
submitted, it checks to see whether validation is successful by invoking =
the=20
method loginUser, which invokes the loginUser method in the StoreAccess =
Bean as=20
shown below.</B></P>
<P><IMG height=3D232=20
src=3D"http://www.tusc.com.au/tutorial/images/chap8/LoginUser.png" =
width=3D542=20
align=3Dleft border=3D0 name=3DGraphic25><BR clear=3Dleft><B>Once that =
is finished, as=20
shown above in the processRequest method code snippet, it builds url's =
for=20
displaying items and error. Code snippet for the method displayAllItems =
is shown=20
below.</B></P>
<P><IMG height=3D568=20
src=3D"http://www.tusc.com.au/tutorial/images/chap8/DisplayAllItems.png" =
width=3D556=20
align=3Dleft border=3D0 name=3DGraphic3><BR clear=3Dleft><B>Code snippet =
for=20
displayLoginErrorScreen shown below.</B> </P>
<P style=3D"FONT-STYLE: normal" align=3Dleft><IMG height=3D234=20
src=3D"http://www.tusc.com.au/tutorial/images/chap8/DisplayLoginError.png=
"=20
width=3D570 align=3Dleft border=3D0 name=3DGraphic22><BR =
clear=3Dleft><FONT=20
color=3D#000000><B>All these helper method are being used by the =
processRequest=20
method, which is acting as a Controller and dispatches the request to =
the=20
appropriate view, such as the login screen or display items=20
screen.</B></FONT></P>
<P>Now our servlet is complete, as implementation of the rest of the =
methods is=20
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 &gt; right click &gt; select Deploy =
module=20
as shown below , make sure the server is running</B>.</P>
<P><IMG height=3D360=20
src=3D"http://www.tusc.com.au/tutorial/images/chap8/DeployWebModule.png" =
width=3D296=20
align=3Dleft border=3D0 name=3DGraphic23><BR clear=3Dleft><B>Deployment =
status appears=20
on the console as shown below. If deployment is successful then test =
your=20
servlet.</B></P>
<P><IMG height=3D226=20
src=3D"http://www.tusc.com.au/tutorial/images/chap8/ConsolesDeployView.pn=
g"=20
width=3D844 align=3Dleft border=3D0 name=3DGraphic24><BR =
clear=3Dleft><BR><BR></P>
<H3>Test your Servlet :</H3>
<P><B>Go to your browser and access the servlet using following URL,=20
'http://localhost:8080/OnlineStore/access'</B></P>
<P><B>where 'access' is the url mapping that was assigned while creating =
the=20
servlet using the Servlet Creation Wizard, and OnlineStore is the web=20
module,where this servlet resides.</B></P>
<P><B>The login screen will be displayed. Enter username as 'ANDY' and =
password=20
as 'PASSWD'. The next screen will be list of items in MyStore as shown=20
below.</B></P>
<P><IMG height=3D291=20
src=3D"http://www.tusc.com.au/tutorial/images/chap8/ServletResult.png" =
width=3D601=20
align=3Dleft border=3D0 name=3DGraphic26><BR clear=3Dleft><BR><BR></P>
<P><B>We have successfully created a servlet, now let's access this =
catalogue=20
via a JSP page.</B></P>
<H3>Create JSP Page :</H3>
<P style=3D"MARGIN-BOTTOM: 0in"><B>Go To Package Explorer &gt; Expand =
Web Module=20
node (OnlineStore) &gt; right click and a menu will pop up.</B></P>
<P><B>On the pop up menu &gt; New &gt; Select Lomboz JSP Wizard as shown =

below.</B></P>
<P><IMG height=3D290=20
src=3D"http://www.tusc.com.au/tutorial/images/chap8/LombozJSPWizard.png" =
width=3D610=20
align=3Dleft border=3D0 name=3DGraphic28><BR clear=3Dleft><B>Add file =
name showItems.jsp=20
as shown below.</B></P>
<P><IMG height=3D586=20
src=3D"http://www.tusc.com.au/tutorial/images/chap8/CreateJSPWizardFirst.=
png"=20
width=3D628 align=3Dleft border=3D0 name=3DGraphic4><BR =
clear=3Dleft><B>Press Next &gt;=20

⌨️ 快捷键说明

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