J2EE Overview
description
Transcript of J2EE Overview
![Page 1: J2EE Overview](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815a83550346895dc7f262/html5/thumbnails/1.jpg)
J2EE OverviewJ2EE Overview
Web ProgrammingWeb Programming
CSCI 4300CSCI 4300
![Page 2: J2EE Overview](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815a83550346895dc7f262/html5/thumbnails/2.jpg)
J2EE multi-tier architectureJ2EE 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)
![Page 3: J2EE Overview](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815a83550346895dc7f262/html5/thumbnails/3.jpg)
J2EE ArchitectureJ2EE Architecture
• Applets and special clients: downloaded, connect back to server
• Enterprise Java Beans: long-term, application-scope data storage and business logic
![Page 4: J2EE Overview](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815a83550346895dc7f262/html5/thumbnails/4.jpg)
Web Application LayoutWeb Application Layout
![Page 5: J2EE Overview](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815a83550346895dc7f262/html5/thumbnails/5.jpg)
Mirroring packages with directoriesMirroring packages with directories
![Page 6: J2EE Overview](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815a83550346895dc7f262/html5/thumbnails/6.jpg)
Web Application Descriptor, Web Application Descriptor, web.xmlweb.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
![Page 7: J2EE Overview](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815a83550346895dc7f262/html5/thumbnails/7.jpg)
Web.xml prologWeb.xml prolog
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
http://java.sun.com/dtd/web- 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
![Page 8: J2EE Overview](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815a83550346895dc7f262/html5/thumbnails/8.jpg)
Web.xml root elementWeb.xml root element
<web-app>
… web application contents.
</web-app>
• Can contain:– Servlet definitions– Servlet paths– Parameters– Etc, etc.
![Page 9: J2EE Overview](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815a83550346895dc7f262/html5/thumbnails/9.jpg)
Servlet elementServlet element
<servlet>
<servlet-name>GonzoServlet</servlet-name>
<servlet-class>cs4300.Gonzo</servlet-class>
</servlet>
Or
<servlet>
<servlet-name>GonzoServlet</servlet-name>
<jsp-page>gonzo.jsp</jsp-page>
</servlet>
![Page 10: J2EE Overview](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815a83550346895dc7f262/html5/thumbnails/10.jpg)
Servlet-mapping elementServlet-mapping element
<servlet-mapping> <servlet-name>GonzoServlet</servlet-name> <url-pattern>/gonzo</url-pattern>
</servlet-mapping>If the web application root is
http://ganesha.cs.uga.edu/myapp,GonzoServlet is accessed athttp://ganesha.cs.uga.edu/myapp/gonzo
![Page 11: J2EE Overview](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815a83550346895dc7f262/html5/thumbnails/11.jpg)
Welcome pageWelcome page
<welcome-file-list>
<welcome-file>
index.jsp
</welcome-file>
</welcome-file-list>
• Index.jsp is the file served at the URL http://ganesha.cs.uga.edu/myapp
![Page 12: J2EE Overview](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815a83550346895dc7f262/html5/thumbnails/12.jpg)
Error page elementsError page elements
<error-page> <error-code>404</error-code> <location>notFound.jsp</location></error-page><error-page> <exception-type> org.xml.sax.SaxParseException </exception-type> <location>parseError.jsp</location></error-page>
![Page 13: J2EE Overview](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815a83550346895dc7f262/html5/thumbnails/13.jpg)
Basic JSP pageBasic JSP page
<%@ page contentType="text/html; charset=UTF-8" %> <html>
<head> <Static HTML code…>• Declarations• Java expressions• Scriptlets (chunks of Java code)Try to minimize the mixing of HTML with
Java (violates Model 2 architecture)
![Page 14: J2EE Overview](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815a83550346895dc7f262/html5/thumbnails/14.jpg)
Declaration, scriptlet, and Declaration, scriptlet, and expressionexpression
<%! int i; String[] userName %>
<table border=1>
<% for (i=0;i<userName.length;i++) { %>
<tr><td><%=i+1 %> </td>
<td> <%=userName[i] %> </td></tr>
<% } %>
</table>
![Page 15: J2EE Overview](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815a83550346895dc7f262/html5/thumbnails/15.jpg)
Equivalent HTML from previousEquivalent HTML from previous
<table border=1>
<tr><td>1</td><td>Michael Adams</td></tr>
<tr><td>2</td><td>Dan Everett</td></tr>
<tr><td>1</td><td>Hunter Thompson</td></tr>
</table>
• But note that we try to avoid such mixing of Java and HTML!
![Page 16: J2EE Overview](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815a83550346895dc7f262/html5/thumbnails/16.jpg)
Model 2 Architecture (MVC)Model 2 Architecture (MVC)
![Page 17: J2EE Overview](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815a83550346895dc7f262/html5/thumbnails/17.jpg)
Helper Object ScopesHelper Object Scopes
![Page 18: J2EE Overview](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815a83550346895dc7f262/html5/thumbnails/18.jpg)
JavaBeanJavaBean
• Helper class that holds information
• Separates Java code from HTML
• Implements Model component of MVC architecture
• Useful for holding session data
![Page 19: J2EE Overview](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815a83550346895dc7f262/html5/thumbnails/19.jpg)
JSP Address Book InterfaceJSP Address Book Interface
![Page 20: J2EE Overview](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815a83550346895dc7f262/html5/thumbnails/20.jpg)
JavaBean ExampleJavaBean Example
• Store functionality: user enters name -email pair for storage in bean
• Fetch functionality: user enters name, bean replies with email
![Page 21: J2EE Overview](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815a83550346895dc7f262/html5/thumbnails/21.jpg)
JSP-Bean communicationJSP-Bean communication
• Make <INPUT> element names in JSP correspond to variable names in Bean
• Write Bean class with setX() and getX() setter and getter methods
• <jsp:setProperty> sends inputs to Bean
• <jsp:getProperty> gets values from Bean
![Page 22: J2EE Overview](https://reader036.fdocuments.in/reader036/viewer/2022062323/56815a83550346895dc7f262/html5/thumbnails/22.jpg)
AcknowledgementsAcknowledgements
• J2EE architecture diagrams: Sun J2EE tutorial, http://java.sun.com/j2ee/1.4/docs/tutorial/doc/index.html
• Object scopes: jGuru Java Server Pages fundamentals, http://java.sun.com/developer/onlineTraining/JSPIntro/contents.html#JSPIntro6_3