XML and JSP this article from http://www.javaworld.com/javaworld/jw-03-2000/jw-0331-ssj-jspxml.htm
If you visit this page, you can also connect to Resource, Init.JSP, and other source code files.
Consequence
}
Two Great Tastes That Taste Great Together
Author: Alex Chaffee
Overview:
XML and JSP are the hottest things in these days. This article describes how to combine these two
Techniques to build a dynamic website. You can also look at DOM, XPath, XSL,
And sample code for other Java-XML technology.
We have assumed you to know JavaServer Pages (JSP) and Extensible
Markup Language (XML). But maybe you have used them to use them.
confuse.
JSP applications are easy, you can use it to design web pages, make it look like and HTML
kind. The only difference is that JSP is dynamically executed. For example, they can handle forms FORM
And read and write the database.
The description of the application of XML is more difficult. It seems that all products support it, everyone is also
It seems to use it in a variety of different purposes.
In this article, you can see how to use an XML in a fairly advanced way to design one
System. Many sites have huge amounts of data to collect and use a very standard or unbelievable way.
To display them. I will design a system that uses XML files on the web server.
Store and use JSP to display data.
XML VS relational database
"Wait!" You may ask, "Do you store data with XML files? Why don't you use a database?"
This question is very good. My answer is that the use of many purposes is too
Waste. To use a database, you must install and support a separate server
Treatment process (a Separate Server Process), which often requires installation and support
Administrator. You must learn SQL, write queries with SQL, then convert data,
Return. And if you store data with XML file, you will reduce the negative of additional servers.
Load. Also, you have also found a simple way to edit data. You only need to use text
Compare, without having to use complex database tools. XML file is easy to back up, and friends
Enjoy, or download to your client. Similarly, you can easily upload new in FTP
Data to your site.
XML has a more abstract advantage that as a layer-based format is better than the relationship.
It can design data structures in a very direct way to meet your needs. You don't need
To use an entity-relational editor, you don't need to make your chart (Schema) standard
. If you have an element (Element) contains another element, you can
In the format, it indicates it without the need to use the association.
Note that in many applications, it is not sufficient to rely on the file system. If there are a lot of updates,
The file system is destroyed by simultaneous writing. Database usually supports transaction processing,
It is possible to cope with the requested request and not damage. For complex query statistics, there must be repeated
, Timely update, this time the database performance is excellent. Of course, the relational database is also
There are many advantages, including rich query languages, chart tools, scalability, access
Control, etc.
(Note: You can use simple file lock to provide a transaction server, you still
You can perform an XML INDEX-AND-Search tool in Java, but this is already
Another theme of an article. )
In the following case, as most small, based on the site-based site, most data access you may involve is read, not written, data
However, it may be large, but it is relatively unusually updated, you don't need to do it.
Complex query, even if you need to do it, you will use a separate query tool, then
The advantages of the cooked RDBMS disappeared, and the advantages of object-oriented data models can be
By manifestation.
Finally, provide a querier shell for your database to perform SQL query and turn them
It is also possible to enter XML Stream is also possible.
So you can choose one of these two ways. XML is turning into a very robust, easy
Programming tools, as a front-end tool for a mature database for storage and queries.
(Oracle's XSQL servlet is a good example of this technology.)
Application: an online album
Everyone likes to take pictures! They like to show their own, relatives, friends, vacation
Time photos, and web is a good place they show. - Even relatives outside a thousand miles
You can see it. I will focus on defining a separate Picture object. (This application
The source code can be obtained in Resources). This object describes a photo required to indicate a photo.
Field: Title, Date, an optional title, and a point for the source
to.
An image requires some of its own fields: source file (GIF / JPEG) positioning, wide
Degree and height pixels (to assist in building tags. Here you can see a very simple
Advantages, that is, when using the file system instead of the database, you can save the graphic file
Put in the same directory as the data file.
Finally, let us use an element to expand the picture record, which defines a set of thumbnails
Figure (thumbnail) is used for content sheets or elsewhere. Here I used and previously the same
Defined picture content.
A picture of the XML representation can be like this:
iMAGE>
iMAGE>
iMAGE>
thumbnails>
Picture>
Note that by using XML, you put all the information on a separate picture to a separate
In the file, not to disperse it into 3-4 tables.
We will call this as .pix file
- So your file system will be like this:
Summer99 / alex-beach.pix
Summer99 / alex-beach.jpg
Summer99 / alex-beach-sm.jpgsummer99 / alex-beach-med.jpg
SUMMER99 / ALEX-SNORKELING.PIX
ETC.
Technical articles
As the saying goes, how to peel off the skin of the cat. Similarly, put XML data into JSP
More than one way is also known. Here is some methods, (in fact, many other workers)
It can also be done. )
DOM: You can use classes to call the DOM interface (interface) to XML
The file is analyzed.
XMLENTRYLIST: You can use my code to load XML to Name-Value
PAIRS 'java.util.list.
XPath: You can use an XPath processor (such as RESIN) through the path name in XML
Locate elements in the file.
XSL: You can use some XSL processor to convert XML to HTML.
COCOON: You can use Cocoon Framework for open source
Run your own bean: You can write a housing class (Wrapper Class), use a
Other techniques to load data into a word-defined JavaBean.
Please note that these technologies will be in the same way as you have learned from another source.
Excellent, such as a client or an application server.
JavaServer Pages
The JSP specification has a lot of substitutes, and the performance of different JSP products is not the same, between different versions.
There is also a difference. I chose Tomcat, which is based on the following reasons:
It supports most of the latest JSP / Servlet specifications
It is recognized by Sun and Apache
You can run it independently without need to configure a web server.
It is open source
You can choose any JSP engine you like, but you must configure it yourself, it must be at least
JSP 1.0 specification. There are many differences between 0.91 and 1.0. Jswdk
(Java Server Web Development Kit) may just be suitable for requirements.
JSP structure
When you create a JSP website (WebApp), I like to import public functions, import, constants,
Variable declarations are placed in a separate file init.jsp. Then
<% @ include file = "init.jsp"%> Load to each file.
<% @ include%> Just like the C language #include, including Include makes it when compiling
Text is added as a part to compile and compile together, relatively,
The flag is to compile the files independently, then embed a pair in the file.
Its call.
Find files
When JSP starts, the first thing to initialize is to find the XML file you want. How is it?
What one of you are looking for in numerous files? It comes from a parameter, user
Add parameters in the URL calling JSP: Picture.jsp? File = summer99 / alex-beach.pix
(Or pass the file parameters via HTML form).
However, when JSP accepts this parameter, you still have half work, because
Where is the root directory of the file system. For example, in a UNIX system, the actual file may
In this path:
/Home/alex/public_html/pictures/summer99/alex-beach.pix.
There is no current path concept when the JSP file is executed. So you have given Java.IO package
A absolute path.
The Servlet API can provide a method to route a URL from the current JSP
Or the path to the servlet is converted to an absolute file system path. The method is: servletContext.getRealPath (String).
Each JSP has a servletContext object called Application. So
The code can be:
String picturefile =
Application.getRealPath ("/" Request.GetParameter ("file"));
or
String picturefile =
GetServletContext (). getRealPath ("/" Request.GetParameter ("file"));
It can also work in servlet. (You must add / because this method needs to pass Request.getPathInfo
()the result of. )
There is an important tip here: Whenever you access local resources, you must checked the legality of the input data very carefully.
.
Hackers or careless users, may send fake or wrong data to destroy your station
point. For example, please think about what the following results occur:
If you enter file = .. / .. / .. / .. / etc / passwd. This way users read back to you
Server's Password file!
DOM (Document Object Model)
DOM represents document object model Document Object Model. It browsing XML documentation
A standard API developed by World Wide Web Consortium (W3C). interface
In the Org.W3c.DOM package, the documentation will see the W3C site.
There are many available DOM analyzer tools. I chose the IBM XML4J. But you can use
Any other DOM analyzer. This is because the DOM is a set of interfaces, not classes - all
The DOM Analyzer (PARSER) must return objects that deal with these interfaces.
Unfortunately, although it is very standard, DOM still has two major flaws:
1 API is also object-oriented, is still quite cumbersome.
Dom Parser does not have a standard API, so when each analyzer returns one
Org.w3c.dom object, document object - analyzer initialization and file itself loaded
, Corresponding to different analyzers is typically always specific.
This simple picture file that has been described above can be in a tree structure in a tree structure.
Some objects are represented as follows:
Document Node
-> Element Node "Picture"
-> Text node "/ n" (Whitespace)
-> Element Node "Title"
-> Text Node "alex on the beach"
-> Element Node "Date"
-> ... ETC.
In order to get "Alex On The Beach", you have to do some ways to call, travel DOM
Tree, and analyzer may choose to disperse "Whitespace" text Nodes
According to, you have to use cycles and series, etc. (you can call Normalize ()
Correct this issue. ) The analyzer may also contain separate XML entities (such as &),
CDATA NODES or other entity nodes (such as Big will become at least three
Node. There is no way to express it in the DOM "Get Me TEXT VALUE OF"
In short, there is a cumbersome game in the DOM. (See this article to replace the DOM chapter in this article.)
2 From a higher place, the problem of the DOM is that the XML object cannot be directly like the Java object.
They need to be obtained by a DOM API.
You can refer to my summary of the Java-XML Data Binding technology discussion,
It also uses this method of directly using Java to access XML data.
I wrote a small tool class called Domutils, including static methods to perform public
DOM task. For example, to get the text of the Tostle child elements of the root (picture) element
Rong, you can write the following code:
Document Doc = Domutils.xml4jparse (PictureFile);
Element noderoot = doc.getdocumentelement ();
Node nodetitle = Domutils.getChild (Noderoot, "Title");
String title = (nodetitle == null)? Null: Domutils.getTextValue (NodetiL)
;
Get the value of the iMage child element is also the same:
Node nodeImage = Domutils.getchild (Noderoot, "Image");
Node nodesrc = Domutils.getChild (NodeImage, "src");
String SRC = Domutils.getTextValue (Nodesrc);
and many more.
Once you need to use the Java variable for each related element, you have to do it.
The amount is embedded in the HTML tag: