Overview:
xml and jsp are the hottest things of these days. This article introduces how to combine these two technologies to build a dynamic website. You can also take a look at the sample code of dom, xpath, xsl, and other java-xml technologies at the same time.
Let's assume here that you already understand javaserver pages(jsp) and extensible markup language (xml). But maybe you are still a little confused about how to use them in combination.
JSP is easy to use, you can use it to design web pages so that they look the same as html. The only difference is that jsp is executed dynamically. For example, they can handle form forms and read and write databases.
The instructions for the application of xml are more difficult. It seems that all products support it, and everyone seems to be using it for various purposes.
In this article, you can see how to use a fairly advanced way to design a system with XML. Many sites have huge amounts of data collected and display them in a very or very unstandard way. I will design a system that uses xml files to store on a web server and uses jsp to display data.
xml vs relational database
"Wait a minute!" you might ask, "Do you use xml files to store data? Why don't you use a database?"
This question is very good. My answer is that for many purposes, using a database is too wasteful. .To use a database, you must install and support a separate server process (a separate server process), which often requires an administrator that installs and supports it. You must learn SQL, and write queries in SQL, then convert the data, and then return. And if you use xml files to store data, it will reduce the load on the additional server. Also, you have also found an easy way to edit data. You just need to use a text editor instead of using complex database tools. XML files are easy to back up, share with friends, or download to your client. Similarly, you can conveniently upload new data to your site via ftp.
xml has a more abstract advantage, that is, the format as a hierarchical type is better than the relational type. It can design data structures in a very direct way to meet your needs. You don't need to use an entity-relationship editor, nor do you need to normalize your schema. If you have an element that contains another element, you can represent it directly in the format without using the table association.
Note that in many applications, relying on the file system is not sufficient. If there are many updates, the file system will be damaged due to simultaneous writes. The database usually supports transaction processing and can handle the requests that occur without being corrupted. For complex query statistics, you must have repeated and timely updates, and the database performance is excellent at this time. Of course, relational databases have many advantages, including rich query languages, graphical tools, scalability, access control, etc.
(Note: You can use simple file locking to provide a transaction server, and you can also execute an xml index-and-search tool in Java, but this is already the topic of another article.)
In the following case, just like most small and medium-sized, information-based sites, most of the data access you may be involved in is read rather than write. Although the data may be large, it does not update and change frequently, and you do not need to do very complex queries. Even if you need to do it, you will use an independent query tool. Then the advantages of mature rdbms disappear, and the advantages of object-oriented data models can be reflected.
Finally, it is also entirely possible to provide your database with a queryer shell for SQL queries and convert them into XML stream.
So you can choose one of these two ways. xml is becoming a very robust and easy-to-programm tool as a front-end tool for a mature database for storage and query. (Oracle's xsql servlet is a good example of this technology.)
Application: An online photo album
Everyone loves taking pictures! They like to show their own, their loved ones, their friends, their vacation photos, and the web is a great place for them to show. -- Even relatives thousands of miles away can see it. I'll focus on defining a separate picture object. (The source code of this application can be obtained in resources) . The object describes the fields required to represent a photo: title, date, an optional title, and a pointer to the image source.
An image requires some of its own fields: the positioning, width and height pixels of the source file (gif/jpeg) (to help create <img> tags. Here you can see a very simple advantage, that is, when using a file system instead of a database, you can store the graphics file in the same directory as the data file.
Finally, let's expand the image record with an element that defines a set of thumbnails for use in the content table or elsewhere. Here I used the same image definition as before.
The xml representation of a picture can be like this:
<picture>
<title>alex on the beach</title>
<date>1999-08-08</date>
<caption>trying in vain to get a tan</caption>
<image>
<src></src>
<width>340</width>
<height>200</height>
</image>
<thumbnails>
<image>
<src></src>
<width>72</width>
<height>72</height>
</image>
<image>
<src></src>
<width>150</width>
<height>99</height>
</image>
</thumbnails>
</picture>
Note that by using xml, you put all the information of a separate picture into a separate file instead of spreading it into 3-4 tables. We call this .pix file
-- So your file system will look like this:
summer99/
summer99/
summer99/
summer99/
summer99/
etc.
Technology
As the saying goes, there is more than one way to peel off a cat's skin. Similarly, there is more than one way to put XML data into JSP. Here are some of these methods (in fact, many other tools can be done equally well.)
dom: You can use classes to call the dom interface to analyze and check the XML file.
xmlentrylist: You can use my code to load xml into name-value pairs .
xpath: You can use an xpath processor (such as resin) to locate elements in an xml file through the path name.
xsl: You can use some kind of xsl processor to convert xml into html.
cocoon: You can use the open source cocoon framework
Run your own bean: You can write a wrapper class and use some other technique to load data into a word-defined javabean.
Please note that these technologies will perform as well as an XML stream you get from another source, such as a client or an application server.
javaserver pages
There are many stand-in models for the jsp specification, and different jsp products perform differently, and there are also differences between different versions. 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 configuring another web server.
It is open source
You can choose any jsp engine you like, but to configure it yourself, it must support at least the jsp 1.0 specification. There are many differences between 0.91 and 1.0. And jswdk (java server web development kit) may just fit the requirements.
jsp structure
When creating a jsp website (webapp), I like to put common functions, imports, constants, and variable declarations into a separate file. Then use <%@include file=""%> to load it into each file. <%@include%> Just like the #include in the c language, include the text is added as a part and compiled together at compile time. In contrast, the <jsp:include> tag makes the files in it compile independently, and then embed a call to it in the file.
Find files
When jsp starts, the first thing you do after initialization is to find the xml file you want. How does it know which one you are looking for among the many files? It comes from a parameter, and the user will add parameters to the URL calling jsp: ?file=summer99/ (or pass file parameters through the html form).
However, when jsp accepts this parameter, you are still only half done because you also need to know where the root directory of the file system is. For example, in a unix system, the actual file might be in a path like this:
/home/alex/public_html/pictures/summer99/。
The jsp file has no current path concept when it is executed. So you want to give an absolute path to the package.
servlet api can provide a method to convert a url path from a path relative to the current jsp or servlet into an absolute file system path. The method is:
(string)。
Each jsp has a servletcontext object called application. So the code can be:
string picturefile =
("/" + ("file"));
or
string picturefile =
getservletcontext().getrealpath("/" + ("file"));
It works in servlets as well. (You have to add / because this method needs to pass the result of().)
Here is an important tip: Whenever you access local resources, be very careful to check the legality of the input data. Hackers or careless users may send fake or wrong data to destroy your site. For example, think about what will happen to the following expression:
If file=../../../../etc/passwd is entered. In this way, the user reads back to your server's password file!
dom (document object model)
dom represents the document object model document object model. It is a standard API for browsing XML documents, developed by world wide web consortium (w3c). The interface is in the org. package, see the w3c site for the documentation.
There are many dom analyzer tools available. I chose ibm's xml4j. But you can use any other dom analyzer. This is because dom is a set of interfaces, not a class - all dom parsers must return objects that handle these interfaces the same way.
Unfortunately, although it is very standard, dom still has two major flaws:
1 Although the api is also object-oriented, it is still quite bulky.
dom parser does not have a standard API, so when each analyzer returns an org. object, document object--the way in which the analyzer is initialized and the file itself is loaded, it is usually always specific to different analyzers.
This simple picture file described above can be represented by some objects in a tree structure in the dom 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 obtain "alex on the beach", you have to make some method calls and travel to the dom tree. Moreover, the analyzer may choose to disperse some data of the "whitespace" text nodes, and you have to use loops and concatenation. (You can correct this problem by calling normalize().) The analyzer may also contain separate xml entities (such as &), cdata nodes or other entities nodes (such as <b>big<b> will be turned into at least three nodes. There is no way to simply mean "get me the text value of the title element." In short, traveling in the dom is a bit clumsy. (See the chapter on replacing dom with xpath.)
2 From a higher point, the problem with dom is that xml objects cannot be obtained directly like java objects, and they need to be obtained one by one through dom api.
You can refer to some summary of my discussion on java-xml data binding technology, where this method of directly using java is also used to access xml data.
I wrote a small tool class called domutils, which contains static methods to perform common dom tasks. For example, to get the text content of the title child element of the root (picture) element, you can write the following code:
document doc = domutils.xml4jparse(picturefile);
element noderoot = ();
node nodetitle = (noderoot, "title");
string title = (nodetitle == null) ? null : (nodetitle);