2. The Web Container Applying a Model/View/Controller Pattern in J2EE Platform Using Struts Framework.

134 consider bot h t echnical f orces such as t he net work and non-t echnical f orces such as t he nat ure of t he appl icat ion [ Bro03] .

3. 2. The Web Container

A t ypical Java Web appl icat ion consist s of a col l ect ion of JavaServl et s and JSPs t hat run inside a J2EE server’ s Web cont ainer. The cont ainer manages each component ’ s l if ecycl e, dispat ches service request s t o appl icat ion component s, and provides st andard int erf aces t o cont ext dat a such as session st at e and inf ormat ion about t he current request . [ Bro03] . JavaServl et s and JSPs are depl oyed in t he Web cont ainer and t ypical l y perf orms t he f ol l owing f unct ions in a J2EE appl icat ion: • Web-enables business l ogic - t hey manage int eract ion bet ween Web cl ient s and appl icat ion business l ogic. • Generat es dynamic cont ent - t hey generat e cont ent dynamically, in ent irel y arbit rary dat a f ormat s, incl uding HTML, images, sound, and video. • Present s dat a and collect s input - t hey t ranslat e HTTP PUT and GET act ions int o a f orm t hat t he business l ogic underst ands and present resul t s as Web cont ent . • Cont rols screen f low - The logic t hat det ermines which “ screen” t hat is, which page t o displ ay next usual l y resides in t hese component s, because screen f l ow t ends t o be specif ic t o cl ient capabil it ies. • Maint ains st at e - t hey have a simple, f lexible mechanism f or accumul at ing dat a f or t ransact ions and f or int eract ion cont ext over t he l if et ime of a user session. • Support s mult iple and f ut ure client t ypes - Ext ensible MIME t ypes describe Web cont ent , so a Web cl ient can support any current and f ut ure t ype of downl oadabl e cont ent . • May implement business logic - While many ent erprise appl icat ions impl ement business l ogic in ent erprise beans, Web-onl y, l ow- t o medium-vol ume appl icat ions wit h simpl e t ransact ional . Java Servlet s A Java Servl et is a Java cl ass t hat ext ends a J2EE-compat ibl e Web server. Each servl et cl ass produces dynamic cont ent in response t o service request s t o one or more URLs. Servl et s of f er some import ant benef it s over earl ier dynamic cont ent generat ion t echnol ogies. Servl et s are compil ed Java cl asses, so t hey are general l y f ast er t han CGI programs or server-side script s. Servl et s are saf er t han ext ension l ibraries, because t he Java Virt ual Machine JVM can recover f rom a servl et t hat exit s unexpect edl y. Servl et s are port abl e bot h at t he source-code l evel because of t he Java Servl et specif icat ion and at t he binary l evel because of t he innat e port abil it y of Java byt ecode. Servl et s al so provide a richer set of st andard services t han any ot her widel y adopt ed server ext ension t echnol ogy. Appl ying a Model View Cont rol l er Pat t ern in J2EE Plat f orm Using St rut s Framework Niko Ibrahim 135 Figure 4 shows a Java Servl et s t hat print s “ Hel l o Worl d” in a browser. Figure 4: A “ Hello World” Java Servlet JavaServer Pages JSP Most Web appl icat ions produce primaril y dynamic HTML pages t hat , when served, change onl y in dat a val ues and not in basic st ruct ure. For exampl e, al l of t he cat al og pages in an onl ine st ore may have ident ical st ruct ure and dif f er onl y in t he it ems t hey displ ay. JSP t echnol ogy exist s f or producing such cont ent [ Rod03] [ Bro03] . A JSP page is a document cont aining f ixed t empl at e t ext , pl us special markup f or incl uding ot her t ext or execut ing embedded l ogic. The f ixed t empl at e t ext is al ways served t o t he request er j ust as it appears in t he page, l ike t radit ional HTML. The special markup can t ake one of t hree f orms: direct ives, script ing el ement s, or cust om t ags al so known as “ cust om act ions” [ Sin02] . Figure 5 shows a JSP page t hat print s a “ Hel l o Worl d in a browser: Figure 5: A “ Hello World” JSP The f irst t ime t he JSP f il e is request ed, it is t ransl at ed int o a servl et and t hen compil ed int o an obj ect t hat is l oaded int o resident memory. The generat ed servl et t hen services t he request , and t he out put is sent back t o t he request ing cl ient . On al l subsequent request s, t he server checks t o see whet her t he original JSP source f il e has changed. If it has not changed, t he import java.io.; import javax.servlet.; import javax.servlet.http.; public class HelloWorld extends HttpServlet { public void doGetHttpServletRequest request, HttpServletResponse response throws ServletException, IOException { PrintWriter out = response.getWriter; out.printlnHello World; } } html hody p = Hello, world p body html 136 server invokes t he previousl y compil ed servl et obj ect . If t he source has changed, t he JSP engine re-parses t he JSP source. The st eps are as f ol l ow Figure 5: 1 Client request s a JSP page 2 The JSP Engine compiles t he JSP int o a Servlet 3 The generat ed servlet is compiled and loaded 4 The compiled servlet services t he request and sends a response back t o t he cl ient Figure 6: T he execut ion of a Java servlet 4. Development Frameworks Frameworks, l ike many t hings, are of t en born out of necessit y. In t he process of creat ing mul t ipl e appl icat ions, devel opers discover t hat t here are some areas of f unct ional it y t hat are common. These areas are t hen abst ract ed and general ized t o f orm t he f eat ures of t he f ramework. Frameworks provide benef it at many dif f erent l evel s of appl icat ion devel opment [ Sin02] [ Goo04] : • St ruct ure: f rameworks provide a st ruct ure f or applicat ion devel opment - a backbone t o buil d f rom, making t he devel opment process a l it t l e easier. They ensure t hat our proj ect incl udes al l of t he key el ement s such as securit y, dat abase access, UI, and so f ort h by providing an out l ine int o which t he det ail s of our part icul ar appl icat ion are f il l ed. • Services: a f ramework provides a wide array of services t o it s appl icat ions, f or use in bot h t he devel opment and depl oyment phases. These services very of t en begin wit h eit her a component cont ainer, or access t o a component cont ainer provided by t he appl icat ion server. • Complet eness: using a f ramework can expand t he horizons slight ly: if adding some of t he nice t o have f unct ional it y does not cost any Web Server 2 3 Web Browser JSP Page Generated Servlet Compiled Servlet 1 4 Request Response Appl ying a Model View Cont rol l er Pat t ern in J2EE Plat f orm Using St rut s Framework Niko Ibrahim 137 addit ional t ime, we can end up wit h a richer, more compl et e appl icat ion t hat is comparat ivel y more pol ished t han it coul d have been wit hout t he f ramework. An appl icat ion f ramework is dif f icul t t o creat e. It is dif f icul t , not so much because t he code is dif f icul t t o writ e, or t hat t here is a l arge amount of code. Indeed, many ent erprise appl icat ions have several t imes more code t han t he f rameworks t hat hel ped creat e t hem. The code of a f ramework is of t en compl ex; however, proper design hel ps t o make it underst andabl e. This compl exit y, t hough, is not t he reason f or f rameworks being so dif f icul t t o creat e. The dif f icul t y l ies in obt aining t he experience t hat is dist il l ed int o t hem. Frameworks evol ve f rom t he ef f ort t o reuse bot h design and code, and f rom repeat ed ref act oring t hat resul t s in widel y appl icabl e services and component s. This is what is dif f icul t about t heir creat ion. Because f rameworks t end t o be l arge and f airl y compl ex pieces of sof t ware, t hey are of t en not a cost -ef f ect ive proj ect f or a singl e organizat ion. It is al so a good t hing when a part icul ar f ramework is used in many proj ect s - it grows, it becomes a de f act o st andard t o a degree, and t he organizat ions using it get t he benef it s. Al l of t hese f act ors mean t hat f rameworks are good candidat es f or open-source proj ect s, and indeed many of t he best f rameworks are open source.

4. 1. Overview of Struts Frameworks