XML and J2EE's perfect combination

xiaoxiao2021-03-06  57

Currently, the Java 2 Platform Enterprise (J2EE) architecture is highly respected in the manufacturer market and developer community. As a tool, the Scalable Markup Language (XML) simplifies data exchange, inter-process messages exchange this class, and thus gradually attractive to developers and begins to popularize. Naturally, the idea of ​​accessing or integrating an XML solution in the J2EE architecture is also attractive. Because this will be a combination of powerful system architecture with highly flexible data management solutions. The application of XML seems to be endless, but they can be roughly divided into three categories: ● Simple data representation and exchange (simple API (SAX) and document object model (DOM) grammar parsing, different document types Definition (DTDS) and Summary (Schemas)) ● Batter-oriented calculation (XML-RPC (remote procedure call), SOAP protocol, electronic service XML (EBXML)) ● User interface related, representation related context (scalable style sheet Language (XSL), Extended Style Table Language Transformation (XSLT) These types of applications just in the J2EE architecture: data representation and switching function is part of the persistence service in the EJB component model, based on The message is handled by the Java Message Service (JMS) API, while the interface represents the Java Server page (JSP) and Java Servlets take advantage of the good play. In this article, we will see how the XML is applied in the previous J2EE-based applications, as well as how these applications will develop in future versions of the relevant standards. Basics: The contents of data representation and exchange of prototype XML applications are usually: Data is stored in XML format, in order to display, modify or even write an XML document, often read into an object. In the model.

As an example, it is assumed that we are working with multiple types of media (drawings, videos, text documents, etc.), and describe the metadata of these media with the following simple XML DTD: - Media assets area. Assets Are Also Hierarchical - They Can Contain Other assets. -> The following is an XML document based on the above media DTD describes the content related to a course lecture: <> < DOCTYPE Media-asset public "- // Jim Farley // DTD Media assets // en" "http://localhost/articles/sun/dtds/media.dtd"> All content related to the 14th lecture Lecture slide MS PowerPoint application / vnd.ms-power> http://javatraining.org/jaf/e123 / LECTURE- 14 / SLIDES.PPT Lecture video card segment < Type> RealPlayer Streaming Video video / vnd.rn-realvideo http://javatraining.org/jaf/e123/lex - 14 / Lecture.rv http://javatraining.org/jaf/e123/lecture-14/index.jsp From the perspective of web or enterprise applications, you can access data in this way is really a gospel because it reflects the height of removability, so that we are isolated from the actual resources of metadata itself.

These resources may come from a relational database system, a static XML document on a moving media server or a web server, and so on. If you want to load these data into the Java app, we can use one from the current numerous Java language XML parser. By loading XML data into a DOM document, finally traversing the document, converting all of these data to our application Object model. Below is a simple DOM-based resolution program, the above media DTD can be parsed. The parser is used by apache Xerces: package jaf.xml; import java.util. *; Import java.ioException; import org.w3c.dom. *; Import org.xml.sax. *; // XML document parser Using the above media DTD. Public class mediaparser imports errorhandler {/ ** Using Apache Xerces Resolution * / Org.Apache.XERCES.PARSERS.DOMPARSER MPARS.PARSERS.DOMPACHE.XERCES.PARSERS.DOMPARSER (); / ** Construction Function * / public mediaParser () {// tells the parser to verify and resolve the document Try {mParser.setfeature ("http://xml.org/sax/features/Validation", true);} catch (saxException e) {system .out.println ("Error Setting Validation on Parser:"); E.PrintStackTrace ();} // Setting the error handler of the parser MPARSER.SETERRORHANDLER (this);} / ** Resolution The specified URL, return to find XML document * / public document parse (String url) throws SAXException, IOException {mParser.parse (url); document mediaDoc = mParser.getDocument (); return mediaDoc;} / ** specified URL parsing XML documents, converting the contents to MediaAsset object * / public collection loadassets (String URL) throws saxception, ioException {document doc = parse (URL); Collection assets = new linkedList (); nodelist assetnodes = Doc.getElementsBytagname ("Media-asset"); for (int i = 0; i

The following is an example of using MediaAsset classes: package jaf.xml; import java.util *; public class MediaAsset {// resource metadata private String mName = ""; private String mDesc = ""; private Collection mChildren = new LinkedList. (); private vector mtypes = new vector (); private string murn = ""; protected mediaasset (org.w3c.dom.node assetnode) {// For the sake of simplicity, the back code is omitted..}} Sketch The detailed code of the MediaAsset class, but the application model is still clear. The Node of the MediaAsset class traverses the document, when it comes to different child nodes, it fills your own member data with the contents of the child node. If it discovers a nested sub-resource node, it only needs to create a new MediaAsset object and then populate the data of the sub-resource node to the member data of the new object. Realize the number of methods for the above treatment. We can also use other parsers or parser architectures such as Java API for XML Parsing (JAXP). In addition to using the DOM model, event-driven SAX models can also be used to parse XMLs. Similar programs can also be used to generate XML data - premise is to generate new data objects (in this example is MediaAsset), which can insert their corresponding XML entities into the DOM, then output DOM to a stream ( Such as a file, a socket, or an HTTP connection ...). There are also other higher levels of standards that can be mapped to the Java object process (or simplified). For example, using an XML Summary (Schema) and XML Binding Processing Engine, you can transform XML data that meets an XML profile into Java data objects. Representative engines are CASTOR, which is a product of an open source project by the Exolab team. The simple example of the above Xerces Dom is merely demonstrated the underlying model of this process. The above examples show that it is very convenient to resolve or produce XML in a Java environment, which is not necessarily associated with J2EE. Formatted to XML data can flow or output from any level of the application, which makes the integration with the external system unlimited. But can we set XML data sources into a J2EE architecture in a more direct way?

Driving message J2EE architecture contains access to the JMS (Java Message Service) API to implement message-oriented communication (J2EE 1.2.1) only JMS API, JMS in J2EE 1.3 is basically fixed, at this time must be A JMS API Provider that is compatible with a J2EE platform. Asynchronous interaction (relative to: local or remote method calls) is proven to be very useful in certain applications. At some point, interaction only needs to be implemented by indirect requests or answers, ie, in some cases, it is impossible to receive a reply immediately after issuing a message, but we still hope that when the message makes the message, make sure he can accept Go to the reply information. One of the actual applications of the message system is the loose integration between enterprises. Similar to document exchange in the era (electronic document exchange) era, two companies exchange messages due to the needs of the business, and this time does not interact between the two remote methods such as RPC or RMI, CORBA, DCOM. Tightly. Message systems such as JMS API allow both parties to exchange JMS API messages, provided that both parties provide compatible JMS API services when they are sessions. The current still existing is: whether the two sides can respect the same format or agreement. This is the time when XML is very show. XML is explicitly designed to solve such data exchange issues - Lower Dan is "Message-Oriented Communication Scheme), which is based on a two parties identified DTD or Schema, swaps the message load with XML format. JMS API supports several messages, which represent text messages. A simple and effective XML message exchange scheme is to insert our XML document into TextMessage at one end, then unfold the data with homemade XML resolution (such as the previous MediaParser) and convert it to Java object. This allows us to use JMS API's publicly subscribed message model, and you can also send an XML message with a point-to-point message model supported by JMS API. The above method has some limitations because the content of XML is basically opaque for JMS runtime processing. For example, the JMS API allows the use of a specific message header based route. This is easy to understand, especially when we want the XML message to take different trends according to its content. For example, in our MediaAsset example, we hope to open the seminars, but just send specific content to those who book a course, or transmitted to those who can accept certain media formats (such as video streams). In order to play the value of the JMS API to achieve the above-mentioned message-based message routing, we need to resolve critical information from the XML data, then insert this information when constructing a standard JMS API message. This is feasible, but to implement XML information, we have to write a lot of code (both parties to exchange messages). In order to make a bridge between XML and JMS API, some vendors provide custom JMS extensions to directly support the XML message mechanism. For example, the BEA system company is based on J2EE-based WebLogic application servers, providing XMLMESSAGE subclasses for TextMessage, allowing XML messages to be filtered with XPath expressions. However, this is a proprietary extension, which requires that both parties to exchange messages must handle such messages. To this end, Sun is currently developing Java API (JAXM) for XML messages. Its goal is to provide a high-level standard service to achieve the synthesis and delivery of EBXML messages.

A JAXM service provider can map such messages to the appropriate physical message system (such as JMS API). Let XML see how XML integrate user interfaces with a web system is clearly a useful attempt. Most interface programs, whether it is still based on whether it is still not based on the Web, is converted to the user with the readable format. Store data with such "easy digestion", such as XML, simplifies the above work, and it also greatly improves the managementability of the content, and then we can see this. But first, a big book is that XML is based on the development of the web interface layer in the development of JSP technology. Everyone has always hoped that the representation of the Web application and the underlying object model, the JSP framework was born in these efforts (including early JHTML attempts). The JSP framework allows the Java code to embed the HTML content, so that dynamic content can be implemented, and the Java Servlets can be modified frequently. The way to include Java technology in the page is through the JSP tag (JSP Tags), which appears in an XML style. In JSP, the Java program exists in a code snippet, the server-side JavaBeans component, and triggers the contacted form of opaque tags (standard or custom) or the like in the server side. When a user requests a JSP page through a browser, a Java application server resolves the JSP page and compiles it into a Java Servlet, and then executes the servlet to generate a reply page. Methods to direct XML data sources into the JSP interface is to load XML into the JavaBeans component (like we do in the MediaAsset example) and then reference these JavaBeans components directly in JSP.

Below is an example of an embedded Java code snippet: The media resource of the 14th lecture </ title> </ head> <body> <! - Introduced our class -> <% @ Page Import = "jaf.xml. *"%> <center> <h3> Media assets for lecture 14: </ h3> </ center> <! - Defines a resource object for display -> <jsp : usebean class = "jaf.xml.mediaasset" id = "asset" /> <! - From a previously defined location loading resource -> <% mediaparser Parser = new MediaParser (); Collection assets = parser.loadassets "http://javaschool.org /jaf/e162/lecture14-assets.xml"); itrator it = assets.ITerator ();%> <table border = 0> <tr> <tH> name </ tH> < TH> Type </ th> <TH> URN </ TH> </ Tr> <% while (ore.hasnext ()) {ask ();%> <TR> <TD> < JSP: getproperty name = "askPROPERTY =" name "/> </ td> <td> <jsp: getproperty name =" ask "proty =" type "/> </ td> <td> <jsp: getproperty name = "Asset" Property = "URN" /> </ td> </ tr> <%}%> </ table> </ body> </ html> wherein the bold part is a JSP code snippet and tag, the rest is Standard HTML text. The above program has a more concise write, that is, using a custom JSP page tag. This way we can remove the code segment from the JSP page, just use the JavaBeans component and custom JSP tag. For example, in order to remove the creation parser, load resource data into the code in the collection, we can create a mark that completes these work behind the scenes.</p> <p>The following is an example:......................................................................................... / center> <! - Load our custom tag library -> <% @ Taglib Uri = "http://javaschool.org/taglib" prefix = "Media"%> <! - from a previous definition Location Loading Resources -> <Media: Load Url = "http://javaschool.org/jaf/e162/lecture14-assets.xml" CollectionName = "Assets" CURSORNAME = "asset" /> <Table Border = 0>. The maximum benefit of using custom tags is to make our program code in one place (for Java technology, generally refer to "class"), easy to manage. This can define the integration relationship between the object layer in the program, which is clear, and the impact of modifying the code is predictable and managed. Another way to convert XML data into a web display content is to use XSL and XSLT. In this scenario, XML data is mapped to HTML (or WML, etc.) is defined by the XSL style sheet (XSL Stylesheet). The style sheet describes how each particular XML data entity converts to interface data entities (such as HTML tables, inline tags, etc.). In the JSP architecture, XSL conversion can only be applied to a specific XML data source, and most ideal is to adopt a custom JSP tag and reference a XSLT handler. Typical examples of this, please refer to white paper integrated with XML with JSP architecture in java.sun.com. Compared to the scheme of the XML parser component in the previous JSP, the XSLT scheme is better, and it has better manageability. In this case, our conversion logic is written in an XSL style sheet instead of in the Java code. This means that when you need to modify the interface, most cases just edit the style sheet or HTML, the code is not affected. However, before deciding what kind of scheme is selected, it is still necessary to carefully wear it according to the actual situation. If you choose an XSLT solution, you have to maintain these XSL style sheets (either person responsible for the interface, or write the program). XSLT is like a content, and a program, so both sides cannot push the responsibility to each other. As a result, everyone may have been contradictory by this ugly XSLT. From this consideration, the method of custom tags and the interface developer are used to embed its embedded layer seem more attractive, because such software engineers only consider Java code, and content engineers are only worked. The Java Servlet filter is a web component that J2EE version 1.3 is released in its Web layer. When the Sevelet reads a resource or read an answer information from a resource, the filter can easily convert the header information and content information. The resource mentioned here can be a Java Servlet, a JSP page, or even a static web page. The filter is indeed "cool" because it allows the developer to separate the part of the code from the code of the conversion content, and is reused. The Java Servlet filter is especially useful when you need to convert XML data to a different XML application target via the XSLT mode.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-112950.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="112950" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.042</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'C3U_2FYu9vP2eTkVQhomj1RASla_2B0DtcTYdSdXRKvDNxdZ_2FjgIDoNpbj2HT1ykpejyiweAzD0GTNfmQLHys7_2FfjA_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>