站内搜索: 请输入搜索关键词

当前页面: 开发资料首页JSP 专题Servlet和JavaServerPages的集成应用

Servlet和JavaServerPages的集成应用

摘要: Servlet和JavaServerPages的集成应用


  本文描述了通过使用 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 使不同的应用程序能够相互集成。

</td> </tr> <tr> <td vAlign=top align=left height="100%">
↑返回目录
前一篇: JSP页面显示乱码问题的解决
后一篇: J2ME中实现可伸展目录树TreeList