分享
 
 
 

Tutorial for building J2EE Applications using JBOSS and ECLIPSE (8)

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

Chapter 8.

Creating Web ClientsThis 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.

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

Deploy the module OnlineStore.

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 implemented 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 Mapping 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 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:

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.

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

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:

protected void processRequest (HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException

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

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.

Create a session object and get value from useraction 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 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.

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

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.

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.

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.

Press Finish. A new file named showItems.jsp will be created under the web module OnlineStore 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 proccessRequest 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 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.

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.

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.

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.

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有