国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Servlet和JavaServerPages的集成應用

2019-11-18 12:32:12
字體:
來源:轉載
供稿:網友

  本文描述了通過使用 servlet 和 javaServer Pages來集成瘦客戶機與 MQSeries。這種集成需要用戶具體填寫 Html 表單,并從該表單中收集用戶數據然后通過一個消息隊列將數據發送到后端應用程序。接下來后端應用程序將處理該表單數據并通過該消息隊列發回一個應答。該應答需要在瀏覽器中顯示。
  
  本文討論了瘦客戶機、servlet 及消息隊列間的交互,并演示了如何在該解決方案中使用 IBM 提供的不同產品。讀者應該了解Java語言并熟悉 WebSphere 和 MQSeries。
  
  體系結構概述 下圖給出了所建議的解決方案體系結構。它涉及三層方法。
  
Servlet和JavaServerPages的集成應用


  處理流程
  
  用戶填寫 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
  ("/AddressOutput

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 偏关县| 宿迁市| 清原| 陆良县| 临沭县| 呼伦贝尔市| 老河口市| 景泰县| 渭源县| 云和县| 阳曲县| 友谊县| 永善县| 宁夏| 湄潭县| 沅陵县| 泉州市| 樟树市| 淳安县| 文水县| 申扎县| 当涂县| 丰原市| 东乡县| 黄龙县| 方山县| 营口市| 紫金县| 贞丰县| 鄱阳县| 苍南县| 进贤县| 莱州市| 闻喜县| 永清县| 青神县| 土默特左旗| 庆元县| 开封县| 若尔盖县| 锦州市|