Tutorial for building j2ee applications using jboss and eclipse -8

xiaoxiao2021-03-06  81

Chapter 8.

Creating Web Clients

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).

Tasks:

Create a Servlet named AccessController under the package au.com.tusc.servlet. Add a business method to StoreAccess Bean named getAllItems () with the following signature public java.util.ArrayList getAllItems () Implement the init method. Implement doGet and doPost methods . Add a method processRequest with the following signature protected void processRequest (HttpServletRequest request, HttpServletResponseresponse) throwsServletException, IOException Implement the processRequest method. Deploy the AccessController Servlet. Test the AccessContoller Servlet. Create a JSP Page named showItems. Modify the method processRequest in Servlet AccessController Test The showItems.jsp page. Test The showItems.jsp page.

Create AccessController Servlet:

Go to Package Explorer> Expand MyStore (Project) Node> SELECT SRC, RIGHT CLICK AND A MENU WILL POP UP.

On The Pop Up Menu> New> Lomboz Servlet Wizard As Shown Below.

Enter the package name au.com.tusc.servlet, with servlet name accesscontroller and select four methods to be used in the servlet as shown.

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 as Access, and maping url as '/ access / *' .press finish.

This Will Create a package named au.com.tusc.servlet Under Src and AccessController WITHIN THAT PACKAGE AS Shown Below.

AS Can Be Seen from the Figure, And Only Their Implementation is Required.

Apart from this, Some Descriptors Are Generated in Web.xml, Under Web-Module Online / WEB-INF AS SHOWN BELOW:

These tags are generated by the Servlet Creation Wizard, where tag specifies the path name by which the servlet will to be accessed In this case it will be http:. // localhost: 8080 / OnlineStore / access (NB. - It's not necessary to have the same example and .)

Web.xml Contains All The Deployment Descriptors Required for Deployment of Servlets.

Lomboz Creates Two Pages When You Create Your Web Module, Index.jsp and Error.jsp.

Before we go further and start getting company and flys defirst What Directories and Files Are Generated by Lomboz Under Web Modules, In this case Onlinestore, AS Shown Below.

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.

Add Business Method:

Before We Start Implementing Our Servlet, Add One More Business Method To StoreAccess Bean Called getAllItems () with the following signature: public java.util.ArrayList getAllItems ()

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):

Now Let's Start Implementing The Generated Methods in The Servlet.

Implement init method:

This Method Is Responsible for Initializing Servlets, So It Is Invoked When The Servlet Is First Created and is Not Called Again for Each User Request.

WE WILL Cache The References for Our StoreAccess Bean in this Method, as all the client interfaces Available Aree Exposed in StoreAccess.

So, First Create a Context. Then Get a Reference To A StoreAccess Object By Looking Up The StoreAccess Bean Via The JNDI API.

Add A Variable StoreAccessHome of Type StoreAccessHome To Store The Reference Obtained by Narrowing The Object.

Create Helper Methods for getting context, home and assigning the reference to storeaccessHome as shown below.

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.

Implement Methods Doget and Dopost:

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.

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 Different Request Types.add a Method Named ProceSreme:

Protected Void ProcessRequest (httpservletRequest Request, HttpservletResponse Response)

Throws ServleTexception, IOException

DELEGATE REQUEST from doget and dopost method to this method as shown bell.

Now Implement The ProcessRequest () Method.

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 ........................ ..

Add Following Attributes Shown Below, for building Different URLs.

Create a session Object and get value from UseAction Parameter of Request Object As Shown Below In Code Snippet.

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. Code snippet for displayLoginScreen is shown below.

This Method Calls DisplayLogindatafields To Generate The Data Fields for Form Submission As Shown Below.

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.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.

Code Snippet for DisplayLoginerrorScreen Shown Below.

All The ProcessReueSt Method, Which IS Acting As a Controller and Display Item Screen.

NOW OUR Servlet is Complete, As IMPLEMENTATION OF THE REST OF THE METHODS IS LEFT AS AN EXERCISE.

Deploy AccessController Servlet:

In Order to Deploy Your Servlet, Go to Lombozj2ee View.

Select Web Module OnlineStore> Right Click> Select Deploy Module As Shown Below, Make Sure The Server Is Running.

Deployment Status Appears on The Console As Shown Below. If Deployment Is Successful The Test Your Servlet.

Test Your Servlet:

Go to your browser and access the servlet using following URL, 'http: // localhost: 8080 / onlineestore / access'

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.

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.

WE HAVE SUCCESSFULLY CREATED A Servlet, Now Let's Access this Catalog Via A JSP Page.

CREATE JSP Page:

Go to Package Explorer> Expand Web Module Node (Onlinestore)> Right Click and A Menu Will Pop Up.on The Pop Up Menu> New> Select Lomboz JSP Wizard As Shown Below.

Add file name showitems.jsp As Shown Below.

Press next> Menu, The Form Set JSP Details Will Appear> Select Add .. under beans section as shown below.

The Menu for select Beans Will Appear> Add ID As ItemData, Scope As page and class as itemdata.

Press OK. Now SELECTED JSP Details Can Be Seen After Selecting Various Options As Shown Below.

....................... ..

Modify Method ProcessRequest In Servlet AccessController:

Now, in Order to Display All Items of MyStore from a JSP page, We Have to Modify The ProcchesRequest Method In Servlet AccessController.

Modify string items_screen to "/showItems.jsp" as shown below.

Comment Out The Call To Method DisplayAllItems in ProcessRequest.

Add a session attribute named itemslist with value of listitems.

Add A Request Dispatcher and Pass Buildurl As an Argument, Which Has The Url for the showItems.jsp page.

Forward to That Request Dispatcher to Draw the JSP page.

Code Snippet of The Modified ProcessRequest Method Is Shown Below.

Add HTML and JSP Tags:

First Import The Following Packages: au.com.tusc.cmp. *, Java.util.Arraylist and java.util.itrator.

Now Add the nextary HTML and JSP Tags to Access Items of MyStore.

Get Itemslist from Our session Attribute Set in Servlet AccessController.

Set itemdata as a page attribute via pagecontext.

Now Display The Attributes of ItemData Using JSP Property Access Tags. Code Snippet for the showItems page is shown below.

Note: This JSP Page IS A View in The Front Controller Pattern Discussed Above.Now The JSP page is done.

Go to onlineestore (Web Module) Node> Right Click> SELECT LOMBOZ J2EE .. on POP Up Menu> Check All JSP Syntax.

See if........

Go to OnlineStore Node> Right Click> SELECT LOMBOZ J2EE .. on pop up menu> add web-inf / lib jars to the classpath as shown below.

Deploy Module Onlinestore:

In Order to Deploy, Go to Lombozj2ee View.

Select Web Module OnlineStore> Right Click> Select Deploy Module, make sure the server is running.

DEPLOYMENT STATUS WILL BE Visible on the console. If Deployment is successful the test your jsp page.

Test Your JSP Page:

Go to your browser and access the servlet Using the following web address, 'http: // localhost: 8080 / onlinestore / access'

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.

.

You Have SuccessFully Created a JSP Page Using All The J2EE Components, and The Have Been Created and Deployed SuccessFully. Congraturations!

转载请注明原文地址:https://www.9cbs.com/read-121907.html

New Post(0)