【文章內(nèi)容簡介】
n idea of how prevalent this solution is, try running a search on 39。JSP39。 at 。 you39。ll find more books devoted to JSP technology than about almost any other single Java API. Before I dive into the specific problems that JSP technology presents, you need a clear understanding of what it claims to do. Content vs. presentation Above all, JSP technology is about separating content from presentation, foremost in Sun39。s published set of goals for JSP pages. In fact, JSP design stemmed directly from the plaints of developers who were tired of typing (HTMLHEADTITLE + () + /TITLE/HEAD)。 into their servlet code. This mixing of hardcoded content with runtime variables presented a horrible burden on servlet developers. It also made making even minor changes to the presentation layer difficult for the technology addresses this situation by allowing normal HTML pages (and later, WML or other markup language pages) to be piled at runtime into a Java servlet, essentially mimicking the () paradigm, without requiring the developer to write this code. And it allows you to insert variables into the page that are not interpreted until a JSP page the HTML snippet shown in Listing 2 could look like the example in Listing 3. class=displaycode%@ page import= % 東華理工大學(xué)畢業(yè)設(shè)計(jì) 外文翻譯 7 %@ page import= % % PageInfo pageInfo = (PageInfo)(PAGE_DATA) % HTML HEAD TITLE %=()% /TITLE /HEAD BODY ! Other HTML content /BODY /HTML Judging by these initial principles, then, JSP technology (at least in its stated design) would satisfy the first te of a presentation technology, as outlined above: that content be separated from presentation. Code vs. markup Second on the JSP technology39。s list of features is something that might raise a bit of concern. JSP coding lets you insert Java code directly into a page of markup. To understand why this decision was made, recall that when the JSP specification was being developed, Sun39。s petition from Microsoft was at an alltime high, primarily due to the success of Microsoft Active Server Pages (ASP). The similarity of the name JavaServer Pages to Active Server Pages was not merely coincidental. And the ability to mimic many of ASP39。s features was also intentional. So JSP authors were given the option to add Java code into their an example of Java code being added to markup, the JSP snippet in Listing 4 dynamically adds rows as needed to show each item in the Vector of actors. class=displaycode%@ page import= % %@ page import= % %@ page import= % %@ page import= % %@ page import= % % 東華理工大學(xué)畢業(yè)設(shè)計(jì) 外文翻譯 8 PageInfo pageInfo = (PageInfo)(PAGE_DATA) Vector actors = () % HTML HEAD TITLE %=()% /TITLE /HEAD BODY H2 ALIGN=centerSearch Results: Actors/H2 CENTER HR width=85% TABLE width=50% CELLPADDING=3 CELLSPACING=3 border=1 BGCOLOR=FFFFCC % for (Iterator i = ()。 ()) { Actor actor = (Actor)()。 % TR BGCOLOR=FFCCCC TH width=50% ALIGN=center %=()% /TH TH width=50% ALIGN=center %=()% /TH /TR % } % /TABLE /CENTER /BODY /HTML 東華理工大學(xué)畢業(yè)設(shè)計(jì) 外文翻譯 9 Remember that so far I am simply describing the initial design goals of JSP technology。 I39。ll defer my own judgment about the goals until a later section about the problems of JSP technology. You might be a little suspicious already, however, since embedding code into a JSP page would seem to cause problems with the first goal of JSP technology, separating content from presentation. But really (ahem), I39。m not editorializing yet. Designer vs. developer A final (and admirable) goal of JSP technology worth mentioning is that it seeks to establish clearly defined roles in the application development process. By ostensibly breaking content from presentation, JSP technology creates a clearer distinction between the designer and developer. The designer creates markup, using only standard HTML, WML, or whatever language is appropriate, and the developer writes code. Of course, many designers today have learned JavaScript, so it should e as no surprise that many of these same designers have begun to learn JSP coding. Often, instead of just doing pure markup, they encode a plete JSP page and hand it over to the developer. Then the usual tweaking takes place, and the developer puts the JSP page into place as a frontend for some portion of the overall application. The key, though, is that many designers do not learn JSP coding, so it must also be workable in that environment. The problemsI39。ve spelled out what a good presentation technology should provide, as well as the specific problems that JSP technology seeks to address. Now, I39。m ready to cut to the chase: JSP technology, while built on good ideas, presents quite a few problems. Before you choose to use JSP coding in your applications (which you might still do), you should at least be aware of possible should also be aware of a facet of the J2EE programming platform that is often ignored: just because an API es with the platform doesn39。t mean you have to use it. As silly as this sounds, many developers are struggling with the JSP, or EJB, or JMS APIs, thinking if they don39。t use these APIs, their applications somehow won39。t really be J2EE applications. In fact, the platform boasts more APIs than most applications need. If you have problems with or doubts about JSP technology, you don39。t have to use it! Take a close look at both the positives and the negatives before choosing to use JS