Integrated application of Servlet and Javaserverpages

Source: Internet
Author: User
Tags end html form html page http request mqseries string version websphere application server
Erp|server|servlet

This article describes the integration of thin clients and MQSeries by using servlet and JavaServer pages. This integration requires users to fill out HTML forms in detail, collect user data from the form, and then send the data to the back-end application through a message queue. The back-end application then processes the form data and sends a reply back through the message queue. The answer needs to be displayed in the browser.

This article discusses the interactions between thin clients, servlet, and message queues, and demonstrates how to use the different products IBM offers in the solution. Readers should understand the Java language and become familiar with WebSphere and MQSeries.

  Overview of the architecture The following illustration shows the proposed solution architecture. It involves a three-layer approach.

  Processing process

The user fills out an HTML form. Sends a form to the servlet. The servlet converts the HTTP request into a MQSeries message and puts it into a queue. The backend application processes the message and sends a reply back through the message queue. The servlet retrieves the message from the queue and stores it in a Java Bean. The servlet then invokes the compiled JavaServer page and dynamically generates the resulting HTML page. The JSP retrieves the message content of the page from the Java Bean, merges it into HTML, and then echoes the resulting page back to the browser.

The solution leverages the following technologies:

Html/http, Java-servlet, Java Beans and JavaServer Page, Web server, Message Queuing

The solution integrates the following products:

Netscape 4.0/internet Explorer 3.0 or later, IBM HTTP Server 3.0, WebSphere 2.02, JDK version 1.1.7, MQSeries version 5.0

  Connect to MQSeries

We chose the servlet model because the model has many advantages over CGI. A servlet is a standard server-side Java application that extends the functionality of a WEB server. The servlet is fully operational on the Web Server and does not download anything to the browser. The servlet is mounted into the server's address space during the mount or during the initial request. After the initial request, the servlet responded very quickly. The servlet's Init method is ready for the servlet to run. Each servlet mount invokes only one Init method at a time. In the Init method, establish a connection to the MQSeries Queue Manager as follows:

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);
}
}
}

Because it takes a long time to establish a connection to the MQSeries Queue Manager, the Init method is the ideal place to perform this process. Subsequent calls to the servlet are then executed more quickly. WebSphere also allows the user to preloaded the servlet by using the administration GUI, so as the Queue Manager connection is established, the servlet prepares and waits for any message to be delivered.

If Mqexception is captured in the Init method, the preceding code is disconnected from the queue manager. As a result, users will have to reload the servlet in order to establish a connection to the queue manager.

In order for the servlet to talk to MQSeries, you must use MQSeries bindings for Java. MQSeries bindings for Java enables you to write MQSeries applications in the Java language. These applications communicate directly with the MQSeries Queue Manager to provide high productivity, high performance development options. They are invoked directly to the existing Queue Manager APIs using the Java native method instead of communicating through the MQSeries server Connection channel, which provides better performance for the Java MQSeries application. In the code we have to import the "com.ibm.mqbind.*" package. MQSeries Java classes should also be located in the classpath of WebSphere, which will allow the WebSphere application server to locate the MQSeries bindings for Java package.

  Creating MQ messages

Because Billingaddressservlet is primarily used to process HTTP requests from addressinputform, we generate subclasses for HttpServlet. This abstract subclass of Genericservlet implements the default service () method for processing requests. The most common methods of coverage include Doget () and DoPost (). The following example overrides the DoPost () method. Addressinputform calls this DoPost () method through the following call:

<form method=post action= "/servlet/billingaddressservlet" >

The POST request sends an HTTP message body that contains all the data sent to the servlet. The DoPost () method then creates the following message string from the input:

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;
}
}

  Send a message

The Putorder () method is then called by the DoPost () method to place the message string into the message queue by using the following code:

public void
Putorder (String tempaddress) {
try {
int openoptions = MQC. Mqoo_input_as_q_def | MQC. Mqoo_output;
Specify the \ 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.........

  Retrieving messages

To retrieve a message returned by a back-end application, the DoPost () method calls the Orderupdatestatus () method. The Orderupdatestatus () method retrieves the messages in the queue using the following code:

Public String Orderupdatestatus () {
String msgtext = null;
try {
int openoptions = MQC. Mqoo_input_as_q_def | MQC. Mqoo_output;

Specify the \ 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.........

  Call JSP

Finally, the Putorder () method calls the Performtask () method to store the message in the Billingaddress Bean. The servlet then invokes the Addressoutputpage JSP and passes the Billingaddressbean handle to it. The JSP extracts dynamic content (that is, messages from Java beans) and merges them into an HTML page that is displayed on the browser.

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" in The bean
Mybillingaddressbean.setmqreturnmessage (ReturnMessage);
Store the Bean in the ' request so it can ' 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.........

  Conclusion

In this solution, the combination of a servlet and a JSP allows functional logic to be separated from the display. The servlet acts as a controller, and the JSP acts as the result view. Similarly, the content display and content itself are clearly divided using JSP and Java beans. By using the Servlet init method to prepare the servlet, for example, to create a connection to the Queue manager, improved performance. WebSphere allows users to mount the servlet in advance to enable time-consuming tasks such as establishing a connection to be completed during initialization before the user uses the servlet. By using messages and queues, MQSeries enables different applications to integrate with each other.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.