* Web programming --What's it got to do with databases? * Almost always a database at bottom of stack end user <-> browser <-> internet <-> web server <-> database * DB used to store two things 1) Transactional data for data-centric apps --most common is E-commerce (inventory, orders, shipments, etc.) --but most data live in databases! 2) Actual web content --So change website by just manipulating DB... no coding! --ex: have tables PROF, CLASSTAUGHT, PAPERWRITTEN, RESEARCHINTEREST --Add new data to tables means a new prof appears on home page * What prog. langguages are used in this stack? 1) HTML at web browser --to tell browser how to render page 2) JavaScript embedded in HTML --to interact with user at client side 3) Java/Python/PHP and others at server --to dynamically generate HTML/JavaScript 4) SQL at server (embedded in Java/Python/PHP) --to interact with DB * We will focus on doing this in Java (everyone knows it) --Will not use JavaScript * Classic model for Java web development --MVC design pattern --"model-view-controller" --Archaic? Possibly! * At one end user--at other, web server --User views HTML, takes some action * This action is passed over internet to web server --Causes an event to be processed at the "controller" * Java class that extends "HttpServlet" * called a "servlet" --Servlet takes some action (talks to DB?) * Writes result into Java object * This is the "model" in MVC * Then sends to a "JSP" page --JSP page encapsulates the "view" * JSP uses the object passed to generate HTML * HTML is forwarded over internet to browser * Specific points to discuss: 1) JSP 2) Java servlet 3) How Java can talk to DB 4) How to store data in a session * JSP --Stands for "Java server page" --Nothing more than HTML (possibly JavaScript) with Java inside --When served to end-user --Parts outside <% %> passed verbatim --Code inside <% %> is run --See www.jsptut.com --Ex: <% int accum = 1; for (int i = 1; i < 10; i++) { accum *= i; %>

<% =i %> factorial is <% =accum %><\p> <% } %> --Special objects always available: request: of type HttpServletRequest session: of type HttpSession * Java servlet --Java class extending HttpServlet --Has a set of event handlers (doPost, doPut, doGet) --Event handler takes as input request, response --Can create response itself, if it wants --But better is to modify request, dispatch it to correct JSP --Note: specify what servlet to handle event in HTML widget ex:

...
--Note: in Google, specify mapping from servlet name to Java class in web.xml * Talking to DB in Java --Done using JDBC --See example! --basically PreparedStatement stmt = c.prepareStatement ("SELECT..."); ResultSet rs = stmt.executeQuery (); // then go thru rs --Browse java.sql.Statement! * Sessions --HTTP is stateless --Want to remember stuff? Use a session --Session mapped to user via a client-side cookie --"session" is a var of type HttpSession that's always available in JSP --Can access in servlet via HttpServletRequest.getSession () * Final notes --Wep programming is confusing enough --Gets really nasty when you have to run web server, database, etc. --We'll use Google AppEngine framework --You use their web server --You use their database