Java Web Scripting and Apache Tomcat
Total Page:16
File Type:pdf, Size:1020Kb
WIRTSCHAFTSUNIVERSITÄT WIEN BAKKALAUREATSARBEIT Titel der Bakkalaureatsarbeit: Java Web Scripting und Apache Tomcat Englischer Titel der Bakkalaureatsarbeit: Java Web Scripting and Apache Tomcat Verfasser: Stanislav Ryabenkiy Matrikel-Nr.: 0350436 Studienrichtung: Wirtschaftsinformatik Kurs: 0675 Wirtschaftsinformatik Projektseminar Textsprache Englisch BetreuerIn: Prof. Mag.Dr. Rony G. Flatscher Unternehmen: Wirtschaftsuniversität Wien Ich versichere: dass ich die Bakkalaureatsarbeit selbstständig verfasst, andere als die angegebenen Quellen und Hilfsmittel nicht benutzt und uns auch sonst keiner unerlaubten Hilfe bedient habe. Dass ich die Ausarbeitung zu dem obigen Thema bisher weder im In- noch im Ausland (einer Beurteilerin/ einem Beurteiler zur Begutachtung) in irgendeiner Form als Prüfungsar- beit vorgelegt habe. Dass diese Arbeit mit der vom Betreuer beurteilten Arbeit übereinstimmt. Datum Unterschrift RYABENKIY Abstract In a world of increasing hardware capabilities, agile development meth- ods and Web 2.0, scripting languages play an increasingly important role. This paper explores the synergy of scripting languages and web development on the Java platform through literature review and practi- cal work. KEYWORDS:JAVA,SCRIPTING,WEB DEVELOPMENT,BEAN SCRIPT- ING FRAMEWORK, JSR-223, TOMCAT In einer Welt geprägt von mächtiger werdenden Hardware, agiler Soft- wareentwicklung, und Web 2.0 spielen Skriptsprachen eine zunehmend wichtige Rolle. Diese Arbeit erforscht die Synergie von Skriptsprachen und Webentwicklung auf der Java Platform durch Literaturübersicht und praktische Arbeit. STICHWÖRTER:JAVA,SCRIPTING,WEB DEVELOPMENT,BEAN SCRIPT- ING FRAMEWORK, JSR-223, TOMCAT 2 RYABENKIY Contents 1 Introduction 7 1.1 Methodology . .8 1.2 Structure . .8 2 Java Scripting 10 2.1 Scripting Languages . 11 2.1.1 Compiler Architecture . 12 2.1.2 Variable Typing . 14 2.1.3 Other Advantages . 15 2.2 Scripting in Java . 17 2.2.1 Bean Scripting Framework . 19 2.2.2 Scripting for the Java Platform (JSR-223) . 20 3 Java Web Development 22 3.1 Apache Tomcat . 22 3.2 Servlet Specification . 25 3.3 JavaServer Pages Specification . 29 3.3.1 JSP Architecture . 30 3.3.2 Tag Libraries . 32 4 Java Web Scripting 34 4.1 The BSF Tag Library . 34 4.1.1 Architecture . 34 4.1.2 Deployment . 36 4.1.3 Usage . 39 4.2 The Script Tag Library . 39 4.2.1 Architecture . 40 4.2.2 Deployment . 40 4.2.3 Usage . 41 4.3 RexxScriptEngine . 42 4.3.1 Architecture . 43 4.3.2 Deployment . 44 4.3.3 Usage . 45 3 RYABENKIY 4.4 QuercusEngine . 45 4.4.1 Architecture . 46 4.4.2 Deployment . 46 4.4.3 Usage . 48 5 Example Application 49 5.1 Architecture . 50 5.2 Interface . 55 6 Conclusions 57 References 59 Glossary 63 A Script Tag Library for JSR-223 64 A.1 Tag Library Descriptor . 64 A.2 ExpressionTag Class . 66 A.3 ScriptletTag Class . 70 B RexxScriptEngine for JSR-223 74 B.1 Service Descriptor . 74 B.2 RexxScriptEngine Class . 74 B.3 RexxScriptEngineFactory Class . 82 C QuercusEngine for BSF 86 C.1 Modified BSF 2.4 Descriptor . 86 C.2 QuercusEngine Class . 86 4 RYABENKIY Figures 1 Compiler architecture (Bosanac 2007) . 12 2 Interpreter Architecture (Bosanac 2007) . 13 3 Hybrid compiler-interpreter architecture (Bosanac 2007) . 14 4 A selection of Java scripting languages . 18 5 Internals of the Scripting Engine (Zhang 2008) . 21 6 Generic configuration (Turk 2008) . 23 7 Secure generic configuration (Turk 2008) . 24 8 Load balancing configuration (Turk 2008) . 25 9 Servlet-based web application workflow . 26 10 Request/Response flow calling as JSP page (Mahmoud 2003) . 31 11 JSP Model 1 architecture (Mahmoud 2003) . 32 12 JSP Model 2 architecture (Mahmoud 2003) . 32 13 Architecture of Birthday Manager application . 51 14 Birthday Manager - Contact List . 55 15 Birthday Manager - New Contact . 56 16 Birthday Manager - Edit Contact . 56 5 RYABENKIY Listings 1 itoa() in x86 assembly . 10 2 itoa() in Java . 11 3 Printing a list in Python (Bosanac 2007) . 16 4 Printing a list in Java 1.4 (Bosanac 2007) . 16 5 Runtime code generation in Python (Bosanac 2007) . 17 6 Higher-order functions in Python (Bosanac 2007) . 17 7 Incompatible interfaces (Roth 2007) . 19 8 Sample HTTP servlet code . 27 9 JSP example . 30 10 Part of a translated JSP page . 31 11 Setting language using JSP page directive . 34 12 doEndTag() of scriptlet class . 35 13 Custom Ant script for building BSF taglib . 36 14 BSF taglib entry in WEB-INF/web.xml . 38 15 BSF taglib usage . 39 16 Script taglib entry in WEB-INF/web.xml . 41 17 Script taglib usage . 42 18 The eval() method of RexxScriptEngine . 43 19 Usage of RexxScriptEngine . 45 20 Using QuercusEngine . 48 21 Part of JYTHONCONTROLLER front controller HTTP GET dispatch . 52 22 Excerpt of PERSONSERVICE CRUD methods . 52 23 delete_do.jsp in PHP . 53 24 edit_do.jsp in Rexx . 54 6 RYABENKIY 1 Introduction As hardware capabilities and user expectations continue rising, soft- ware is becoming increasingly complex. Scripting languages help meet these increasingly complex environments by increasing developer pro- ductivity. Google trends show that searches involving scripting lan- guage names are on the rise (Zhang 2008). Additionally, the well estab- lished Java platform provides advanced web development capabilities. The popularity of Java for web development is dwarfed by the ubiq- uity of the PHP web development scripting language - in 2010, J2EE was present on 7.87% of domains compared to 30.32%, according to BuiltWith.com 1. The purpose of this paper is to explore the synergy of scripting lan- guages and web development on the Java platform and to fill a number of perceived gaps through practical work by extending better support for scripting languages to the Java web development environment. Better support for scripting languages in the Java web development environ- ment can provide more choice for developers, leverage existing knowl- edge of scripting language programmers and ease their transition into Java, and counteract vendor lock-in by allowing developers from other languages to apply their knowledge in a Java environment. Practical evidence suggests that providing a choice of scripting language in such an environment has been on the mind of bigger software companies - Microsoft’s Active Server Pages (ASP.NET) technology can run other script languages via Component Object Model (COM) interfaces, and IBM provides a Java-based dynamic scripting environment for enter- prise development via WebSphere sMash, which is scriptable using 1http://trends.builtwith.com/framework 7 RYABENKIY PHP and Groovy. 1.1 Methodology This paper consists of a literature review followed by practical work. The initial sections of the paper introduce the theoretical concepts of scripting and web development on the Java platform, while the latter sections discuss the author’s various implementations extending the available functionality at the intersection of these two areas as well as the example application that demonstrates this functionality. The code of the various implementations can be found in the appendix and is made available under open source licenses. 1.2 Structure Section 2 introduces basic concepts and advantages of scripting as well as the relevance of scripting for the Java platform. It also discusses the Bean Scripting Framework (BSF) and the Java Specification Request 223 (“JSR-223 Scripting for the Java Platform”). Section 3 briefly intro- duces web development from a Java perspective. It discusses the ba- sics of core technologies such as the Servlet API and the JavaServer Pages API (JSP), especially the Tag Extension mechanism, which is relevant to further work in the paper. It also introduces Apache Tomcat, a Servlet container that was used extensively in this project. Section 4 bridges the gap between web development and scripting on the Java platform. After introducing the already existing BSF Tag Library, this section discusses the author’s Script Tag Library, QuercusEngine for 8 RYABENKIY BSF 2.4, and RexxScriptEngine for JSR-223. Section 5 introduces an example application that demonstrates the basic functionality of both the BSF Tag Library and the Script Tag Library in Rexx, PHP, and Python. 9 RYABENKIY 2 Java Scripting At the most basic level, computers manipulate data using machine in- structions. An operation code, or opcode, tells the processor what task to perform on some operands. These commands are represented as sequences of bits. The instruction set of such sequences understood by a given processor is called machine language (Bosanac 2007). Ev- ery transistor-based computer2 in use today is based on these princi- ples, regardless of the actual programming language used. However, increasing hardware functionality and affordability leads to increasing expectations from users (Bosanac 2007). The first generation programming languages, machine languages, are barely readable and writeable by humans. Second-generation assem- bly languages were introduced which provided human-readable sym- bols for sequences of bits (Bosanac 2007). While more readable, these were still quite verbose and do not extend the capabilities of the pro- cessor. For instance, listing 1 demonstrates a function that converts an integer into a null-terminated ASCII string using the x86 instruction set. Listing 1: itoa() in x86 assembly itoa:; partially borrowed from Brian Raiter’s ls push eax; store number to display _mov ecx, 6 _mov al,’’ rep stosb; edi is filled with two spaces pop eax push edi; save end of string pointer mov cl, 10 .decloop: cdq 2as opposed to quantum computers, which operate using qubits 10 RYABENKIY div ecx add dl,’0’ dec edi mov[edi], dl or eax,eax jnz .decloop pop edi; returns position after string ret Higher-level, third generation programming languages add more and more abstract concepts, such as powerful loops, advanced data struc- tures, and objects, making it easier to develop applications. Such a language will often provide necessary functionality such as the integer conversion function above as part of its standard library or built into the language, making it a simple operation (see listing 2).