Advanced Servlets and Java Server Pages[tm] Programming

drivercutInternet και Εφαρμογές Web

13 Νοε 2013 (πριν από 3 χρόνια και 11 μήνες)

103 εμφανίσεις

Doris Chen Ph.D.
Staff Engineer
Technology Evangelism
Sun Microsystems, Inc.
Advanced Servlets and Java Server
Pages[tm] Programming
Chapter 1
Introduction
Objectives

Explain Servlet/Java Server Pages
TM (JSP[tm])
architecture and programming model

Show how to write Servlets session tracking,
filtering and lifecycle events

Show how to write JavaServer Pages(JSP) custom
tag libraries, and new features in JSP1.2

Show how JSP can work with XML[tm] data source

Show best practices in Servlets/JSP programming

Develop your own servelt and JSP application by
using all the advanced features
Outline

Introduction

Servlets/JSP[tm] overview

Servlet Filters

Servlets Session Tracking and Lifecycle
Events

New Features in JSP1.2

JavaServerPages [tm] Technology and XML

Best Practices in JSP/Servlets Programming
Speaker Bio
Doris Chen is a staff engineer/Technology Evangelist at
Sun Microsystems, responsible for developing and
delivering code camps.
As a Technology Evangelist, her expertise includes
web services, Java[tm] Performance Tuning, J2EE [tm]
technologies, JSP [tm] and Servlets, J2ME [tm]
wireless programming and web based distributed
computing. She has also presented these topics at
various conferences around the world.
Before coming to Sun Doris developed medical image
compression applications and web based network
management products. Doris received her Ph.D. from
University of California at Los Angeles (UCLA) in
computer engineering, specialized in medical
informatics.
Lab Exercises

Separate Workbook.

Slides show important code fragments.

Lab workbook shows complete program
listings.

Lab workbook also contains suggested
programming exercises.

You don't have to do these.

You can invent your own project to develop.
Security
XMLP/SOAP
XHTML/WML
MIDP Devices
HTML/XML
XMLP/SOAP
Rich Clients
Browsers
XMLP/SOAP
Services on
Demand
J2EE Server—JSP/Servlet/EJB
XML
JAX-RPC
JAXM
JAXB
JAXP
JDBC
DBMS
JMS
Connectors
Existing
Apps
EJB
JSP
Tools (Service Creation and Assembly)
Service on Demand Architecture
Platforms (Service Deployment)
Service Integration
Service Delivery
Service Container
Chapter 2
Servlets/JSP[tm] Overview
Objectives

Explain Java[tm] 2 Enterprise Edition
(J2EE)

Understand the fundamentals of servlet
and JavaServer Pages(JSP[tm])

Discuss benefits of servlets and JSP

Decide when to use
Where is Servlet and JSP
Web TierEJB Tier
Servlets and Benefits

Java

objects which extend the
functionality of a HTTP server

Dynamic customization of content

Platform and server independent

No CGI limitations

Reliable, better performance and
scalability

Abundant third-party tool and Web
server support
Servlet Details
Http
request
Http
response
Load
Invoke
No
Yes
ClientServer
Is Servlet Loaded?
Servlet Container
Run
Servlet
GET and POST
Request
Service( )
Response
Server
HttpServlet subclass
Key:Implemented by subclass
doGet(
)
doPost( )
Common Requests: GET and
POST

GET requests:

Getting information

Appended to the URL in a query string ( limited
amount of data)

.../servlet/ViewCourse?name=servlets_codecamp

Can be bookmarked and emailed

POST requests:

Sending information

Send any amount of data

.../servlet/PlaceOrderProcess

Can Not be bookmarked and emailed.

Use POST for things like placing an order.
Servlet Life Cycle
Ready
doGet (
)
doPost ( )
service( )
destroy( )init( )
Request parameters
Init
parameters
First Servlet
package codecamp;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
Public class HelloServlet extends HttpServlet
{
public void doGet(HttpServletRequest
request, HttpServletResponse
response) throws ServletException,
IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<title>First
Servlet</title>");
out.println("<big>Hello Code Camp!
</big>");
}
}
Tomcat

Tomcat3.2 is the official reference
implementation of the servlet2.2/ JSP1.1
specifications

Tomcat4.0 supports servlet2.3/JSP1.2

Used as a small stand-alone server for
testing, or can be integrated into the
Apache Web Server

Tomcat is free

http://jakarta.apache.org
Setup Environment on Solaris
#!/bin/csh
setenv J2EE_HOME $HOME/j2ee/j2sdkee1.2
setenv JAVA_HOME $HOME/java1.3
setenv CLOUDSCAPE_HOME $HOME/Cloudscape_3.6
setenv TOMCAT_HOME $HOME/tomcat3.2
setenv PATH $J2EE_HOME/bin:$JAVA_HOME/bin:
$TOMCAT_HOME/bin:
$CLOUDSCAPE_HOME/frameworks/RmiJdbc/bin:$PATH
setenv CLASSPATH .:$J2EE_HOME/lib/j2ee.jar
setenv CLASSPATH
$CLOUDSCAPE_HOME/lib/cloudscape.jar:
$CLOUDSCAPE_HOME/lib/client.jar:
$CLOUDSCAPE_HOME/lib/tools.jar:
$CLOUDSCAPE_HOME/frameworks/RmiJdbc/classes/Rm
iJdbc.jar:$TOMCAT_HOME/webapps/sample/WEB-
INF/classes:.:$CLASSPATH
Compiling and Invoking
Servlets (Tomcat)

Place source code in
$TOMCAT_HOME/webapps/doc_root

JSP, .html and any other resource files will also go under
here

Run setup script to setup CLASSPATH

javac -d ../Web-inf/classes HelloServlet.java

HelloServlet.class is generated in servlet directory
$TOMCAT_HOME/webapps/doc_root/Web-inf/classes

Start server

Invoke startup.sh or startup.bat (under
$TOMCAT_HOME/bin)

Invoke servlet

http://localhost:8080/servlet/HelloWorld
Tomcat 3.2 Directory Structure
The Need for JSP

Servlets can do a lot of things, but it is a
pain to:

Use those println() statements to generate
HTML.

Maintain that HTML.
JSP Benefits

Business and presentation logic are separated

Simplify development with JSP, JavaBeans
and custom tags

Supports software reuse through the use
of components

Recompile automatically when changes
are made to the source file

Easier to author

Platform independent
Separate Business Logic
From Presentation
isOrderValid( )
public class OrderServlet…{
public void doGet(…){
if(isOrderValid(req)){
SaveOrder(req);

out.
println(“<html>”);
out.println(“<body>”);
……
private void isOrderValid(….){
……
}

private void saveOrder(….){
……
}
}
JSP
<html>
<body>
<ora: loop name =“order”>
…..
</ora:loop>
<body>
</html>
Pure Servlet
saveOrder
( )
JavaBeans
Servlet
Public class OrderServlet…{
public void doGet(…){
……
if(bean.isOrderValid(..)){
bean.saveOrder(….);
forward(“conf.jsp”)
;
}
}
Request processing
Presentation
Business logic
JSP Architecture
JSP
Translator
Servlet Engine
Client Request
ResponseLoad JSP
Redirect
Create Servlet
Load Servlet
JSP
Servlet
JSP
Servlet
First JSP Hello
<%@ page info="a hello world example" %
>
<html>
<head>
<title>Hello, Code Camp</title>
</head>
<body bgcolor="#ffffff"
background="background.gif">
<%@ include file="dukebanner.html" %>
...
<h1>Hello, Code Camp! </h1>
...
</body>
</html>
When to Use Servlet?
When to Use JSP™?

Use JSP technology for textual response
generation

Use servlets for services, for extending the
server, or for generating binary content

Examples:

Displaying shopping cart => use JSP technology

Generating images => use servlets

Web proxy service => use servlets

Request Processing => use servlets
Lab Exercises

Install Tomcat4.03

Run your first servlet
Doris Chen Ph.D.
doris.chen@sun.com