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

📄 chapter8.htm

📁 使用eclipse 开发 j2ee应用的教程
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0047)http://www.tusc.com.au/tutorial/html/chap8.html -->
<HTML><HEAD><TITLE>Chapter8 - Creating Web Clients</TITLE>
<META http-equiv=CONTENT-TYPE content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2800.1400" name=GENERATOR>
<META content="Glen McCallum and Vishal Sharma" name=AUTHOR>
<META content=20030729;9513900 name=CREATED>
<META content=Vishal name=CHANGEDBY>
<META content=20031013;14430800 name=CHANGED>
<META 
content="Tutorial for building J2EE Applications using Eclipse, Lomboz, Jboss and Xdoclet." 
name=DESCRIPTION>
<META 
content="Tutorial, J2EE, Lomboz, Eclipse, Jboss, Xdoclet, J2EE Patterns, EJB, Servlets, JSP, BMP, CMP, Session, Configuration, DAO" 
name=KEYWORDS>
<META content="14 days" name=revisit-after><!-- Meta tags --><!-- Copyright 漏 2003 TUSC. All rights reserved.--><!--- Header --></HEAD>
<BODY lang=en-US dir=ltr>
<FORM name=searchForm action=../../../../search/index.php method=post>
<TABLE style="PAGE-BREAK-BEFORE: always" cellSpacing=0 cellPadding=4 
width="100%" bgColor=#0a6a91 border=0>
  <TBODY>
  <TR>
    <TD vAlign=top width=154>
      <P><A 
      href="http://www.tusc.com.au/news_events/event_details.php?id=44"><IMG 
      alt=TUSC src="chapter8.files/tuscJ2eeLogo.gif" align=bottom border=0 
      name=Graphic1></A></P></TD>
    <TD vAlign=top width=276>
      <P><IMG height=73 alt="Reliable, On-Time Delivery." 
      src="chapter8.files/reliable.gif" width=268 align=bottom border=0 
      name=Graphic2><!--search table --></P></TD>
    <TD width=21></TD>
    <TD width=43>
      <P><IMG height=9 alt=Search src="chapter8.files/search.gif" width=41 
      align=bottom border=0 name=Graphic3></P></TD>
    <TD width=119>
      <P><INPUT maxLength=50 size=12 name=KEYWORD></P></TD>
    <TD width=31>
      <P><INPUT type=image height=18 width=29 src="chapter8.files/go.gif" 
      align=bottom name=go><INPUT type=hidden value=Y name=SECTION_ALL></P></TD>
    <TD vAlign=top width=50>
      <P><IMG height=73 alt=Spacer src="chapter8.files/invis.gif" width=50 
      align=bottom border=0 name=Graphic5></P></TD>
    <TD vAlign=top width=178>
      <P><A href="http://www.logec.com/"><IMG alt="LOGEC's Event Correlation" 
      src="chapter8.files/logec.gif" align=bottom border=0 name=Graphic57></A> 
      </P></TD>
    <TD width=14>
      <P><IMG height=10 alt=spacer src="chapter8.files/invis.gif" width=14 
      align=bottom border=0 name=Graphic58> </P></TD></TR></TBODY></TABLE></FORM>
<H2 align=center><!--- Header finished -->Tutorial for building J2EE 
Applications using JBOSS and ECLIPSE <IMG height=2 
src="chapter8.files/redrulr5.gif" width=775 align=middle border=0 
name=Graphic1></H2>
<P><BR><BR></P>
<H1>Chapter 8. </H1>
<P><IMG height=2 src="chapter8.files/blurulr6.gif" width=785 align=middle 
border=0 name=Graphic2></P>
<H2>Creating Web Clients</H2>
<P>This chapter describes how to create web clients in the Client 
Tier/Presentation Tier to access or otherwise communicate with the Business 
Tier. Servlets and JSP pages are the two examples of web clients which will be 
created. In a typical Model View Controller Pattern, the Servlet acts as the 
Controller whilst JSP pages act as the View (the Model being the data, of 
course).</P>
<P><IMG height=210 src="chapter8.files/J2EECaseStudyFlowDiagram.png" width=707 
align=left border=0 name=Graphic6><BR clear=left><BR><BR></P>
<H3>Tasks :</H3>
<OL>
  <LI>
  <P>Create a Servlet named AccessController under the package 
  au.com.tusc.servlet.</P>
  <LI>
  <P><FONT color=#000000>Add a business method to StoreAccess Bean named 
  getAllItems() with the following signature</FONT></P>
  <P><B><FONT color=#000000>public</FONT> <FONT 
  color=#000000>java.util.ArrayList</FONT> <FONT 
  color=#000000>getAllItems()</FONT></B></P>
  <LI>
  <P>Implement the init method.</P>
  <LI>
  <P>Implement doGet and doPost methods.</P>
  <LI>
  <P>Add a method processRequest with the following signature</P>
  <P><B>protected void processRequest (HttpServletRequest 
  request,HttpServletResponseresponse)</B></P>
  <P><B>throwsServletException, IOException </B></P>
  <LI>
  <P>Implement the processRequest method.</P>
  <LI>
  <P>Deploy the AccessController Servlet.</P>
  <LI>
  <P>Test the AccessContoller Servlet.</P>
  <LI>
  <P><FONT color=#000000><FONT face="Bitstream Vera Serif">Create a JSP Page 
  named showItems.</FONT></FONT></P>
  <LI>
  <P>Modify the method processRequest in Servlet AccessController.</P>
  <LI>
  <P><FONT color=#000000><FONT face="Nimbus Roman No9 L"><FONT 
  face="Bitstream Vera Serif">Add HTML and JSP tags to display a list of all 
  items in MyStore.</FONT> </FONT></FONT></P>
  <LI>
  <P>Deploy the module OnlineStore.</P>
  <LI>
  <P><FONT color=#000000><FONT face="Bitstream Vera Serif">Test the 
  showItems.jsp page.</FONT></FONT></P></LI></OL>
<H3 style="MARGIN-TOP: 0in; MARGIN-BOTTOM: 0in"><BR></H3>
<H3 style="MARGIN-TOP: 0in; MARGIN-BOTTOM: 0in"><B>Create AccessController 
Servlet :</B></H3>
<P style="MARGIN-BOTTOM: 0in"><BR></P>
<P><B>Go To Package Explorer &gt; Expand Mystore (project) node &gt; select src, 
right click and a menu will pop up.</B></P>
<P><B>On the pop up menu &gt; New &gt; Lomboz Servlet Wizard as shown 
below.</B></P>
<P><IMG height=414 src="chapter8.files/LombozServletWizard.png" width=536 
align=left border=0 name=Graphic7><BR clear=left><BR><BR></P>
<P><B>Enter the package name au.com.tusc.servlet, with servlet name 
AccessController and select four methods to be implemented in the servlet as 
shown.</B></P>
<P><IMG height=542 src="chapter8.files/CreateServletWizard.png" width=792 
align=left border=0 name=Graphic8><BR clear=left><BR><BR></P>
<P><B>Press Next. A new screen will appear as shown below. Add Web Module 
(Browse.. web module and it will show list of web modules in this project), in 
this case it is OnlineStore, so select that. Enter the Servlet name</B> <B>as 
access, and Mapping URL as ' /access/* '</B>.</P>
<P><IMG height=566 src="chapter8.files/CreateServletWizardNext.png" width=812 
align=left border=0 name=Graphic9><BR clear=left><B>Press Finish.</B></P>
<P><B>This will create a package named au.com.tusc.servlet under src and 
AccessController within that package as shown below.</B></P>
<P><IMG height=506 src="chapter8.files/AccessControllerServlet.png" width=668 
align=left border=0 name=Graphic10><BR clear=left><B>As can be seen from the 
figure above the four methods we selected in the wizard are created, and only 
their implementation is required.</B></P>
<P><B>Apart from this, some descriptors are generated in web.xml, under 
Web-Module OnlineStore/WEB-INF as shown below:</B></P>
<P><IMG height=548 src="chapter8.files/webxml.png" width=458 align=left border=0 
name=Graphic11><BR clear=left><B>These tags are generated by the Servlet 
Creation Wizard, where &lt;url-pattern&gt; tag specifies the path name by which 
the servlet will to be accessed. In this case it will be 
http://localhost:8080/OnlineStore/access. (N.B. - It's not necessary to have the 
same &lt;servlet-name&gt; and &lt;url-pattern&gt;.)</B></P>
<P><B>Web.xml contains all the deployment descriptors required for deployment of 
servlets</B>.</P>
<P>Lomboz creates two pages when you create your web module, index.jsp and 
error.jsp.</P>
<P>Before we go further and start getting our hands dirty with servlets, let's 
have a look at what directories and files are generated by Lomboz under Web 
Modules, in this case OnlineStore, as shown below.</P>
<P><IMG height=290 src="chapter8.files/GenertedWebModule.png" width=202 
align=left border=0 name=Graphic12><BR clear=left>Files of interest include 
web.xml, where all the deployment descriptors will be placed (as discussed 
above), and targets.xml, which contains information about the server in which it 
will be deployed (in this case JBOSS). See the code snippet below from 
targets.xml.</P>
<P><IMG height=54 src="chapter8.files/targetsxml.png" width=320 align=left 
border=0 name=Graphic13><BR clear=left><BR><BR></P>
<H3>Add Business Method :</H3>
<P><B>Before we start implementing our servlet, add one more business method to 
StoreAccess Bean called getAllItems() with the following signature:</B></P><PRE style="MARGIN-BOTTOM: 0.2in"><FONT face=Sans><FONT size=3><FONT color=#000000>public</FONT> <FONT color=#000000>java.util.ArrayList</FONT> <FONT color=#000000>getAllItems()</FONT></FONT></FONT></PRE>
<P><FONT color=#000000><B>This method will return all the items in MyStore by 
invoking the finder method in ItemsLocalHome named findAll, as shown below (code 
snippet from StoreAccess bean):</B></FONT></P>
<P><IMG height=390 src="chapter8.files/GetAllItems.png" width=532 align=left 
border=0 name=Graphic16><BR clear=left><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 
when the servlet is first created and is <U>not</U> called again for each user 
request.</B></P>
<P><B>We will cache the references for our StoreAccess Bean in this method, as 
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 
by looking up the StoreAccess bean via the JNDI API.</B></P>
<P><B>Add a variable storeAccessHome of type StoreAccessHome to store the 
reference obtained by narrowing the object.</B></P>
<P><FONT color=#000000><B>Create helper methods for getting Context, Home and 
assigning the reference to storeAccessHome as shown below</B>. </FONT></P>
<P><IMG height=580 src="chapter8.files/init.png" width=582 align=left border=0 
name=Graphic15><BR clear=left><FONT color=#800000><I>Note : We have to narrow 
this object because we are accessing a remote interface. If it was a local 
interface, we wouldn't need to narrow the 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 
provide the functionality for both of them. The request is delegated to this 
method where all processing of information takes place. Once this business logic 
processing is complete it dispatches the request to the appropriate view for 
display, ie. JSP pages. </B></P>
<P><FONT color=#800000><I>Note : This approach is based on the Front Controller 
pattern, where the controller acts as a central point of contact for handling 
requests, delegating business processing, and coordinating with dispatcher 
components, whilst dispatchers are responsible for view management and 
navigation. This pattern suggests centralizing the handling of all requests in 
this way, but also allows for different handler methods to be used in processing 

⌨️ 快捷键说明

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