10
c h a p t e r 1 An Overview of JSP 1 IN THIS CHAPTER Server-Side Scripting and Servlets JavaServer Pages The Power of Java • Recap While JavaServer Pages (JSP) is still a somewhat new technology, the speed at which it is becoming a standard for creating enterprise-class Web applications is amazing. While Java technology has been slowly gaining ground in client/server and standalone applications, with both JSP and Java servlets, Java has become the major platform for large-scale eBusi- ness applications. 1.1 Server-Side Scripting and Servlets To better understand why JSP has risen so fast in the realm of Web application development, it is best to have a brief introduc- tion to server-side scripting languages and Java servlets. 1.1.1 Server-Side Scripting Solutions There are several common scripting solutions to create Web ap- plications. These are scripts that are run on the server before the page is sent to the user. Netscape’s server-side scripting solution is called Server-Side JavaScript (SSJS). In SSJS, JavaScript is executed on the server to ch01.qxd 6/15/01 8:21 AM Page 1

chapter 1 An Overview of JSP - Pearson Education · back of using Java servlets is in their power. The Java program-ming language is at once both powerful and complicated, and learning

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: chapter 1 An Overview of JSP - Pearson Education · back of using Java servlets is in their power. The Java program-ming language is at once both powerful and complicated, and learning

ch

ap

te

r

1 An Overview of JSP

1

IN THIS CHAPTER

• Server-Side Scripting and Servlets

• JavaServer Pages

• The Power of Java

• Recap

While JavaServer Pages (JSP) is still a somewhat new technology, thespeed at which it is becoming a standard for creating enterprise-class Webapplications is amazing. While Java technology has been slowly gainingground in client/server and standalone applications, with both JSP andJava servlets, Java has become the major platform for large-scale eBusi-ness applications.

� 1.1 Server-Side Scripting and Servlets

To better understand why JSP has risen so fast in the realm ofWeb application development, it is best to have a brief introduc-tion to server-side scripting languages and Java servlets.

1.1.1 Server-Side Scripting SolutionsThere are several common scripting solutions to create Web ap-plications. These are scripts that are run on the server before thepage is sent to the user.

Netscape’s server-side scripting solution is called Server-SideJavaScript (SSJS). In SSJS, JavaScript is executed on the server to

ch01.qxd 6/15/01 8:21 AM Page 1

Prentice Hall PTR
This is a sample chapter of Essential JSP for Web Professionals ISBN: 0-13-064941-4 For the full text, visit http://www.phptr.com ©2001 Pearson Education. All Rights Reserved.
Page 2: chapter 1 An Overview of JSP - Pearson Education · back of using Java servlets is in their power. The Java program-ming language is at once both powerful and complicated, and learning

modify HTML pages. In SSJS, scripts are precompiled to improveserver performance. SSJS is available on several different versionsof Netscape Web servers. To learn more about SSJS, go to: http://developer.netscape.com/tech/javascript/ssjs/ssjs.html.

Microsoft servers offer Active Server Pages (ASP). ASP is verysimilar to JSP. ASP allows developers to embed VBScript or JScriptcode directly into a Web page. ASP has to be compiled every timeit is run, mirroring one of the major drawbacks of Common Gate-way Interface (CGI) scripts. ASP is available to developers run-ning Microsoft’s Internet Information Server 3.0 or above.

By far, the biggest drawback of the major scripting solutionsis their proprietary nature. All of the solutions discussed are de-pendent on either certain Web servers or specific vendors.

1.1.2 Java ServletsJava servlets are a powerful alternative to CGI programs andscripting languages. Servlets are extremely similar to proprietaryserver APIs (application programming interfaces), but becausethey are written in the Java programming language, they can beeasily ported to any environment that supports the Java ServletAPI. Since they run in the Java Virtual Machine (JVM), they by-pass the security problems that affect the server APIs.

Servlets are run inside a servlet engine. Each individualservlet is run as a thread inside a Web server process. This is amuch more efficient solution than multiple server processes im-plemented by CGI programs. By running in threads, servlets arealso very scaleable, and as they are a part of the Web serverprocess themselves, they can interact closely with the Web server.

Servlets are extremely powerful replacements for CGI pro-grams. They can be used to extend to any type of server imagin-able. The built-in thread and security support make servlets arobust tool for extending a server service.

All major Web servers now support servlets. The major draw-back of using Java servlets is in their power. The Java program-ming language is at once both powerful and complicated, andlearning Java is a formidable task for the average Web developer.

� 1.2 JavaServer Pages

JSP is an extremely powerful choice for Web development. JSP is atechnology using server-side scripting that is actually translatedinto servlets, which are compiled before they are run. This givesdevelopers a scripting interface to create powerful Java servlets.

2 Chapter 1 • An Overview of JSP

ch01.qxd 6/15/01 8:21 AM Page 2

Page 3: chapter 1 An Overview of JSP - Pearson Education · back of using Java servlets is in their power. The Java program-ming language is at once both powerful and complicated, and learning

JSP provides tags that allow developers to perform most dy-namic content operations without writing complex Java code.Advanced developers can add the full power of the Java pro-gramming language to perform advanced operations in JSP.

1.2.1 Template PagesClearly, the most effective way to make a page respond dynami-cally would be to simply modify the static page. Ideally, specialsections to the page could be added that would be changed dy-namically by the server. In this case, pages become more like apage template for the server to process before sending. These areno longer normal Web pages; they are now server pages.

On a server page, the client requests a Web page, the serverreplaces some sections of a template with new data, and sendsthis newly modified page to the client (see Figure 1–1).

Since the processing occurs on the server, the client receiveswhat appears to be static data. As far as the client is concernedthere is no difference between a server page and a standard Webpage. This creates a solution for dynamic pages that does notconsume client resources and is completely browser-neutral.

1.2.2 Static Data vs. Dynamic ElementsSince JSP is designed around static pages, it can be composed of thesame kind of static data as a standard Web page. JSP pages useHTML or XML to build the format and layout of a page. As long asa normal Web page can contain the data, so can a JSP page.

1.2 JavaServer Pages 3

FIGURE 1–1 Server Page

ch01.qxd 6/15/01 8:21 AM Page 3

Page 4: chapter 1 An Overview of JSP - Pearson Education · back of using Java servlets is in their power. The Java program-ming language is at once both powerful and complicated, and learning

To replace sections of a page, the server needs to be able torecognize the sections it needs to change. A JSP page usually has aspecial set of “tags” to identify a portion of the page that shouldbe modified by the server. JSP uses the <% tag to note the start of aJSP section and the %> tag to note the end of a JSP section. JSP willinterpret anything within these tags as a special section.

JSP pages usually contain a mixture of both static data anddynamic elements. It is important to understand the distinctionbetween the two forms. Static data is never changed in the serverpage, and dynamic elements will always be interpreted and re-placed before reaching the client.

1.2.3 A Simple JSP PageOften, the easiest way to understand something is to see it. Script1.1 shows a very simple JSP page.

Don’t worry too much about what the JSP page is doing; thatwill be covered in later chapters. It is important to notice the twotypes of data in the page: static data and dynamic data. Under-standing the difference between these builds an essential founda-tion for creating JSP pages.

Script 1.1 simpleDate.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Final//EN"><HTML><HEAD><TITLE>Stitch Magazine! - A simple date example</TITLE></HEAD><BODY COLOR=#ffffff><font face="Arial">The current time is <%= new java.util.Date() %></font></BODY></HTML>

When the client requests this JSP page, the client will receivea document as HTML. The translation is displayed in Figure 1–2.

When compiled and sent to the browser, the page shouldlook like Figure 1–3.

1.2.4 JavaServer PagesMost Web servers that understand JSP will look for a specific file-name extension. Typically, any filename that ends in .jsp willbe interpreted and processed by the JSP engine. Often, the actual

4 Chapter 1 • An Overview of JSP

ch01.qxd 6/15/01 8:21 AM Page 4

Page 5: chapter 1 An Overview of JSP - Pearson Education · back of using Java servlets is in their power. The Java program-ming language is at once both powerful and complicated, and learning

1.2 JavaServer Pages 5

FIGURE 1–2 A Server Page into HTML Data

FIGURE 1–3 A Simple JSP Page

ESSENTIAL NOTE: JSP PAGES WITHOUT JSP TAGSSince a JSP can handle the same static data as an HTML file, anyHTML file can be changed to the extension .jsp. If the JSP server isrunning, these files will now run through the JSP engine. Withoutspecific tags to identify dynamic sections, this document will comeout exactly the same as the original HTML file, but it will take moreresources because the JSP engine will attempt to parse and executethe file.

extension is configurable, but for the sake of clarity, this bookwill use .jsp throughout.

ch01.qxd 6/15/01 8:21 AM Page 5

Page 6: chapter 1 An Overview of JSP - Pearson Education · back of using Java servlets is in their power. The Java program-ming language is at once both powerful and complicated, and learning

� 1.3 The Power of Java

JSP inherits many of its advantages from the underlying Javaprogramming language and Java servlet technology. It also hasadvantages over alternate methods of development by integrat-ing into the component model. Beyond these advantages, the JSPspecification is extremely well designed, enabling extensibilityand integration with other languages and specifications.

1.3.1 Write Once, Run AnywhereBecause JSP utilizes the Java programming language, it automat-ically has many advantages. First and foremost is the high levelof portability offered by Java’s well-defined and accepted API. AJSP page developed on one platform can be deployed on a largenumber of systems. For example, a JSP page developed on a Win-dows NT system tested on the JSP Reference Implementation canbe easily deployed on a Linux box running Allaire Software’sJRun Application Server.

Further, JSP avoids the few troublesome areas of cross-platform Java development. Since JSP pages run on the server,applications do not need to be tested with several different clientplatforms, as is often necessary with Java applets. The sometimestroublesome GUI systems developed in Java, such as AWT andSwing, are also avoided in JSP.

1.3.2 The Java APIProbably one of the first things you’ll notice when writing JSP isthat you have the full power of the Java API. The core Java APIoffers the power of networking, multithreading, database connec-tivity, internationalization, image manipulation, object serializa-tion, remote method invocation, CORBA access, and more.Standard extensions to Java, such as the Java Naming and Direc-tory Interface (JNDI) and the Java Mail API, offer powerful exten-sions to Web applications.

With the Java classes, JavaBeans, and Enterprise JavaBeans (EJB) components offered by numerous software vendors,it is easy to add powerful code to Web applications. Using theJavaBeans component framework, JSP can form the presentationlayer of multitier applications.

JSP can be written to communicate directly to applets, allow-ing the same code to be leveraged on both the server and cli-ent. This opens a whole new world of client/server application development.

6 Chapter 1 • An Overview of JSP

ch01.qxd 6/15/01 8:21 AM Page 6

Page 7: chapter 1 An Overview of JSP - Pearson Education · back of using Java servlets is in their power. The Java program-ming language is at once both powerful and complicated, and learning

1.3.3 Security and SafetyAnother advantage inherited from the Java programming lan-guage is strong type safety. Unlike common scripting languages,JSP and the underlying Java Servlet API manipulate data in theirnative types instead of strings. Java also avoids many memory issues with automatic garbage collection and the absence ofpointers.

Java is also known for its excellent exception handling.When an error occurs, JSP can safely catch the exception and no-tify the user, instead of potentially crashing the server. This built-in feature is considered far superior to the add-on extensions andmodules often implemented in other Web application environ-ments.

Finally, a Java application server can utilize the Java securitymanager, protecting itself from poorly written JSP that could po-tentially affect server performance or damage the host file sys-tem. The Java security manager controls rights to resources thatcould be used to damage the system, only allowing processeswith the proper rights to gain access to protected resources. Thisis a fundamental part of the Java programming language.

1.3.4 ScalabilityThe Java programming language, as well as the Java Servlet API,adds several scalability components to JSP. After a JSP page isloaded, it generally is maintained in memory. When a new re-quest comes in for the JSP page, the server makes a simplemethod invocation. This is very different from traditional CGI ap-plications, which often spawn a process and an interpreter forevery request. The underlying server handles multiple requestsconcurrently by utilizing separate threads, making JSP highlyscalable.

When integrated into the JavaBean component framework,JSP becomes even more scalable. For example, a JDBC JavaBeancan handle multiple requests from JSP and maintain a single, ef-ficient connection to the back-end database. This is especially ef-ficient when integrated with Enterprise JavaBeans, which addtransaction and security services to Web applications, as well asmiddleware support for Java components.

1.3.5 ExtensibilityAnother area where JSP often outshines its competitors is in itsextensibility. The JSP specification itself is an extension of theJava servlet extension. Within JSP, the specification can be

1.3 The Power of Java 7

ch01.qxd 6/15/01 8:21 AM Page 7

Page 8: chapter 1 An Overview of JSP - Pearson Education · back of using Java servlets is in their power. The Java program-ming language is at once both powerful and complicated, and learning

extended to create custom tags. These tags allow the JSP “lan-guage” to be extended in a portable fashion. One good idea, forexample, would be to create a custom tag library filled with em-bedded database queries. By making these tag libraries portable,and by giving them a common interface, JSP can express thecomponent model internally.

The JSP specification authors also left room for further exten-sibility by making the elements utilized by JSP independent ofany certain scripting language. Currently the JSP specificationonly supports the Java programming language for scripting, butJSP engines can choose to support other languages.

JSP’s close relationship to the Extensible Markup Language(XML) is also very important, due to the extensibility and highlyorganized structure of XML. A properly formed JSP page can ac-tually be written as a valid XML document. Simple XML genera-tion can be done in JSP by using static templates. Dynamic XMLgeneration can be done with custom tag components, JavaBeans,or Enterprise JavaBean components. XML can also be received asrequest data and sent directly to custom tag components, Jav-aBeans, or Enterprise JavaBean components.

1.3.6 ComponentsAn extremely powerful feature of JSP is its ability to integrate intothe JavaBean component framework. This opens the door forlarge-scale, enterprise applications created by developmentteams. As Web applications become more complex, utilizing thecomponent nature of JSP helps break down the complex tasksinto simpler, manageable modules. JSP helps separate presenta-tion logic from business logic, and allows the separation of staticand dynamic data.

Because of this component-centric nature, both Java pro-grammers and non-Java programmers alike can utilize JSP. It al-lows Java programmers to make and use JavaBeans, and tocreate dynamic Web pages utilizing fine control over thosebeans. Non-Java programmers can use JSP tags to connect toJava-Beans created by experienced Java developers.

� 1.4 Recap

In closing, JSP offers significant benefits over legacy Web devel-opment technologies. Its use of the Java programming lan-guage gives it security, reliability, and access to a powerful API. Its memory and threading models offer significant speed

8 Chapter 1 • An Overview of JSP

ch01.qxd 6/15/01 8:21 AM Page 8

Page 9: chapter 1 An Overview of JSP - Pearson Education · back of using Java servlets is in their power. The Java program-ming language is at once both powerful and complicated, and learning

enhancements. Additionally, JSP is built around a componentmodel that helps separate business logic from presentationlogic.

The next chapter, “Scripting Elements,” takes an in-depthlook at the JSP language itself.

1.4 Recap 9

ch01.qxd 6/15/01 8:21 AM Page 9

Page 10: chapter 1 An Overview of JSP - Pearson Education · back of using Java servlets is in their power. The Java program-ming language is at once both powerful and complicated, and learning

ch01.qxd 6/15/01 8:21 AM Page 10