Installing the mcb Application

Writ e program s using JSP not at ion and execut e t hem using a servlet cont ainer.

16.4.3 Discussion

As described in Recipe 16.3 , Apache can be used t o run Perl, PHP, and Pyt hon script s. For Java, a different approach is needed, because Apache doesnt serve JSP pages. I nst ead, well use Tom cat , a server designed for processing Java in a web environm ent . Apache and Tom cat are very different servers, but t here is a fam ilial relat ionship—Tom cat is part of t he Jakart a Proj ect , which is overseen by t he Apache Soft ware Foundat ion. This sect ion provides an overview of JSP program m ing wit h Tom cat , but m akes several assum pt ions: • You have a som e fam iliarit y wit h t he concept s underlying JavaServer Pages, such as w hat a servlet cont ainer is, what an applicat ion cont ext is, and what t he basic JSP script ing elem ent s are. • The Tom cat server has been inst alled so t hat you can execut e JSP pages, and you know how t o st art and st op it . • You are fam iliar wit h t he Tom cat webapps direct ory and how a Tom cat applicat ion is st ruct ured. I n part icular, you underst and t he purpose of t he WEB- I NF direct ory and t he web.xm l file. • You know what a t ag library is and how t o use one. I recognize t hat t his is a lot t o assum e, because t he use of JSP and Tom cat in t he MySQL world is not so widespread as t he use of our ot her languages wit h Apache. I f youre unfam iliar wit h JSP or need inst ruct ions for inst alling Tom cat , Appendix B provides t he necessary background inform at ion. Once you have Tom cat in place, you should inst all t he following com ponent s so t hat you can work t hrough t he JSP exam ples in t his book: • The mcb sam ple applicat ion locat ed in t he t om cat direct ory of t he recipes dist ribut ion. • A MySQL JDBC driver. You m ay already have one inst alled for use wit h t he script s in earlier chapt ers, but Tom cat needs a copy, t oo. • The JSP St andard Tag Library JSTL , which cont ains t ags for perform ing dat abase act ivit ies, condit ional t est ing, and it erat ive operat ions wit hin JSP pages. I ll discuss how t o inst all t hese com ponent s, provide a brief overview of som e of t he JSTL t ags, and t hen describe how t o writ e t he JSP equivalent of t he MySQL show- t ables script t hat was im plem ent ed in Recipe 16.3 using Perl, PHP, and Pyt hon.

16.4.4 Installing the mcb Application

Web applicat ions for Tom cat t ypically are packaged as WAR web archive files and inst alled under it s webapps direct ory, which is roughly analogous t o Apaches ht docs docum ent root direct ory. The recipes dist ribut ion includes a sam ple applicat ion nam ed mcb t hat you can use for t rying t he JSP exam ples described here. Look in t he dist ribut ions t om cat direct ory, w here you will find a file nam ed m cb.war. Copy t hat file t o Tom cat s webapps direct ory. Heres an exam ple inst allat ion procedure for Unix, assum ing t hat t he recipes dist ribut ion and Tom cat are locat ed at u paul recipes and usr local j akart a-t om cat . The com m and t o inst all m cb.war would look like t his: cp upaulrecipestomcatmcb.war usrlocaljakarta-tomcatwebapps For Windows, if t he relevant direct ories are D: \ recipes and D: \ j akart a-t om cat , t he com m and looks like t his: C:\ copy D:\recipes\tomcat\mcb.war D:\jakarta-tomcat\webapps Aft er copying t he m cb.war file t o t he webapps direct ory, rest art Tom cat . As dist ribut ed, Tom cat is configured by default t o look for WAR files under webapps when it st art s up and aut om at ically unpack any t hat have not already been unpacked. This m eans t hat copying m cb.war t o t he webapps direct ory and rest art ing Tom cat should be enough t o unpack t he mcb applicat ion. When Tom cat finishes it s st art up sequence, look under webapps and you should see a new m cb direct ory under which are all t he files cont ained in m cb.war. I f Tom cat doesnt unpack m cb.war aut om at ically, see t he sidebar Unpacking a WAR File Manually . I f you like, have a look around in t he m cb direct ory at t his point . I t should cont ain several files t hat client s can request using a browser. There should also be a WEB- I NF subdirect ory, which is used for inform at ion t hat is privat e—t hat is, available for use by script s in t he m cb direct ory, but not direct ly accessible by client s. Next , verify t hat Tom cat can serve pages from t he mcb applicat ion cont ext by request ing som e of t hem from your browser. The following URLs request in t urn a st at ic HTML page, a servlet , and a sim ple JSP page: ht t p: t om cat .snake.net : 8080 m cb t est .ht m l ht t p: t om cat .snake.net : 8080 m cb servlet Sim pleServlet ht t p: t om cat .snake.net : 8080 m cb sim ple.j sp Adj ust t he host nam e and port num ber in t he URLs appropriat ely for your inst allat ion. Unpacking a WAR File Manually WAR files are act ually ZI P- form at archives t hat can be unpacked using j ar, WinZip, or any ot her t ool t hat underst ands ZI P files. However, when unpacking a WAR file m anually, youll need t o creat e it s t op- level direct ory first . The following sequence of st eps show s one w ay t o do t his, using t he j ar ut ilit y t o unpack a WAR file nam ed m cb.war t hat is assum ed t o be locat ed in Tom cat s webapps direct ory. For Unix, change locat ion t o t he webapps direct ory, t hen issue t he following com m ands: mkdir mcb cd mcb jar xf ..mcb.war For Windows, t he com m ands are only slight ly different : C:\ mkdir mcb C:\ cd mcb C:\ jar xf ..\mcb.war Unpacking t he WAR file in t he webapps direct ory creat es a new applicat ion cont ext , so youll need t o rest art Tom cat before it not ices t he new applicat ion.

16.4.5 Installing the JDBC Driver