Problem Solution Discussion Using Tomcat to Run Web Scripts

A st andard inst allat ion of Pyt hon includes cgi and urllib m odules t hat are useful for web program m ing. However, we dont act ually need t hem yet , because t he only web- relat ed act ivit y of our first Pyt hon web script is t o generat e som e sim ple HTML. Heres a Pyt hon version of t he MySQL st at us script : usrbinpython show_tables.py - Issue SHOW TABLES query, display results import sys sys.path.insert 0, usrlocalapachelibpython import MySQLdb import Cookbook Print header, blank line, and initial part of page print Content-Type: texthtml html head titleTables in cookbook Databasetitle head body bgcolor=white pTables in cookbook database:p Connect to database, display table list, disconnect conn = Cookbook.connect cursor = conn.cursor cursor.execute SHOW TABLES for tbl_name, in cursor.fetchall : print tbl_name + br cursor.close conn.close Print page trailer print body html Put t he script in Apaches cgi- bin direct ory and invoke it like t his: ht t p: apache.snake.net cgi-bin show_t ables.py

16.4 Using Tomcat to Run Web Scripts

16.4.1 Problem

You want t o run Java- based program s in a web environm ent .

16.4.2 Solution

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