erp|server|servlet
本文描述了通過使用 servlet 和 JavaServer Pages來整合瘦客戶機與 MQSeries。這種整合需要使用者詳細填寫 HTML 表單,並從該表單中收集使用者資料然後通過一個訊息佇列將資料發送到後端應用程式。接下來後端應用程式將處理該表單資料並通過該訊息佇列發回一個應答。該應答需要在瀏覽器中顯示。
本文討論了瘦客戶機、servlet 及訊息佇列間的互動,並示範了如何在該解決方案中使用 IBM 提供的不同產品。讀者應該瞭解Java語言並熟悉 WebSphere 和 MQSeries。
體繫結構概述 下圖給出了所建議的方案架構。它涉及三層方法。
處理流程
使用者填寫 HTML 表單。 將表單發送給 servlet。 servlet 將該 HTTP 要求轉換成一個 MQSeries 訊息,並將其放入一個隊列。 後端應用程式處理該訊息,然後通過訊息佇列發回一個應答。 servlet 從隊列中檢索訊息,並將其存放在一個 Java Bean 中。 然後 servlet 調用編譯過的 JavaServer Page 並動態產生結果 HTML 頁面。 JSP 從 Java Bean 檢索該頁面的訊息內容,將其合并到 HTML,然後將結果頁面回顯在瀏覽器上。
該解決方案利用了下列技術:
HTML/HTTP、Java - servlet、Java Beans 和 JavaServer Page、Web 服務器、訊息佇列
該解決方案整合了下列產品:
Netscape 4.0/Internet Explorer 3.0 或更高版本 、IBM HTTP Server 3.0 、WebSphere 2.02 、JDK 版本 1.1.7 、MQSeries 版本 5.0
串連至 MQSeries
我們選擇 servlet 模型是因為該模型相對於 CGI 有許多優點。servlet 是擴充了 Web 服務器的功能的標準伺服器端 Java 應用程式。servlet 完全運行在 Web Server 上,不會將任何東西下載到瀏覽器。在裝入期間或初始請求期間會將 servlet 裝入伺服器的地址空間。在初始請求之後,servlet 非常迅速地作出響應。servlet 的 init 方法為 servlet 的運行做好了準備。每個 servlet 裝入只調用一次 init 方法。在 init 方法中,按如下建立到 MQSeries 隊列管理器的串連:
public void init(ServletConfig config)
throws ServletException {
super.init(config);
try {
//Create a connection to the queue manager
qMgr = new MQQueueManager("NC.QManager");
}
catch (MQException ex)
{
System.out.println
("An MQ error occurred in init(): Completion code "
+ ex.completionCode +
" Reason code" + ex.reasonCode);
try
{
if (qMgr != null)
//Disconnect from the queue manager
qMgr.disconnect();
}
catch (MQException e)
{
System.out.println("An MQ error occurred "
+ "in init() while disconnecting:" + " Completion code " +
e.completionCode + " Reason code" + e.reasonCode);
}
}
}
由於只須建立一次到 MQSeries 的隊列管理器的串連,並且建立串連需要很長時間,因此 init 方法是執行這一過程的理想位置。然後對該 servlet 的後繼調用會執行得更快。WebSphere 也允許使用者通過使用管理 GUI 預先裝入 servlet,因此隨著隊列管理器串連的建立,servlet 做好了準備,接下來就等著傳遞任何訊息。
如果在 init 方法中捕獲到 MQException,則上述代碼會與隊列管理器中斷連線。結果是,為了建立與隊列管理器的串連,使用者將不得不重新裝入 servlet。
為了使 servlet 與 MQSeries 對話,必須使用 MQSeries Bindings for Java。MQSeries Bindings for Java 使您能夠用 Java 語言編寫 MQSeries 應用程式。這些應用程式直接與 MQSeries 隊列管理器通訊以提供高生產率、高效能開發選項。它們使用 Java 本機方法直接調用到現有的隊列管理器 API 而不是通過 MQSeries 伺服器串連通道進行通訊;這為 Java MQSeries 應用程式提供了更佳效能。在代碼中我們必須匯入“com.ibm.mqbind.*”包。MQSeries 的 java 類也應該位於 WebSphere 的類路徑中;這將允許 WebSphere 應用程式伺服器定位 MQSeries Bindings for Java 包。
建立 MQ 訊息
由於 BillingAddressServlet 主要用於處理來自 AddressInputForm 的 HTTP 要求,我們對 HttpServlet 產生子類。 GenericServlet 的這個抽象子類實現了用於處理請求的預設 service() 方法。覆蓋的最常見方法包括 doGet() 和 doPost()。下面的樣本將覆蓋 doPost() 方法。AddressInputForm 通過下面的調用來調用這個 doPost() 方法:
<FORM METHOD=POST ACTION="/servlet/BillingAddressServlet">
POST 請求發送一個 HTTP 訊息主體,該訊息主體含有發給 servlet 的所有資料。然後 doPost() 方法從輸入中建立下列訊息字串:
String tempAddress = "Input information is";
res.setContentType("text/plain");
..............
if ("application/x-www-form-urlencoded".equals(req.getContentType()))
{
System.out.println("In doPost()");
Enumeration enum = req.getParameterNames();
while (enum.hasMoreElements())
{
String name = (String) enum.nextElement();
String values = req.getParameter(name);
if(values != null) {
tempAddress = tempAddress + "; " + name + ": " + values;
}
}
發送訊息
然後通過使用下列代碼調用 putOrder() 方法,doPost() 方法將訊息字串放入訊息佇列:
public void
putOrder(String tempAddress) {
try {
int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT;
//Specify the queue that we wish to open, and the open options.
MQQueue ncOrderDataQ = qMgr.accessQueue("NC.OrderCreateQ",
openOptions,
qManager,
null, // no dynamic q name
null); // no alternate user id
//Define a MQ message
MQMessage customerAddress = new MQMessage();
customerAddress.writeUTF(tempAddress);
//specify the message options
MQPutMessageOptions pmo = new MQPutMessageOptions();
//put the message on the queue
ncOrderDataQ.put(customerAddress, pmo);
//Close the queue
ncOrderDataQ.close();
}
catch .........
檢索訊息
要檢索由後端應用程式返回的訊息,doPost() 方法調用 orderUpdateStatus() 方法。orderUpdateStatus() 方法使用下列代碼檢索隊列中的訊息:
public String orderUpdateStatus() {
String msgText = null;
try {
int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT;
//Specify the queue that we wish to open, and the open options.
MQQueue ncOrderUpdateQ = qMgr.accessQueue("NC.UpdateQ",
openOptions,
qManager,
null, // no dynamic q name
null); // no alternate user id
//create a new get the message
MQMessage retrievedMessage = new MQMessage();
retrievedMessage.messageId = MQC.MQMI_NONE;
//set the get message options
MQGetMessageOptions gmo = new MQGetMessageOptions();
//get the message off the queue
ncOrderUpdateQ.get(retrievedMessage, gmo);
//Display the message
msgText =
retrievedMessage.readString(retrievedMessage.getMessageLength()); //for NC.UpdateQ
//Close the queue
ncOrderUpdateQ.close();
}
catch .........
調用 JSP
最後,putOrder() 方法調用 performTask() 方法,以將訊息儲存在 BillingAddress Bean 中。然後 servlet 調用 AddressOutputPage JSP,並將 BillingAddressBean 的控制代碼傳給它。JSP 抽取出動態內容(即,來自 Java Bean 的訊息)並將它們合并進顯示在瀏覽器上的 HTML 頁面中。
public void performTask(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
java.lang.String returnMessage) {
try
{
// instantiate the bean
BillingAddressBean myBillingAddressBean = new BillingAddressBean();
// set the return message in the bean
myBillingAddressBean.setMQReturnMessage(returnMessage);
// store the bean in the request so it can be accessed by
pages which are accessed with callPage()
((com.sun.server.http.HttpServiceRequest)request).setAttribute
("BillingAddressBean", myBillingAddressBean);
// Call the output page
((com.sun.server.http.HttpServiceResponse)response).callPage
("/AddressOutputPage.jsp", request);
}
catch .........
結束語
在這一解決方案裡,servlet 和 JSP 的結合允許將功能邏輯與顯示分離。servlet 充當控制器,而 JSP 充當結果檢視。同樣,使用 JSP 和 Java Bean 清晰劃分了內容顯示和內容本身。通過使用 servlet init 方法為 servlet 運行做好準備,例如,建立到隊列管理器的串連,改進了效能。WebSphere 允許使用者預先裝入 servlet 以便能夠在使用者使用 servlet 之前在初始化期間就完成如建立串連等耗時的任務。通過使用訊息和隊列,MQSeries 使不同的應用程式能夠相互整合。