1、计算机专业文献翻译XML与JSP联手英文原文: Using XML and JSP together Im going to assume that, like most Java programmers, you know what JavaServer Pages (JSP) and Extensible Markup Language (XML) are, but you may be a little unclear on how you can use them. JSP use is pretty easy to defend. It allows you to design a
2、Website built from files that look and act a lot like HTML. The only difference is that JSPs also act dynamically - for example, they can process forms or read databases - using Java as a server-side scripting language. XML use is more difficult to justify. While it seems as if every new product sup
3、ports it, each one seems to be using XML for a different purpose.In this article, you will learn to design a system using XML in a fairly modest way. Many Websites have vast collections of data that are displayed in a more or less standard way. I will design a system that uses XML files to store dat
4、a on a Web server and JSP files to display that data. XML versus relational databases But wait, you may ask, youre using XML to store data? Why not use a database? Good question. The answer is that for many purposes, a database is overkill. To use a database, you have to install and support a separa
5、te server process, which often also requires installing and supporting a database administrator. You must learn SQL, and write SQL queries that convert data from a relational to an object structure and back again. If you store your data as XML files, you lose the overhead of an extra server. You als
6、o gain an easy way to edit your data: just use a text editor, rather than a complicated database tool. XML files are also easier to back up, to share with your friends, or to download to your clients. You can also easily upload new data to your site, using FTP. A more abstract advantage of XML is th
7、at, being a hierarchical rather than a relational format, it can be used in a much more straightforward manner to design data structures that fit your needs. You dont need to use an entity relationship editor nor normalize your schema. If you have one element that contains another element, you can r
8、epresent that directly in the format, rather than using a join table. Note that for many applications, a filesystem will not suffice. If you have a high volume of updates, a filesystem may get confused or corrupted by simultaneous writes; databases usually support transactions, which allow concurren
9、cy without corruption. Further, a database is an excellent tool if you need to make complicated queries, especially if they will vary from time to time. Databases build indexes, and are optimized for keeping the indexes up to date with a constantly changing data set. Relational databases also have m
10、any other advantages, including a rich query language, mature authoring and schema design tools, proven scalability, fine-grained access control, and so on. (Note: You can use simple file locking to provide a poor mans transaction server. And you can also implement an XML index-and-search tool in Ja
11、va, but thats a topic for another article.) In this case, as in most low-to-medium volume, publishing-based Websites, you can assume the following: most of the data access is reads, not writes; the data, though potentially large, is relatively unchanging; you wont need to do complicated searches, bu
12、t if you do, youll use a separate search engine. The advantages of using a mature RDBMS fade, while the advantage of using an object-oriented data model come to the fore. Finally, its entirely possible to provide a wrapper for your database that makes SQL queries and translates them into XML streams
13、, so you could have it both ways. XML becomes a more robust, programmer-friendly frontend to a mature database for storing and searching. (Oracles XSQL servlet is one example of this technique.) The application: An online photo album Everybody loves photos! People love showing pictures of themselves
14、, their friends, their pets, and their vacations. The Web is the ultimate medium for self-indulgent shutterbugs - they can annoy their relatives from thousands of miles away. While a full-fledged photo album site would require a complicated object model, Ill focus on defining a single Picture object
15、. (The source code for this application is available in Resources.) The object representing a picture needs fields representing its title, the date it was taken, an optional caption, and, obviously, a pointer to the image source. An image, in turn, needs a few fields of its own: the location of the
16、source file (a GIF or JPEG) and the height and width in pixels (to assist you in building tags). Here there is one neat advantage to using the filesystem as your database: you can store the image files in the same directory as the data files. Finally, lets extend the picture record with an element d
17、efining a set of thumbnail images for use in the table of contents or elsewhere. Here I use the same concept of image I defined earlier. The XML representation of a picture could look something like this: Alex On The Beach 1999-08-08 Trying in vain to get a tan alex-beach.jpg 340 200 alex-beach-sm.j
18、pg 72 72 alex-beach-med.jpg 150 99 Note that by using XML, you put all the information about a single picture into a single file, rather than scattering it among three or four separate tables. Lets call this a .pix file - so your filesystem might look like this: summer99/alex-beach.pixsummer99/alex-
19、beach.jpgsummer99/alex-beach-sm.jpgsummer99/alex-beach-med.jpgsummer99/alex-snorkeling.pixetc.Techniques Theres more than one way to skin a cat, and theres more than one way to bring XML data on to your JSP page. Here is a list of some of those ways. (This list is not exhaustive; many other products
20、 and frameworks would serve equally well.) DOM: You can use classes implementing the DOM interface to parse and inspect the XML file XMLEntryList: You can use my code to load the XML into a java.util.List of name-value pairs XPath: You can use an XPath processor (like Resin) to locate elements in th
21、e XML file by path name XSL: You can use an XSL processor to transform the XML into HTML Cocoon: You can use the open source Cocoon framework Roll your own bean: You can write a wrapper class that uses one of the other techniques to load the data into a custom JavaBean Note that these techniques cou
22、ld be applied equally well to an XML stream you receive from another source, such as a client or an application server. JavaServer Pages The JSP spec has had many incarnations, and different JSP products implement different, incompatible versions of the spec. I will use Tomcat, for the following rea
23、sons: It supports the most up-to-date versions of the JSP and servlet specs Its endorsed by Sun and Apache You can run it standalone without configuring a separate Web server Its open source (For more information on Tomcat, see Resources.) You are welcome to use any JSP engine you like, but configur
24、ing it is up to you! Be sure that the engine supports at least the JSP 1.0 spec; there were many changes between 0.91 and 1.0. The JSWDK (Java Server Web Development Kit) will work just fine. The JSP structure When building a JSP-driven Website (also known as a Webapp), I prefer to put common functi
25、ons, imports, constants, and variable declarations in a separate file called init.jsp, located in the source code for this article. I then load that file into each JSP file using . The directive acts like the C languages #include, pulling in the text of the included file (here, init.jsp) and compili
26、ng it as if it were part of the including file (here, picture.jsp). By contrast, the tag compiles the file as a separate JSP file and embeds a call to it in the compiled JSP. Finding the file When the JSP starts, the first thing it needs to do after initialization is find the XML file you want. How
27、does it know which of the many files you need? The answer is from a CGI parameter. The user will invoke the JSP with the URL picture.jsp?file=summer99/alex-beach.pix (or by passing a file parameter through an HTML form). However, when the JSP receives the parameter, youre still only halfway there. Y
28、ou still need to know where on the filesystem the root directory lies. For example, on a Unix system, the actual file may be in the directory /home/alex/public_html/pictures/summer99/alex-beach.pix. JSPs do not have a concept of a current directory while executing, so you need to provide an absolute
29、 pathname to the java.io package. The Servlet API provides a method to turn a URL path, relative to the current JSP or Servlet, into an absolute filesystem path. The method ServletContext.getRealPath(String) does the trick. Every JSP has a ServletContext object called application, so the code would
30、be: String picturefile = application.getRealPath(/ + request.getParameter(file);or String picturefile = getServletContext().getRealPath(/ + request.getParameter(file);which also works inside a servlet. (You must append a / because the method expects to be passed the results of request.getPathInfo().
31、) One important note: whenever you access local resources, be very careful to validate the incoming data. A hacker, or a careless user, can send bogus data to hack your site. For instance, consider what would happen if the value file=././././etc/passwd were entered. The user could in this way read your servers password file. The Document Object Model DOM stands for the Document Object Model. It is a standard API for browsing XML documents, developed by the World
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1