programming: java - cs.princeton.edu
TRANSCRIPT
![Page 1: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/1.jpg)
Server-Side Web Programming: Java
Copyright © 2021 byRobert M. Dondero, Ph.D
Princeton University
1
![Page 2: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/2.jpg)
Objectives
• We will cover:– Aside: Python lambda expressions– Some advanced Java features– Server-side web programming in Java, via…– Servlets– The Spark web app framework– The Velocity template engine
2
![Page 3: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/3.jpg)
Motivation
• Question: Why study Java server-side web programming?
• Answer 1: Help you better understand server-side web programming
• Answer 2: Give you a sense of how Java is used in the “real world”
3
Motivation
Question: Why study Java server-side web programming?
Answer 1: Doing so will help you better understand server-side web programmingHelp you get a sense of what is common across server-side web programming environments and what is language-specific
Answer 2: Doing so will give you a sense of how Java is used in the “real world”COS 126 uses Java to cover the fundamentals of programmingCOS 226 uses Java to cover algorithms & data structuresTo some extent, “real world” Java programming differs from the kind of academic programming that you did in 126 and 226.
![Page 4: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/4.jpg)
Motivation
• Question: Why study Java server-side web programming (cont.)?
• Answer 3: Show realistic use of some advanced features of Java
• Answer 4: Popular and important…
4
Motivation
Question: Why study Java server-side web programming (cont.)?
Answer 3: Doing so will introduce some advanced features of Java that might be new to you
Answer 4: Java server-side Web programming is popular and important…
![Page 5: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/5.jpg)
MotivationWebsite Front End Back EndGoogle.com JavaScript C, C++, Go, Java, PythonFacebook.com JavaScript Hack, PHP, Python, C++, Java,
Erlang, D, Xhp, HaskellYouTube.com JavaScript C, C++, Java, Python, GoYahoo JavaScript PHPAmazon.com JavaScript Java, C++, PerlWikipedia.org JavaScript PHP, HackTwitter.com JavaScript C++, Java, Scala, RubyBing JavaScript ASP.neteBay.com JavaScript Java, JavaScript, ScalaMSN.com JavaScript ASP.net
5
https://en.wikipedia.org/wiki/Programming_languages_used_in_most_popular_websites
![Page 6: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/6.jpg)
Motivation
-- http://blog.stoneriverelearning.com/ top-5-programming-languages-used-in-web-development/
6
Motivation
If you consider all websites that experience high traffic…
Java is used in 83.3% of them(Python and others) are used in 37.6% of them
![Page 7: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/7.jpg)
Agenda
• Aside: Python lambda expressions• Some advanced Java features• Servlet programming• The Spark Web application framework• The Velocity template engine
7
![Page 8: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/8.jpg)
Python Lambda Expressions
• Lambda expression– From Alonzo Church– 1930s– A nameless function– Implemented in both
Python and Java
8
Lambda Expressions
Lambda expressionA nameless functionFrom Alonzo Church
Alumnus of PrincetonProf of philosophy and mathematics at Princeton for many yearsContemporary of Alan Turing1930sDeveloped some important theoretical results independent of and working with Turing
Lambda expressions are implemented in both Python and Java
![Page 9: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/9.jpg)
Python Lambda Expressions
• In Python:– The keyword lambda– (optionally) Parameters separated by commas– A colon– A single expression that uses the parameters
9
![Page 10: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/10.jpg)
Python Lambda Expressions
mult = lambda x, y: x * y…prod = mult(5, 6)print(prod) # prints 30
print( (lambda x, y: x * y)(5, 6) ) # prints 30
10
def mult(x, y): return x * y…prod = mult(5, 6)print(prod) # prints 30
Without usinga lambdaexpression:
Using alambdaexpression:
Using a lambda expression:
![Page 11: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/11.jpg)
Python Lambda Expressions
11
def compare_lengths(word1, word2): return len(word1)–len(word2)…words.sort(compare_lengths)…
…words.sort( lambda word1,word2: len(word1)–len(word2) )…
Without lambda expression:
With lambda expression:
![Page 12: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/12.jpg)
Python Lambda Expressions
• Recall eventtest2.py– The job:
• Handle events on QPushButton objects
12
![Page 13: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/13.jpg)
Python Lambda Expressions
• See eventtestbad.py– The (desired) job:
• Same as eventtest2.py– Attempts to merge 3 slot functions into 1– Fails miserably
13
Lambda Expressions
[see slide]
Code notes:The argument passed to connect() should be a function (pointer)Here, the argument passed to connect() is None
![Page 14: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/14.jpg)
Python Lambda Expressions
• See eventtestlambda.py– The job:
• Same as eventtest2.py– Uses lambda expressions– Works!
14
Lambda Expressions
[see slide]
Code notes:An (arguably) nicer approachUse lambda expressions
![Page 15: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/15.jpg)
Agenda
• Aside: Python lambda expressions• Some advanced Java features• Servlet programming• The Spark Web application framework• The Velocity template engine
15
![Page 16: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/16.jpg)
Advanced Java: Lambda Exprs
• Java lambda expressions– New to Java SE 8– Examples…
16
![Page 17: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/17.jpg)
Advanced Java: Lambda Exprs
17
class LengthComparator implements Comparator<String>{ public int compare(String word1, String word2) { return word1.length() – word2.length(); }}...String[] words;...Arrays.sort(words, new LengthComparator());...
Without lambda expression:
![Page 18: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/18.jpg)
Advanced Java: Lambda Exprs
18
...String[] words;...Arrays.sort(words, (String word1, String word2) -> word1.length() – word2.length() );...
With lambda expression:
...String[] words;...Arrays.sort(words, (word1, word2) -> word1.length() – word2.length() );...
Sometimes can omit param types:
Advanced Java: Lambda Exprs
[see slide]
Can omit the parameter types if the compiler can infer them.
![Page 19: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/19.jpg)
Advanced Java: Lambda Exprs
• Generalizing…• Functional interface
– An interface that declares a single method• Can use a lambda expression in lieu of an
object of a class that implements a functional interface
19
![Page 20: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/20.jpg)
Advanced Java: Lambda Exprs
• Java lambda expression observations– Function pointers in a language that doesn’t
have functions!– Handy!– Inelegant?
20
Some Advanced Java Features
Lambda expression observations
“It’s best to think of a lambda expression as a function, not an object, and to accept that it can be passed to a functional interface.” -- Horstmann
Function pointers in a language that doesn’t have functions!Handy!Inelegant?
![Page 21: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/21.jpg)
Advanced Java: Packages
• Problem: Different vendors may use conflicting class names
class Book {…} // From vendor A
class Book {…} // From vendor B
21
![Page 22: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/22.jpg)
Advanced Java: Packages
• Solution: Packages– Package: a named group of
classes/interfaces– Prevents name conflicts
• Names must be unique within a package, but not across packages
– A package may contain packages– By default, classes/interfaces are in
“anonymous” package
22
Packages
Solution: PackagesPackage: a named group of classes/interfacesPrevents name conflictsA package may contain packagesBy default, classes/interfaces are in “anonymous” package
![Page 23: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/23.jpg)
Advanced Java: Packages
• Using a package
…java.lang.Math.sqrt(x);…
import java.lang.Math;…Math.sqrt(x);…
23
![Page 24: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/24.jpg)
Advanced Java: Packages• Creating a package
– Defines class edu.princeton.penny.Book– Book.java must be in edu/princeton/penny
directory
// Book.java
package edu.princeton.penny;
// All names defined subsequently are// in the edu.princeton.penny package.
class Book{ … }
24
![Page 25: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/25.jpg)
Advanced Java: Maven
• Maven– Java build tool
• C : make• Java : Maven
– Java package management tool• Python: pip & PyPi• Java: Maven & Maven central repo
25
![Page 26: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/26.jpg)
Advanced Java: Maven
• Maven as a package manager– Manages the central Maven repository, and…– Your local Maven repo (~/.m2), such that…– Your local Maven repo is a subset of the
central Maven repo, and…– Your local Maven repo contains all .jar files
required by your app
26
![Page 27: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/27.jpg)
Advanced Java: Maven
• Maven as a package manager– Programmer defines pom.xml file for app
• Specifies .jar files on which app depends– Maven downloads .jar files, as necessary,
from central Maven repo to your local Maven repo
– Does so recursively!• Related/competing tools:
– Ant, Gradle
27
![Page 28: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/28.jpg)
Agenda
• Aside: Python lambda expressions• Some advanced Java features• Servlet Programming• The Spark Web Application Framework• The Velocity Template Engine
28
![Page 29: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/29.jpg)
Servlet Programming
• Question: How does one do web programming in Java?
• Answer 1: Use CGI• Answer 2: Use Servlets• Answer 3: Use a Java web app
framework
2929
![Page 30: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/30.jpg)
Servlet Programming
• Answer 1: Use CGI– Possible, but…– Very inefficient– Java standard library provides no CGI classes
3030
Servlet Programming
Answer 1: Use CGIPossible, but…Very inefficient
Each HTTP request forks a new child process and execs a new JVM!Java standard library provides no CGI classes
No “real” website would use them anyway(I composed some for my own use; see me if you want)
![Page 31: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/31.jpg)
Servlet Programming
• Question: How does one do web programming in Java?
• Answer 1: Use CGI• Answer 2: Use Servlets• Answer 3: Use a Java web app
framework
3131
![Page 32: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/32.jpg)
Servlet Programming
• Servlets– “A servlet is a Java technology-based Web
component, managed by a container, that generates dynamic content.”
• Servlet specification at:• http://download.oracle.com/.../servlet-3_0-final-spe
c.pdf
3232
![Page 33: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/33.jpg)
Servlet Programming
Browser HTTPServer
Yourapp
Servlet Container
Servlet1
Servlet2
Servlet3
Servlet4
Popular servlet containers: Apache Tomcat Eclipse Jetty
33
![Page 34: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/34.jpg)
Servlet Programming
Browser Yourapp
Tomcat or Jetty
Servlet1
Servlet2
Servlet3
Servlet4
Tomcat or Jetty can be both servlet containerand HTTP server
We’ll use that configurationWe’ll use Jetty
34
![Page 35: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/35.jpg)
Servlet Programming
• Python: WSGI specification connects HTTP server and your Python app– Programmers compose Python code to
conform to WSGI spec– Sys admins deploy Python code with HTTP
server
3535
![Page 36: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/36.jpg)
Servlet Programming
• Java: Servlet specification connects HTTP server and your Java app– Programmers compose Java code to
conform to Servlet spec– Sys admins deploy Java code with Servlet
container and HTTP server
3636
![Page 37: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/37.jpg)
Servlet Programming
• See PennyServlets app
37
$ mvn clean$ mvn compile$ ./runserver 55555
![Page 38: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/38.jpg)
Servlet Programming
• See PennyServlets app (cont.)
38
![Page 39: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/39.jpg)
Servlet Programming
• See PennyServlets app (cont.)
39
![Page 40: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/40.jpg)
Servlet Programming
• See PennyServlets app (cont.)
40
![Page 41: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/41.jpg)
Servlet Programming
• See PennyServlets app (cont.)
41
![Page 42: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/42.jpg)
Servlet Programming
• See PennyServlets app (cont.)
42
![Page 43: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/43.jpg)
Servlet Programming• See PennyServlets app (cont.)
– penny.sql, penny.sqlite– Book.java, Database.java– Common.java, Util.java– IndexServlet.java– SearchFormServlet.java– SearchResultsServlet.java– Penny.java– pom.xml– runserver
43
Servlet Programming
[see slide]
Code notes: Book.javaIn edu.princeton.penny package
Code notes: Database.javaIn edu.princeton.penny packageUses Java database driver to access SQLite database
Code notes: Common.javaIn edu.princeton.penny packageDefines static methods used by multiple classes
Code notes: Util.javaIn edu.princeton.penny packagegetCookie() method accepts cookie name, returns cookie value (or null)
Code notes: IndexServlet.javaIn edu.princeton.penny packageCalled to generate the index page
![Page 44: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/44.jpg)
Code notes: SearchFormServlet.javaIn edu.princeton.penny packageCalled to generate the search form pageGets value associated with given nameGets cookie with given name
Code notes: SearchResultsServlet.javaIn edu.princeton.penny packageCalled to generate the search results pagePerforms redirectionCreates a cookieAccesses database
Code notes: Penny.javaDefine main() methodMaps HTTP requests to Servlet classes
Code notes: pom.xmlConfigures Maven
Use JDK 1.8The app depends upon the 5 specified .jar files
Code notes: runserverBash shell scriptUses Maven to run Jetty Web server…Which serves the app
![Page 45: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/45.jpg)
Servlet Programming
44
PennyServletspom.xmlrunserversrc
mainjava
eduprinceton
pennyBook.javaCommon.javaDatabase.javaIndexServlet.javaPenny.javaSearchFormServlet.javaSearchResultsServlet.javaUtil.java
resourcespenny.sqlpenny.sqlite
![Page 46: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/46.jpg)
Agenda
• Aside: Python lambda expressions• Some advanced Java features• Servlet programming• The Spark Web application framework• The Velocity template engine
45
![Page 47: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/47.jpg)
Spark
• Question: How does one do web programming in Java?
• Answer 1: Use CGI• Answer 2: Use Servlets• Answer 3: Use a Java web app
framework
4646
![Page 48: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/48.jpg)
Spark
• Who: Per Wendel• When: 2011, rewritten
for Java 8 in 2014• Descrip: “A micro
framework for creatingweb applications inKotlin and Java 8 withminimal effort”– Spark website
47
![Page 49: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/49.jpg)
Spark
• Why study Spark?– (Instead of some other framework)– Easy to learn
• Simple• Good documentation and tutorial• Integrated with Jetty container/server
48
![Page 50: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/50.jpg)
Spark
• See PennySpark app
49
$ mvn clean$ mvn compile$ ./runserver 55555
All pages identical to those of PennyServets app
![Page 51: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/51.jpg)
Spark
• See PennySpark App (cont.)– penny.sql, penny.sqlite– Book.java, Database.java– Common.java– Penny.java– pom.xml– runserver
50
Servlet Programming
[see slide]
Code notes: Penny.javaIn edu.princeton.penny packageUsed to generate all pagesUses lambda expressions
Code notes: pom.xmlOnly 2 dependencies!
sqlite-jdbcspark-core
Maven recursively fetches all child dependencies automatically
![Page 52: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/52.jpg)
Spark
51
PennySparkpom.xmlrunserversrc
mainjava
eduprinceton
pennyBook.javaCommon.javaDatabase.javaPenny.java
resourcespenny.sqlpenny.sqlite
![Page 53: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/53.jpg)
Spark
• Spark provides utility methods– Methods to fetch “name=value” pairs– Methods to fetch/create cookies– Methods to implement HTTP redirect
52
![Page 54: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/54.jpg)
Agenda
• Aside: Python lambda expressions• Some advanced Java features• Servlet programming• The Spark Web application framework• The Velocity template engine
53
![Page 55: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/55.jpg)
Velocity
• Problem:– Code contains many assignment statements
to compose HTML code– Bulky; awkward; slow; error prone
• Solution:– Templates– In this case via…– The Apache Velocity template engine
54
![Page 56: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/56.jpg)
Velocity
• See PennySparkVelocity app– penny.sql, penny.sqlite– Book.java, Database.java– header.vtl, footer.vtl, index.vtl, index.vtl,
searchform.vtl, searchresults.vtl– Penny.java– pom.xml– runserver
55
The Velocity Template Engine
[see slide]
Code notes: footer.vtlA Velocity template containing a placeholder
Code notes: index.vtlA Velocity template containing a placeholder
Code notes: searchform.vtlA Velocity template containing a placeholder
Code notes: searchresults.vtlA Velocity template containing a placeholderAlso contains some logic expressed in the Velocity template language (not Java)
Code notes: Penny.javaInstantiates Velocity templates
![Page 57: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/57.jpg)
Velocity
56
PennySparkpom.xmlrunserversrc
mainjava
eduprinceton
pennyBook.javaCommon.javaDatabase.javaPenny.java
resourcespenny.sqlpenny.sqliteheader.vtlfooter.vtlindex.vtlsearchform.vtlsearchresults.vtl
![Page 58: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/58.jpg)
Velocity
• Note the MVC architecture– Model
• penny.sql, penny.sqlite, Book.java, Database.java– View
• header.vtl, footer.vtl, index.vtl, searchform.vtl, searchresults.vtl
– Controller:• Penny.java
57
The Velocity Template Engine
Note the MVC architecture
Modelpenny.sql, penny.sqlite, Book.java, Database.javaConcern of DB admins
Viewheader.vtl, footer.vtl, index.vtl, searchform.vtl, searchresults.vtlConcern of Web designers
Controller:Penny.javaConcern of programmers
![Page 59: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/59.jpg)
Velocity
• Python/Flask/Jinja2– Encourages separation of concerns– But templates can contain Python-like code
• Java/Spark/Velocity– More strongly encourages separation of
concerns– Templates cannot contain embedded Java
code!
58
The Velocity Template Engine
Python/Flask/Jinja2Encourages separation of concernsBut templates can contain Python-like code
Java/Spark/VelocityMore strongly encourages separation of concernsTemplates cannot contain embedded Java code!Instead use a special purpose (Apache Velocity) language
![Page 60: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/60.jpg)
More Spark and Velocity!• There is much more to Spark and Velocity• Spark website:
– http://sparkjava.com/• Velocity website:
– http://velocity.apache.org/engine/1.7/vtl-reference.html
• Don’t confuse Spark (sometimes called SparkJava) with Apache Spark (an open-source cluster-computing framework)
59
![Page 61: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/61.jpg)
Commentary
• Commentary: Python vs. Java for server-side web apps…
• Python/Flask/Jinja2– Small/medium apps: good– Large apps: not so good
• Java/Spark/Velocity– Small/medium apps: not so good– Large apps: good
60
Commentary
Commentary: Python vs. Java for server-side web apps…
Python/Flask/Jinja2Small/medium apps (4 programmers, a few months): good
Simple, succinctLarge apps (40 programmers, a few years): not so good
Dangerous (weak/duck typing, no privacy)Relatively poor performance
Java/Spark/VelocitySmall/medium apps: not so good
Complex, verboseLarge apps: good
Safe (strong typing, privacy)Good performance
![Page 62: Programming: Java - cs.princeton.edu](https://reader031.vdocuments.net/reader031/viewer/2022012101/61db7acda25d25573246c49b/html5/thumbnails/62.jpg)
Summary
• We have covered:– Aside: Python lambda expressions– Some advanced Java features– Server-side web programming in Java, via…– Servlets– The Spark web app framework– The Velocity template engine
61