Java Technologies for Web Applications
Total Page:16
File Type:pdf, Size:1020Kb
1111//3300//22001100 JJaavvaa TTeecchhnnoollooggiieess ffoorr WWeebb AApppplliiccaattiioonnss http://java.sun.com/developer/technicalArticles/tools/w ebapps_1/ Nov 30,2 010 Article Java Technologies for Web Applications By Dana Nourie, November 2006 YYouou can find wwebeb applications eveveryerywherewhere on the IntInternet.ernet. MManyany of tthesehese web applapplicationsications are uusedsed to process online forms ,, tally vvotingoting pollspolls,, taktakee orders within online stores, allow users to book a flight for ttheirheir next busibusinessness trip, or simpsimplyly to display the user's lloginogin namname.e. Which JavJavaa technologies do yoyouu need to creatcreatee a web application and which papackagesckages do you need to import? The answer depends on what kind of appliapplicationcation yyou'reou're building and whawhatt it does. This article provides an overview of the Java technologies involved in creating various types of web applications, tells you when you might decide to ususee them, and provides linlinksks to get yyouou sstarttarted.ed. FutFutureure articles will covcoverer how to ususee these technologies in buibuildinglding web appapplications.lications. To use the technologies desdescribedcribed in thithiss article, download JavJavaa Development Kit (JDK) 5.0 Update 9 with Java Platform, Enterprise Edition (Java EE) or later and install it on your computer. BeforBeforee you begin coding, you need to understand wwhathat a wwebeb application isis,, what JavJavaa technologies are avaavailableilable for use, and what devdevelopmelopmentent ttoolsools can save yyouou time and make creating applications easeasier.ier. Contents What Is a Web Application? JavJavaa Technologies to Use in Web ApplicatApplicationsions OtOtherher Technologies to ConsConsider ider DevDevelopmelopmentent Tools What Is a Web Application? Web applications are by naturnaturee disdistributtributeded applications, mmeaningeaning that they are programs that rrunun on m ore than one computer and comcommunmunicateicate tthroughhrough a network or seservrver.er. SpecSpecificallyifically,, web applications are accessaccesseded with a wewebb browsbrowser er and are popular bebecausecause of the easeasee of usiusingng the browsebrowserr as a ususerer client. FForor the enterprenterprise,ise, the ability ttoo update and maimaintainntain web applications wiwithoutthout deploydeployinging and ins talling software on potentpotentiallyially tthousanhousandsds of client computers is a key reason for their popularitpopularityy.. WWebeb applications are ususeded for web mail, online retairetaill salessales,, discussdiscussionion boardsboards,, weblogs, online banking, and more. OOnene web application can be accesaccessedsed and us ed by mimillionsllions of people. Like desktop applications, web applications are mmadadee up of many parts and ofteoftenn contain miniprogramminiprograms,s, sosomeme of which havhavee ususerer interfinterfaces,aces, and ssomeome of which do not require a graphical us er interfinterfaceace (GUI(GUI)) at all. In addition, web applications frequentfrequently rly requireequire an additional mmarkuparkup or sscriptcriptinging lanlanguage,guage, such as HTMHTML,L, CSS, or JavJavaScriptaScript proprogrammgramm ing languagelanguage.. A Alslso,o, many applications ususee only tthehe JavJavaa programprogrammimingng langulanguage,age, which is ideal bbecausecausee of its vversersatility.atility. A web appapplicationlication can be as ssimimpleple as a page that showshowss the current date and time or as comcomplexplex as a ssetet of pages onon which you can look up and book the most convenient flight, hotels, and car rentals for your next vacation. The Java technologies you'll use to create web applications are a part of the Java EE platform, in addition to many of the JavJavaa Platform, StStandardandard Edition (Jav(Javaa SE) class es and packages. In order for mmanyany of tthesehese technologies to wworkork on a serverserver,, the server musmustt havhavee a container, or web server, installed that recrecognizognizeses and runs the class es yyouou create. FFor or devdevelopmelopmentent and testing of these technologiestechnologies, y, youou can uuss e the tools detailed iinn this articarticle,le, but when yyouou deploy, make sure that the server has Java server software installed to run Java technology-based web applications. If you don't have access to this information, ask the servserverer admiadministrator.nistrator. Java TecTechnohnologilogieses to Use in WWebeb ApplApplicationsications There are too many JavJavaa technologies to list in one article, so this article will describe only the ones m ost frequently used. The number of technologies lis ted here can appear ooverwhelmiverwhelming.ng. Keep in mind that yyouou will not need to ususee them all. In aavvaa..ssuunn..ccoomm//jjsspp__uuttiillss//PPrriinnttPPaaggee..jjsspp??uurr…… 11//77 1111//3300//22001100 JJaavvaa TTeecchhnnoollooggiieess ffoorr WWeebb AApppplliiccaattiioonnss fact, a web application often consists of nothing more than one page created with the JavaServer Pages (JSP) technology. SometimSometimeses you will comcombinebine three or more ssuchuch technologies. No mattatterm er how mmanyany yyouou end up ususing,ing, it'it'ss good to know what is avavailableailable to you and how you can use each oonene in a web application. Java Servlet API The Java Servlet API lets you define HTTP-specific classes. A servlet class extends the capabilities of servers that host applications that are accessed by way of a request-request-responseresponse programmprogramminging mmodel.odel. AAlthoughlthough servservletslets can resrespondpond to any tytypepe of requesrequest, tt, theyhey araree comcommmonlyonly used to extextendend the appliapplicationscations hoshostedted by web serverservers.s. For insinstance,tance, yyouou mmightight use aa servlet to get the text input from an online form and print it back to the screen in an HTML page and format, or you might use a different servservletlet to write the data to a file or databasdatabasee insinstead.tead. A servlet runs on the s erververer sisidede -- without an apapplicationplication GUI or HTMHTMLL ususerer interfacinterfacee (UI) of its owown.n. JavJavaa ServServletlet exextensionstensions mamakeke mmanyany web applications pos sisible.ble. Figure 1 sshowshows clients talking to Java ServServletlet exextensionstensions.. Clients m ay rrangeange in complexitcomplexity fy fromrom simsimpleple HTML forformsms toto sophissophisticatticateded Java tectechnology-hnology-basedbased applets. Figure 1: Servlet Model The javax.servlet and javax.servlet.http packages provprovideide the clasclassesses and interfacinterfaceses to define seservrvlets.lets. HTMHTMLL servlet classes exextendtend the javax.servlet.http.HttpServlet abstract class, which provides a framework for handling HTTP protocol. Learn more about Java Servlet extensions and navigating with servlets. JavaServer Pages Technology JavaServer Pages (JSP) technology provides a simplified, fast way to create dynamic web content. JSP technology enables rapid development of weweb-basedb-based applications that are server- and platform-independplatform-independent.ent. JSP ttechechnologynology lets you add snippets of servlet code directly into a text-based document. Typically, a JSP page is a text-based document that contains two types of text: Static data, which can be expressed in any text-based format, such as HTML, Wireless Markup Language (WML), or JSPXXMMLL tetechnologychnology elements, which determine how the page consconstructructsts dynamic content The packages involvinvolveded in creatcreatinging JSP pages are javax.el,, javax.servlet.jsp,, javax.servlet.jsp.el, and javax.servlet.jsp.tagext, though yyouou will rarely havhavee to imimportport these directlydirectly.. A JSP page can be as sim ple as a bit of HTML with one snippet of JSP code and the .jsp ex extensiontension of the page nam e.e. aavvaa..ssuunn..ccoomm//jjsspp__uuttiillss//PPrriinnttPPaaggee..jjsspp??uurr…… 22//77 1111//3300//22001100 JJaavvaa TTeecchhnnoollooggiieess ffoorr WWeebb AApppplliiccaattiioonnss For instance, yyouou can creatcreatee a wwebeb s ite of JSP ttechnologyechnology pages that use one ssnippetnippet of code to include the header.html file, which contains the site navigation. This way, when you change a link to a button in the navigation, you make the change in only one file, and that ffileile loads into all the pages on the ssiteite that havhavee this code snipsnippet:pet: <%@ include file="header.html" %> That line of code works ververyy mmuchuch like a sserverver-sideer-side ininclude,clude, if yyouou are familfamiliariar with those. Because this web page is now a JSP page, yyouou could als o go on to add mmoreore JavJavaa technology code to creatcreatee dyndynamamicic web content, such as pollspolls,, forformms,s, ways to enter or retrieve data from a database, and so forth. Take a look at two helpful documents about creatcreatinging JSP pages: JavaServer Pages (JSP) v2.0 Syntax Reference JavJavaServeraServer Pages Technology -- Frequently AAskeskedd QuesQuestionstions JavaServer Pages StandStandardard Tag Library The JavaServer Pages Standard Tag Library (JSTL) encapsulates core functionality common to many JSP technology- based applications. Instead of mixing ttagsags from numnumerouserous vendors in your applicatapplications,ions, you employ a sisinglengle sstandardtandard set of tags. This s tandardiztandardizationation allows yyouou to deploy yyourour applications on any JSP