J2EE Overview Web Programming CSCI 4300
J2EE multi-tier architecture Servlet: Java class loaded into Web server JSP page: enhanced HTML page that is compiled into a servlet for deployment JavaBean: data holding class (part of Model in MVC)
J2EE Architecture Applets and special clients: downloaded, connect back to server Enterprise Java Beans: long-term, application- scope data storage and business logic
Web Application Layout
Mirroring packages with directories
Web Application Descriptor, web.xml Defines servlets and maps them to URL paths Declares the welcome file for the app Specifies pages or servlets to handle various errors Provides initialization parameters to servlets Provides links to external resources such as database connections
Web.xml prolog <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" app_2_3.dtd> app_2_3.dtd You can validate your web.xml using the W3C validator service (recommended1) When you change your web.xml, you must ask Chris or me to reload your web-app
Web.xml root element … web application contents. Can contain: –Servlet definitions –Servlet paths –Parameters –Etc, etc.
Servlet element GonzoServlet cs4300.Gonzo Or GonzoServlet gonzo.jsp
Servlet-mapping element GonzoServlet /gonzo If the web application root is GonzoServlet is accessed at
Welcome page index.jsp Index.jsp is the file served at the URL
Error page elements 404 notFound.jsp org.xml.sax.SaxParseException parseError.jsp
Basic JSP page Declarations Java expressions Scriptlets (chunks of Java code) Try to minimize the mixing of HTML with Java (violates Model 2 architecture)
Declaration, scriptlet, and expression
Equivalent HTML from previous 1 Michael Adams 2 Dan Everett 1 Hunter Thompson But note that we try to avoid such mixing of Java and HTML!
Model 2 Architecture (MVC)
Helper Object Scopes
JavaBean Helper class that holds information Separates Java code from HTML Implements Model component of MVC architecture Useful for holding session data
JSP Address Book Interface
JavaBean Example Store functionality: user enters name - pair for storage in bean Fetch functionality: user enters name, bean replies with
JSP-Bean communication Make element names in JSP correspond to variable names in Bean Write Bean class with setX() and getX() setter and getter methods sends inputs to Bean gets values from Bean
Acknowledgements J2EE architecture diagrams: Sun J2EE tutorial, Object scopes: jGuru Java Server Pages fundamentals,