Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

Servlets

© 2000 Sun Microsystems, Inc. All rights reserved.


Agenda

• Servlets
• JavaServer Pages™ (JSP) Technology

© Copyright 1999 Sun Microsystems, Inc., All rights reserved.


J2EE Application Model

© Copyright 1999 Sun Microsystems, Inc., All rights reserved.


Servlets Run in Web Container

Applet Web Container EJB Container


Container
Applet HTTP/ JSP Servlet RMI EJB
HTTPS

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

© Copyright 1999 Sun Microsystems, Inc., All rights reserved.


n-tier Architecture
With Servlets and EJB
Client HTTP, RMI / IIOP EJB Application Server Data Resources

HTML
Container Beans
Database

HTML Web Server


ERP
Applications

Servlets
Legacy
Systems

© Copyright 1999 Sun Microsystems, Inc., All rights reserved.


What Are Servlets?
• Java™ objects which extend the
functionality of a HTTP server
• Alternative for CGI, NSAPI, ISAPI,
etc.
• Available and running on all major
web servers
• Platform and Server Independent

© Copyright 1999 Sun Microsystems, Inc., All rights reserved.


Why Servlets?

• HTTP Internet protocol


• Much less overhead than the CGI model
• Superior alternative to proprietary
HTTP server APIs
• Lightweight

© Copyright 1999 Sun Microsystems, Inc., All rights reserved.


#A
What are Servlets & JSPs?
Servlets are Java’s answer to CGI/Perl...

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 CGI2 CGI


Based Child for CGI2
Webserver
Request CGI1
Child for CGI1

Request Servlet1
Servlet Based Webserver

Request Servlet2 Servlet1


JVM
Request Servlet1 Servlet2

© Copyright 1999 Sun Microsystems, Inc., All rights reserved.


Possible Servlet Applications

• Site-wide document management

• Electronic commerce

• HR applications

• Conference and chat applications

• Anything else you can put on the Web

© Copyright 1999 Sun Microsystems, Inc., All rights reserved.


Servlets Usage

• Results of JavaWorld™ poll


(December 1998)
– 600 respondents
– Top reasons for using servlets
• Platform independence
• Power
• Performance
• Ease of use
• Database access
• Talk to applets
• Security

© Copyright 1999 Sun Microsystems, Inc., All rights reserved.


Servlet Basics

• Well-defined lifecycle
• Managed by container
• Loaded on demand
• Unloaded by container at any time
• Multi-threaded

© Copyright 1999 Sun Microsystems, Inc., All rights reserved.


Illustration

GET /index.html

GET /index.html

GET /map.html

© Copyright 1999 Sun Microsystems, Inc., All rights reserved.


The Servlet Lifecycle

• Servlet is instantiated by the container

• It is initialized via the init() method

• The service() method is called 0-n


times when a client makes a request

• Can clean up when being unloaded via


the destroy() method

© Copyright 1999 Sun Microsystems, Inc., All rights reserved.


Illustration of Client Request

Server

Request

Servlet

Response

© Copyright 1999 Sun Microsystems, Inc., All rights reserved.


Anatomy of a Request

• A client makes a request on a server


• The request is resolved to a servlet
by the container
• The servlet’s service() method is
invoked with a Request and
Response object
• The servlet provides a response
to the request

© Copyright 1999 Sun Microsystems, Inc., All rights reserved.


Code for a Simple Servlet

public class ExampleServlet extends HttpServlet {


public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType(“text/html”);
PrintWriter out = request.getWriter();
out.println(“Hello!<BR>”);
Date rightNow = new Date();
out.println(“The time is: “ + rightNow);
}
}

© Copyright 1999 Sun Microsystems, Inc., All rights reserved.


Illustration

JNDI Server
HTTP Server

Servlet
EJB Server

© Copyright 1999 Sun Microsystems, Inc., All rights reserved.


What is JSP?

• Adds dynamic content generation


capabilities to static templates
• Standard web access layer to Java 2
Enterprise Edition (J2EE)
• Builds on Java Servlet technology
• Leverages JavaBeans™ Architecture
• Extensible via custom tags

© Copyright 1999 Sun Microsystems, Inc., All rights reserved.


JSP and Servlets

• JSP turns servlets inside out


– JSP is compiled into a servlet by the server
– JSP is a scripting language framework
– JSP is for text output such as HTML
and XML
– Java-like code can be embedded in a HTML
page

© Copyright 1999 Sun Microsystems, Inc., All rights reserved.


JSP Usage—Scenario 1

• The JSP page directly services


the request

© Copyright 1999 Sun Microsystems, Inc., All rights reserved.


JSP Usage—Scenario 2

• The request handled by a Servlet or


JSP, then forwarded to a JSP

Request
Forward

© Copyright 1999 Sun Microsystems, Inc., All rights reserved.


Core Syntax

• JSP standard directives


• JSP standard tags
• Script language declarations
• Script language scriptlets
and expressions
• Custom tag extension

© Copyright 1999 Sun Microsystems, Inc., All rights reserved.


JSP Scripting Elements

• Declarations
<%! QuoteBean q; %>

• Scriptlets
<% q = new QuoteBean(); %>

• Expressions
<%= q.getQuote(“SUNW”); %>

© Copyright 1999 Sun Microsystems, Inc., All rights reserved.


#A
What are Servlets & JSPs?
Servlets are Java’s answer to CGI/Perl...

public void doPost (HttpServletRequest req, HttpServletResponse res) {


String name = req.getSession().getValue(“name”);
String acctNum = req.getParameter(“accountNumber”);
IAccountHome accountHome;
IAccountBean account;
Context initCtx = new InitialContext();
accountHome = initCtx.lookup(“com/AccountHome”);
account = accountHome.findByPrimaryKey(acctNum);
accountDetails=
This account.getAccountDetails();
is ugly — we need some help!
Writer out = res.getWriter();

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...

public void doPost (HttpServletRequest req, HttpServletResponse res) {


String name = req.getSession().getValue(“name”);
String acctNum = req.getParameter(“accountNumber”);
IAccountHome accountHome;
IAccountBean account;
Context initCtx = new InitialContext();
accountHome = initCtx.lookup(“com/AccountHome”);
account = accountHome.findByPrimaryKey(acctNum);
accountDetails=
Javaaccount.getAccountDetails();
This isServer
ugly —Pages we need to the some
rescue!
help!
Writer out = res.getWriter();
RequestDispatcher dispatcher;

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

© Copyright 1999 Sun Microsystems, Inc., All rights reserved.


Java Server Pages
Fundamentals

• Intent from the JSP 0.92 Spec:


– Model 1:

– Model 2:

© Copyright 1999 Sun Microsystems, Inc., All rights reserved.


© Copyright 1999 Sun Microsystems, Inc., All rights reserved.

You might also like