380 likes | 538 Views
IA308 – Servlets and JSPs. Christophe Coenraets Manager, Technical Evangelists Internet Applications Division ccoenrae@sybase.com. Servlets Topics. Dynamic Web Sites Architectures Servlets Benefits Servlet's Life Cycle Building a Servlet Tracking Sessions. Site Map. ListDest.jsp.
E N D
IA308 – Servlets and JSPs Christophe Coenraets Manager, Technical Evangelists Internet Applications Division ccoenrae@sybase.com
Servlets Topics • Dynamic Web Sites Architectures • Servlets Benefits • Servlet's Life Cycle • Building a Servlet • Tracking Sessions
Site Map ListDest.jsp Destination.jsp TripFinder.jsp LogonServlet Logon.htm Payment.jsp LogonError.htm Booking.jsp Customer.jsp
<HTML> Aruba Bermuda Bahamas </HTML> Static Web Site http://www.myvacation.com/ListDest.htm HTTP Server HTTP HTML
<HTML> Aruba Bermuda Bahamas </HTML> Dynamic Page Generation Using CGI http://www.myvacation.com/cgi/ListDest.exe HTTP Server CGI Application ODBC / Native HTTP HTML
<HTML> Aruba Bermuda Bahamas </HTML> Dynamic Page GenerationUsing Servlets http://www.myvacation.com/servlet/ListDest HTTP Server Servlet Container JDBC HTTP HTML
What is a Servlet? • Server side Java program that extends the functionality of a Web Server • Used to dynamically generate HTML documents • Comparable to: • CGI • Netscape NSAPI • Microsoft ISAPI • Apache Modules
Written in pure Java Platform independent Can take advantage of JDBC, EJB, JMS, JavaMail, JavaIDL, RMI, ... Server independent Scalability Don’t start new process for each request Can run in same server process as HTTP server Multi-threaded Servlets Benefits
Building a ServletUsing the Servlet API • Extend HttpServlet • Code Servlet's life cycle Methods Servlet GenericServlet HttpServlet LogonServlet
Thread doGet( ) Thread Servlet Thread Servlet Life Cycle Servlets Container HTTP Server init( )
Thread Thread Servlet Life Cycle Servlets Container HTTP Server init( ) doGet( ) Servlet
Thread Servlet Life Cycle Servlets Container HTTP Server init( ) doGet( ) Servlet
Servlet Life Cycle Servlets Container HTTP Server init( ) doGet( ) destroy( ) Servlet
Servlet Life Cycle Servlets Container HTTP Server
Advanced Servlet Support in PowerJ 3.6 • Java Servlet 2.2 wizard • Generates Servlet class • Deployment descriptors • Web Archive (WAR) target (optional) • Design time visual class for drag-and-drop programming • Automated deployment • Remote debugging
Advanced Servlet Support in EA Server 3.6 • Access to connection caches • Configurable threading model • Access to external parameters • Inter-server component invocations • Refresh • Remote debugging • Logging support
Building the Logon ServletDemo ListDest.jsp LogonServlet Logon.htm LogonError.htm
Servlets as Part of J2EE • Contribution • Provides platform/server independent and scalable architecture for developing HTTP-based applications • Limitations • Modification of static content requires recompilation • Business logic not reusableby non Web clients Addressed by JSP Addressed by EJB
Java Server PagesTopics • JSP Architecture • Anatomy of a JSP • Directives • Scripting Elements • Standard Action Tags
Site Map ListDest.jsp Destination.jsp TripFinder.jsp LogonServlet Logon.htm Payment.jsp LogonError.htm Booking.jsp Customer.jsp
<HTML> Aruba Bermuda Bahamas </HTML> Dynamic Page GenerationUsing Servlets http://www.myvacation.com/servlet/ListDest HTTP Server Servlet Container JDBC HTTP HTML EA Server
<HTML> Aruba Bermuda Bahamas </HTML> Dynamic Page GenerationUsing Java Server Pages (JSP) http://www.myvacation.com/ListDest.jsp HTTP Server JSPEngine JDBC HTTP HTML Servlet Container EA Server
Java Server Pages • Web scripting technology • Creation of dynamic content using static templates • Combine markup (HTML or XML) with Java Code to produce a dynamic Web Page • Similar to Microsoft ASP and PowerDynamo • Uses Java as its scripting language • Full power of the Java language • Simple tags allow non Java developer to generate dynamic content
A Simple JSP <html> <body> <%@ page language="java" import="" %> <H1>Welcome back, <% String user = (String) session.getAttribute("user"); out.println(user); %> </H1> </body> </html>
Java Server Pages and Servlets • Servlets • HTML enclosed in Java code • JSP • Java code enclosed in HTML • Easier to Author • Better tool support
How it Works User Request Server FileChanged ? Create Source Compile Execute Servlet
Web Application Development Roles • JSP technology promotes application partitioning and separation of tasks • Java developer builds components encapsulating the application logic • Page designer assembles the application with a few method calls
Anatomy of a JSP • Static template text • Dynamic content • Directives • Scripting elements • Standard actions • Custom tags
Directives • Gives page information to JSP engine • Page directive <%@ page language="java" import="java.sql.*" errorPage="ErrorPage.jsp" %> • Include directive <%@ include file="header.htm" %>
Scripting Elements • Declarations (variables and methods) <%! String destination; %> • Scriptlets <% destination = request.getParameter("destination");%> • Expressions <p>Destination: <%= destination %>
Standard Action Tags • <jsp:forward> • Forwards request to HTML page, JSP, or servlet • <jsp:forward page="relativeUrl" /> • <jsp:include> • Includes data in a JSP page from another file • <jsp:include page="relativeUrl" flush="true" /> • <jsp:plugin> • Downloads Java plugin to browser to execute applet or bean
Standard Action TagsUsing JavaBeans Component • Locates or instantiates a Bean with a specific name and scope <jsp:useBean id="cd" class="samples.Cd" /> • Sets a property value or values in a Bean <jsp:setProperty NAME="cd" PROPERTY="artist" VALUE="Sting" /> • Gets the value of a Bean property artist=<jsp:getProperty NAME="cd" PROPERTY="artist" />
Using Custom Tags • Taglib directive • Defines a custom tag library • Provides a way to extend JSP's native functionality without breaking compatibility • Syntax <%@ taglib uri="URIToTagLibrary" prefix="tagPrefix" %>
JSP Advanced Support in PowerJ 3.6 • HTML and JSP editor with syntax highlighting • Design time syntax checking • Automated deployment (WAR file) • Remote debugging
JSP Advanced Support in EA Server 3.6 • Remote debugging • Access to connection caches • Configurable threading model • Access to external parameters • Inter-server component invocations • Refresh • Logging support