Servlet和JavaServerPages的集成应用

ZDNet软件频道 时间:2008-06-29 作者: | 中国IT实验室 我要评论()
本文关键词:JavaServerPages Servlet JSP
 本文描述了通过使用 servlet 和 JavaServer Pages来集成瘦客户机与 MQSeries。

本文描述了通过使用 Servlet 和 JavaServer Pages来集成瘦客户机与 MQSeries。这种集成需要用户详细填写 HTML 表单,并从该表单中收集用户数据然后通过一个消息队列将数据发送到后端应用程序。接下来后端应用程序将处理该表单数据并通过该消息队列发回一个应答。该应答需要在浏览器中显示。
  
  本文讨论了瘦客户机、Servlet 及消息队列间的交互,并演示了如何在该解决方案中使用 IBM 提供的不同产品。读者应该了解Java语言并熟悉 WebSphere 和 MQSeries。
  
  体系结构概述 下图给出了所建议的解决方案体系结构。它涉及三层方法。
  


  处理流程
  
  用户填写 HTML 表单。 将表单发送给 ServletServlet 将该 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


百度大联盟认证黄金会员Copyright© 1997- CNET Networks 版权所有。 ZDNet 是CNET Networks公司注册服务商标。
中华人民共和国电信与信息服务业务经营许可证编号:京ICP证010391号 京ICP备09041801号-159
京公网安备:1101082134