CSC309: Web ProgrammingGreg Wilson 1 Use Javascript to create richer browser-based GUIsMove data back and forth using HTTPHave a web server handle:Fetching pagesInvoking users’ programs to create dynamic contentDeclare victory and move on?Not quite… !"!Each time a CGI is run, the server must:Create a new processRe-run the Python interpreterEstablish a new database connection Or re-read the data fileThen close them all downThis takes time……which limits how many requests the server can handle#$ % &$ ' &$ ( Why not just keep Python running?It’s just another lump of CEach time a request comes in, wipe the interpreter’s memory, and start execution Or wipe most of it, but leave the database connections alonemod_python (and similar for other languages)This doesn’t work (safely) with C/C++!If a program can access arbitrary memory, there’s no sure way to re-set itCSC309: Web ProgrammingGreg Wilson 2) *Java was originally designed for building downloadable GUIsActually designed to run web TV, but that’s another storyIn the late 1990s, it offered the best of two worlds:Compilation and type checking, which means higher performance and more safetyInterpreted, which means arbitrary code can be run safely At least, more safely than C+++ A servlet is a mini-application designed to be run on a user’s behalf by a serverNo main()Instead, it implements one or more lifecycle methods that the server calls at specific times Just like GUI buttons implement OnClicked()Servlets are run by a containerDynamically loads the servlet’s class(es)Finds and calls methods using reflection,- %. Writing servlets is relatively straightforwardNext lecture will introduce Java Server Pages (JSPs), which make it even easierConfiguring a servlet container is complicatedEverything has to be in exactly the right placeXML configuration files have to be exactly rightThere are open source Eclipse plugins to helpWe’ll use Tomcat as our servlet containerFrom the same folks that brought you ApacheThe reference implementation of the standard/0 & 1Create directory "hello", with three sub-directories:src: for source files like HelloServlet.javaclass: for compiled class filesetc: for other files (like deployment descriptors)Like what?A deployment descriptor is an XML file that tells the container what your servlet is called, what class implements it, and what URL maps to itCSC309: Web ProgrammingGreg Wilson 3230 & 1import javax.servlet.*;import javax.servlet.http.*;import java.io.*;public class HelloServlet extends HttpServlet {public void doGet(HttpServletRequest req,HttpServletResponse resp)throws IOException {PrintWriter out = resp.getWriter();out.println("<html><body><p>Hello, servlet!</p>" +"</body></html>");}}30 & 1<web-app xmlns="http://java.sun.com/xml/ns/jsee" …><!-- Define symbolic name for servlet --><servlet><servlet-name>Hello</servlet-name><servlet-class>HelloServlet</servlet-class></servlet><!-- Connect servlet and URL --><servlet-mapping><servlet-name>Hello</servlet-name><url-pattern>/hello</url-pattern></servlet-mapping></web-app>30 & 1To deploy:Under Tomcat's root directory, create webapps/hello/WEB-INF/classes directoryPut web.xml file in hello/WEB-INFPut HelloServlet.class in hello/WEB-INF/classesDon't panic: it's just another interfaceRe-start Tomcat, and go to http://localhost:8080/hello/HelloPort 8080 because we're running Tomcat on its own ! 4Communication supportLike BaseHTTPServer.HTTPServerLifecycle managementContainer loads classes, calls constructors, etc.MultithreadingThough you still have lots of work to doStandard configuration machineryBelieve it or not, it's simpler than the alternativesJSPsSee these in the next lectureCSC309: Web ProgrammingGreg Wilson 4 40 ! Your servlet must extend HttpServlet"The real benefit of object-oriented programming is that it allows old code to use new code."Python's reliance on "duck typing" doesn't scale to large projects nearly as wellYour servlet must implement doGet(), doPost(), or bothThere are other HTTP methods, but only 0.1% of applications ever use themThe method service() figures out what to invoke#$ 5 HttpServletRequest defines:getContentLength()getCookies()getHeader()getQueryString()Everything you remember from CGI is thereAlong with a lot moreE.g., session managementNote: it's an interface, not a base class$ 6 HttpServletResponse is what your servlet writes togetParameter() and getParameterValues()setContentType()getWriter()addCookie()addHeader()setStatus()Also an interface (rather than a base class)+- %A given servlet is only ever instantiated onceEach request is handled by a separate threadContainer maintains a thread poolIf a thread is idle when a request arrives, the container gives it the servlet and a new pair of request and response objectsOtherwise, request blocks until thread availableServlets must be thread-safe!Shared data must be protectedStack data is intrinsically safeCSC309: Web ProgrammingGreg Wilson 5,7. 89 ( The first time a request arrives for a servlet, the container:Finds and loads the classCalls its default constructorCalls its init() methodThe container then repeatedly calls service(), which in turn calls doGet(), doPost(), etc.When the container wants (or needs) to unload the servlet, it first calls the servlet'sdestroy() method/7. )-%:-Been talking as if GET and POST were basically the
View Full Document