You are on page 1of 8

Java Server Pages Introduction to java server pages 1.

In late 1999, Sun Microsystems added a new element to the collection of Enterprise Java tools Java Server Pages !JSP". #. Java Server Pages are $uilt on top of Java servlets and are designed to increase the efficiency in which programmers, and even nonprogrammers, can create we$ content. %. JSP supports a powerful model for developing we$ applications that separates presentation from processing. &. JSP includes features li'e accessing Java(eans components, separating the processing over multiple pages to increase reusa$ility and simplify maintenance, and sharing information $etween pages, re)uests, and users. *. +he Java # Enterprise Edition !J#EE" has ta'en the tas' of $uilding an Internet presence and transformed it to the point where developers can use Java to efficiently create multi,tier, server,side applications and also supports servlets. What Is Java Server Pages? 1. Java Server Pages is a technology for developing we$ pages that include dynamic content. #. -nli'e a plain .+M/ page, which contains static content that always remains the same, a JSP page can change its content $ased on any num$er of varia$le items, including the identity of the user, the user0s $rowser type, information provided $y the user, and selections made $y the user. %. 1 JSP page contains standard mar'up language elements, such as .+M/ tags, 2ust li'e a regular we$ page. .owever, a JSP page also contains special JSP elements that allow the server to insert dynamic content in the page. &. JSP elements can $e used for a wide variety of purposes, such as retrieving information from a data$ase or registering user preferences. 3hen a user as's for a JSP page, the server e4ecutes the JSP elements, merges the results with the static parts of the page, and sends the dynamically composed page $ac' to the $rowser,

*. JSP defines a num$er of standard elements useful for any we$ application, such as accessing Java(eans components, passing control $etween pages, and sharing information $etween re)uests, pages, and users.

5. Programmers can also e4tend the JSP synta4 $y implementing application,specific elements that perform tas's such as accessing data$ases and Enterprise Java(eans, sending email, and generating .+M/ to present application,specific data. 6. +he com$ination of standard elements and custom elements allows for the creation of powerful we$ applications. Note +o run 2ava server pages 7opy all files in 7 82#sd'1.&.#9:&8li$ to 7 8Program ;iles81pache Software ;oundation8+omcat *.:8common8li$ Embedding Elements in HTM Pages 1. JSP lets you em$ed speciali<ed code !sometimes called scripting code" into .+M/ ages. #. Java is the default scripting language of JSP, $ut the JSP specification allows for other languages as well, such as JavaScript, P=E/, and >(Script. !sing the "ight Person #or Each Tas$ 1. JSP allows you to separate the mar'up language code, such as .+M/, from the programming language code used to process user input, access data$ases, and perform other application tas's. #. ?ne way this separation ta'es place is through the use of the JSP standard and custom action elements the elements are implemented with programming code and used the same way as page mar'up elements in regular we$ pages. %. 1nother way is to com$ine JSP with other Java Enterprise technologies. ;or e4ample, Java servlets can handle input processing, Enterprise Java (eans !EJ(" can ta'e care of the application logic, and JSP pages can provide the user interface. Pre%compilation 1. 1dvantage of JSP page is always compiled $efore it processed $y the server. #. JSP gets around this pro$lem $y compiling each JSP page into e4ecuta$le code the first time it is re)uested, and invo'ing the resulting code directly on all su$se)uent re)uests. %. 3hen coupled with a persistent Java virtual machine on a JSP,ena$led we$ server, this allows the server to handle JSP pages much faster. Java servlet template engines 1. 1 Java servlet template engine is another technology for separating presentation from processing. #. 3hen servlets $ecame popular, it didn0t ta'e long $efore developers reali<ed how hard it was to maintain the presentation part when the .+M/ code was em$edded directly in the servlet0s Java code. %. 1s a result, a num$er of so,called template engines have $een developed as open source products to help get .+M/ out of the servlets. &. +hese template engines are intended to $e used together with pure code components !servlets" and use only we$ pages with scripting code for the presentation part.

*. =e)uests are sent to a servlet that processes the re)uest, creates o$2ects that represent the result, and calls on a we$ page template to generate the .+M/ to $e sent to the $rowser. +he template contains scripting code similar to the alternatives descri$ed earlier. 5. +he scripting languages used $y these engines are less powerful, however, $ecause scripting is intended only for reading data o$2ects and generating .+M/ code to display their values. The JSP &dvantage JSP 1.1 com$ines the most important features found in the alternatives 1. JSP supports $oth scripting and element,$ased dynamic content, and allows programmers to develop custom tag li$raries to satisfy application,specific needs. #. JSP pages are precompiled for efficient server processing. %. JSP pages can $e used in com$ination with servlets that handle the $usiness logic, the model supported $y Java servlet template engines. &. JSP is a specification, not a product. +his means vendors can compete with different implementations, leading to $etter performance and )uality. *. JSP is an integral part of J#EE, a complete platform for Enterprise class applications. The Problem 'ith Servlets In many Java servlet,$ased applications, processing the re)uest and generating the response are $oth handled $y a single servlet class. I(e( servlet program)presentation logic *"e+uest processing,-business logic E.ample pu$lic class ?rderServlet e4tends .ttpServlet @ pu$lic void doAet!.ttpServlet=e)uest re)uest,.ttpServlet=esponse response" throws ServletE4ception, I?E4ception @ response.set7ontent+ype!Bte4tChtmlB"D Print3riter out E response.get3riter! "D if!is?rderInfo>alid!re)uest"" @ save?rderInfo!re)uest"D out.println!BFhtmlGB"D out.println!B FheadGB"D out.println!B FtitleG?rder 7onfirmationFCtitleGB"D out.println!B FCheadGB"D out.println!B F$odyGB"D out.println!B Fh1G?rder 7onfirmationFCh1GB"D render?rderInfo!re)uest"D out.println!B FC$odyGB"D out.println!BFChtmlGB"D H H H

Note 1. +he point is that the servlet contains re)uest processing and $usiness logic !implemented $y methods such as is?rderInfo>alid !" and save?rderInfo !"" and also generates the response .+M/ code, em$edded directly in the servlet code using println !" calls. #. 1 more structured servlet application isolates different pieces of the processing in various reusa$le utility classes, and may also use a separate class li$rary for generating the actual .+M/ elements in the response. /ut even so0 the pure servlet%based approach still has a #e' problems 1. Ietailed Java programming 'nowledge is needed to develop and maintain all aspects of the application, since the processing code and the .+M/ elements are lumped together. #. 7hanging the loo' and feel of the application, or adding support for a new type of client !such as a 3M/ client", re)uires the servlet code to $e updated and recompiled. %. It0s hard to ta'e advantage of we$ page development tools when designing the application interface. If such tools are used to develop the we$ page layout, the generated .+M/ must then $e manually em$edded into the servlet code, a process that is time, consuming, error,prone, and e4tremely $oring. Note 1. 1dding JSP to the pu<<le lets you solve these pro$lems $y separating the re)uest processing and $usiness logic code from the presentation. #. Instead of em$edding .+M/ in the code, you place all static .+M/ in JSP pages, 2ust as in a regular we$ page, and add a few JSP elements to generate the dynamic parts of the page. %. +he re)uest processing can remain the domain of servlet programmers, and the $usiness logic can $e handled $y Java(eans and Enterprise Java(eans !EJ(" components. &. +he separating the re)uest processing and $usiness logic from presentation ma'es it Possi$le to divide the development tas$s among people 'ith di##erent s$ills( *. Java programmers implement the re)uest processing and $usiness logic pieces, we$ page authors implement the user interface, and $oth groups can use $est,of,$reed development tools for the tas' at hand. +he result is a much more productive development process. 5. It also ma'es it possi$le to change different aspects of the application independently, such as changing the $usiness rules without touching the user interface. 6. +his model has clear $enefits even for a we$ page author without programming s'ills who is wor'ing alone. J. 1 page author can develop we$ applications with many dynamic features, using generic Java components provided $y open source pro2ects or commercial companies.

The &natom1 o# a JSP Page 1. 1natomy of a JSP page means structure of a JSP page #. 1 JSP page is simply a regular we$ page with JSP elements for generating the parts of the page that differ for each re)uest. %. Everything in the page that is not a JSP element is called template te4t. +emplate te4t can really $e any te4t .+M/, 3M/, KM/, or even plain te4t. &. Since .+M/ is $y far the most common we$ page language in use today, most of the descriptions and e4amples in this $oo' are .+M/,$ased, $ut 'eep in mind that JSP has no dependency on .+M/D JSP can be used 'ith an1 mar$up language( *. +emplate te4t is always passed straight through to the $rowser.

Lote 3hen a JSP page re)uest is processed, the template te4t and the dynamic content generated $y the JSP elements are merged, and the result is sent as the response to the $rowser. JSP Processing 1. 1 JSP page cannot $e sent as,is to the $rowser, the server must first process all JSP elements. +his is done $y turning the JSP page into a servlet, and then e4ecuting the servlet. #. Just as a we$ server needs a servlet container to provide an interface to servlets, the server needs a JSP container to process JSP pages. +he JSP container is often implemented as a servlet configured to handle all re)uests for JSP pages. In fact, these two containers , a servlet container and a JSP container , are often com$ined into one pac'age under the name we$ container !as it is referred to in the J#EE documentation". 3e$ containerE servlet containerM JSP container. %. 1 JSP container is responsi$le for converting the JSP page into a servlet !'nown as the JSP page implementation class" and compiling the servlet. +hese two steps form the translation phase. &. +he JSP container automatically initiates the translation phase for a page when the first re)uest for the page is received. *. +he translation phase ta'es a $it of time, of course, so a user notices a slight delay the first time a JSP page is re)uested. 5. +he translation phase can also $e initiated e4plicitlyD this is referred to as pre, compilation of a JSP page. Pre,compiling a JSP page avoids hitting the user with this delay. 6. +he JSP container is also responsi$le for invo'ing the JSP page implementation class to process each re)uest and generate the response. +his is called the re)uest,processing phase. J. 1s long as the JSP page remains unchanged, any su$se)uent processing goes straight to the re)uest,processing phase !i.e., it simply e4ecutes the class file". 3hen the JSP page is modified, it goes through the translation phase again $efore entering the re)uest, processing phase. 9. So JSP page is really 2ust another way to write a servlet without having to $e a Java programming wi<. 1nd, e4cept for the translation phase, a JSP page is handled e4actly li'e a regular servlet it0s loaded once and called repeatedly, until the server is shut down. 1:. (y virtue of $eing an automatically generated servlet, a JSP page inherits all of the advantages of servlets li'e platform and vendor independence, integration, efficiency, scala$ility, ro$ustness, and security. . Note 1. 1s $efore, don0t worry a$out the details if you0re not a Java programmer. +he service !" method is the method called $y the servlet container every time the servlet is re)uested.

+he method generates all .+M/ code, using the println !" method to send the strings to the $rowser. #. +here0s no way you could use a we$ development tool to develop this type of em$edded .+M/, ad2ust the layout with immediate feed$ac', verify that lin's are intact, etc. +his e4ample is so simple that it doesn0t really matter, $ut imagine a comple4 page with ta$les, aligned images, forms, some JavaScript code, etc., and you see the pro$lem.

A JSP page is a regular HTML page, except that it may also contain JSP elements like the highlighted element in this example. This element inserts the same Java code in the page as as used in the servlet to add the current date and time. !" you compare this JSP page to the corresponding servlet, you see that the JSP page can #e developed using any e# page editor that allo s you to insert extra, non$HTML elements. And the same tool can later #e used to easily modi"y the layout. This is a great advantage over a servlet ith em#edded HTML.

You might also like