Professional Documents
Culture Documents
Servlets: © 2000 Sun Microsystems, Inc. All Rights Reserved
Servlets: © 2000 Sun Microsystems, Inc. All Rights Reserved
• Servlets
• JavaServer Pages™ (JSP) Technology
J2SE
RMI/IIOP
RMI/IIOP
JDBC
JDBC
JavaMail JavaMail
JNDI
JMS
JNDI
JTA
JMS
JTA
App Client
Container JAF JAF
App HTTP/
Client HTTPS J2SE
RMI
RMI/IIOP
JDBC
JNDI
JMS
J2SE J2SE
Database
HTML
Container Beans
Database
Servlets
Legacy
Systems
HTTPServlet classes...
… respond to web server requests
… are initialized once
… persist across multiple requests
… can maintain user state in a session object
… have simple APIs for working with cookies
and HTTP headers
… are portable across many (all?) web
servers and many app servers
© Copyright 1999 Sun Microsystems, Inc., All rights reserved.
Servlets vs. CGI
Request CGI1
Child for CGI1
Request Servlet1
Servlet Based Webserver
• Electronic commerce
• HR applications
• Well-defined lifecycle
• Managed by container
• Loaded on demand
• Unloaded by container at any time
• Multi-threaded
GET /index.html
GET /index.html
GET /map.html
Server
Request
Servlet
Response
JNDI Server
HTTP Server
Servlet
EJB Server
Request
Forward
• Declarations
<%! QuoteBean q; %>
• Scriptlets
<% q = new QuoteBean(); %>
• Expressions
<%= q.getQuote(“SUNW”); %>
X
out.println(“<HTML><HEAD><TITLE>Your accounts</TITLE></HEAD>”);
out.println(“<BODY>Welcome “ + name + ”.<br>”);
out.println(“Account number: “ + accountDetails.getNumber());
•
•
•
}
© Copyright 1999 Sun Microsystems, Inc., All rights reserved.
#A
What are Servlets & JSPs?
JSPs are a presentation layer for Servlets...
<HTML>
<HEAD>
<TITLE>Your accounts</TITLE>
</HEAD>
<BODY>
<%
AccountDetails ad = (AccountDetails)
req.getAttribute(“account”, accountDetails);
%>
Name : <%=ad.getName()%> <br>
Account Number: <%=ad.getNumber()%>
<br>
Balance: <%=ad.getBalance()%>
</BODY>© Copyright 1999 Sun Microsystems, Inc., All rights reserved.
#A
What are Servlets & JSPs?
Servlets are Java’s answer to CGI/Perl...
X
out.println(“<HTML><HEAD><TITLE>Your accounts</TITLE></HEAD>”);
ServletContext context =getServletContext()
out.println(“<BODY>Welcome “ + name + ”.<br>”);
Why do I •out.println(“Account
care? = context.number:
dispatcher “ + accountDetails.getNumber());
getRequestDispatcher(“OnlineBank/Account.jsp”);
•req.setAttribute(“account”, accountDetails);
• Enables•dispatcher.include(req,
separation of presentation
res); logic from presentation layout
}
• Enable your experts to play to their strengths
© Copyright 1999 Sun Microsystems, Inc., All rights reserved.
#A
How do they all work together?
Presentation Application Logic
Logic Layout Business Data Access
Bank
Account
Entity Bean
debit()
Transfer Bank Manages data
Funds Teller
Servlet Session Bean
doPost() transfer()
Receives request Show Validates request Bank
Validates input Confirmation Executes process Account
Calls session bean Page Enforces transactions
Calls JSP Entity Bean
JSP
Formats HTML credit()
Responds to client Manages data
– Model 2: