Tutorial for building J2EE Applications using JBOSS and ECLIPSE -8

原创 2004年08月30日 17:38:00

Chapter 8.

blurulr6.gif

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

J2EECaseStudyFlowDiagram.png


Tasks :

  1. Create a Servlet named AccessController under the package au.com.tusc.servlet.

  2. Add a business method to StoreAccess Bean named getAllItems() with the following signature

    public java.util.ArrayList getAllItems()

  3. Implement the init method.

  4. Implement doGet and doPost methods.

  5. Add a method processRequest with the following signature

    protected void processRequest (HttpServletRequest request,HttpServletResponseresponse)

    throwsServletException, IOException

  6. Implement the processRequest method.

  7. Deploy the AccessController Servlet.

  8. Test the AccessContoller Servlet.

  9. Create a JSP Page named showItems.

  10. Modify the method processRequest in Servlet AccessController.

  11. Add HTML and JSP tags to display a list of all items in MyStore.

  12. Deploy the module OnlineStore.

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

LombozServletWizard.png


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

CreateServletWizard.png


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 Mapping URL as ' /access/* '.

CreateServletWizardNext.png
Press Finish.

This will create a package named au.com.tusc.servlet under src and AccessController within that package as shown below.

AccessControllerServlet.png
As can be seen from the figure above the four methods we selected in the wizard are created, and only their implementation is required.

Apart from this, some descriptors are generated in web.xml, under Web-Module OnlineStore/WEB-INF as shown below:

webxml.png
These tags are generated by the Servlet Creation Wizard, where <url-pattern> 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 <servlet-name> and <url-pattern>.)

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

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

targetsxml.png


Add Business Method :

Before we start implementing our servlet, add one more business method to StoreAccess Bean called getAllItems() with the following signature:


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

GetAllItems.png


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

init.png
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 procesRequest with the following signature:

                                 

Delegate request from doGet and doPost method to this method as shown below.

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

Add following attributes shown below, for building different urls.

Attributes.png
Create a session object and get value from useraction parameter of request object as shown below in code snippet.

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

DisplayLoginScreen.png
This method calls displayLoginDataFields to generate the data fields for form submission as shown below.

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

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

DisplayAllItems.png
Code snippet for displayLoginErrorScreen shown below.

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

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.

DeployWebModule.png
Deployment status appears on the console as shown below. If deployment is successful then test your servlet.

ConsolesDeployView.png


Test your Servlet :

Go to your browser and access the servlet using following URL, 'http://localhost:8080/OnlineStore/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.

ServletResult.png


We have successfully created a servlet, now let's access this catalogue 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.

LombozJSPWizard.png
Add file name showItems.jsp as shown below.

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

CreateJSPWizardSecond.png
Press Ok. Now selected JSP details can be seen after selecting various options as shown below.

CreateJSPWizardThird.png

Press Finish. A new file named showItems.jsp will be created under the web module OnlineStore as shown below.



ShowItemsCreated.png


Modify Method processRequest in Servlet AccessController :

Now, in order to display all items of MyStore from a JSP page, we have to modify the proccessRequest method in servlet AccessController.

Modify String ITEMS_SCREEN to "/showItems.jsp" as shown below.

StringShowItems.png
Comment out the call to method displayAllItems in processRequest.

Add a Session attribute named itemsList with values 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.

ProcessRequest.Modified.png

Add Html and JSP Tags :

First import the following packages: au.com.tusc.cmp.*, java.util.ArrayList and java.util.Iterator.

Now add the necessary 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.

ShowItemsImplemented.png
Note : This JSP Page is a view in the Front Controller Pattern discussed above.



Now the JSP page is done.

Go to OnlineStore (web module) node > right click > select Lomboz J2EE.. on pop up menu > Check All JSP Syntax.

See if there are any errors.

Go to OnlineStore node > right click > select Lomboz J2EE.. on pop up menu > Add WEB-INF/lib JARs to the Classpath as shown below.

AddWeb-INF.png


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

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.

RunShowItemJSP.png


You have successfully created a JSP page using all the J2EE components, and they have been created and deployed successfully. Congratulations!

Tutorial_for_building_J2EE_Applications_using_JBOSS_and_ECLIPSE

  • 2007年04月13日 07:37
  • 6.47MB
  • 下载

Tutorial.for.building.J2EE.Applications.using.JBOSS.and.ECLIPSE

  • 2011年07月27日 10:00
  • 5.67MB
  • 下载

Building Applications using J2EE 1.4.pdf

  • 2009年01月09日 09:25
  • 224KB
  • 下载

Building Scalable and High-performance Java Web Applications Ssing J2EE Technology.pdf

  • 2007年03月15日 16:43
  • 5.18MB
  • 下载

Win 8 系统下 JBoss AS 7 Final 的下载配置与使用——Windows J2EE开发安装序列之五

一、JBOSS的下载与安装 首先进入官网下载JBoss :http://www.jboss.org/jbossas/downloads 选择最新版本,进行下载。 解压到指定目录 配置环...
  • chenbean
  • chenbean
  • 2014年03月31日 21:49
  • 1349

Eclipse+Lomboz+JBoss搭建J2EE开发环境记录

  • 2008年04月07日 15:19
  • 2.18MB
  • 下载

Eclipse Juno(j2ee)开发环境下JBoss热部署的实现方式(暴露式部署,WTP)

如果对部署方式有疑问,请先看这个帖子: JBoss和Tomcat部署方式的区别(解释暴露式部署) 我们说一下Eclipse J2ee版本(下个WTP插件也行)自带的run on server功能,支...
  • wwwcomy
  • wwwcomy
  • 2015年10月13日 15:05
  • 578

Eclipse集成 Lomboz和JBoss开发J2EE

  • 2011年10月29日 21:50
  • 351KB
  • 下载

J2Ee Jboss Ejb With Eclipse 2003.rar

  • 2008年06月08日 19:37
  • 1.69MB
  • 下载

Eclipse3_0+jboss4Lomboz 3开发J2EE

  • 2008年04月04日 19:56
  • 741KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Tutorial for building J2EE Applications using JBOSS and ECLIPSE -8
举报原因:
原因补充:

(最多只允许输入30个字)