Building Web Applications with Servlets and JavaServer Pages - PowerPoint PPT Presentation

1 / 50
About This Presentation
Title:

Building Web Applications with Servlets and JavaServer Pages

Description:

Building Web Applications with Servlets and JavaServer Pages Software engineering group Computer science faculty Binus University Outline Web Development Overview ... – PowerPoint PPT presentation

Number of Views:735
Avg rating:3.0/5.0
Slides: 51
Provided by: DavidSJ
Category:

less

Transcript and Presenter's Notes

Title: Building Web Applications with Servlets and JavaServer Pages


1
Building Web Applications with Servlets and
JavaServer Pages
  • Software engineering group
  • Computer science faculty
  • Binus University

2
Outline
  • Web Development Overview
  • Introduction to Java Servlets and JavaServer
    Pages
  • Tips on setting up the environment
  • Sample Web Application
  • Web Application Patterns
  • Use in Curriculum

3
Acknowledgments
  • UML is a trademark of Object Management Group,
    Inc. in the U.S. and other countries
  • Rational software and courseware used with
    permission by Rational Software Corp. and through
    Rationals SEED (Software Engineering for
    Educational Development) program
  • http//www.rational.com/corpinfo/college_relations
    /seed/

4
Web Development Overview
  • static html
  • text file containing html tags created manually
  • may include some client-side scripts (e.g.
    JavaScript)
  • dynamic html
  • html file produced at time of request
  • cgi, php, asp, jsp, Servlets
  • active html
  • html contains a program that runs at the client
    inside a web browser
  • Java applets

5
Static HTML
web server
web browser
request URL
_____.html
response HTML
6
Dynamic HTML
application
DBMS
RMI Sockets CORBA EJB
JDBC
web server
_____.class
web browser
request URL
_____.jsp
response HTML
_____.html
7
Active HTML
web server
web browser
request URL
_____.html
_____.class
response HTML
8
Dynamic and Active HTML
application
DBMS
RMI Sockets CORBA EJB
JDBC
web server
_____.class
web browser
request URL
_____.jsp
_____.class
response HTML
_____.html
9
Java Confusion
  • Java Applications
  • stand-alone executable applications
  • Java Applets
  • applications that run within a web-browser
  • Java Servlets
  • applications that run within a web-server
  • JavaScript
  • scripts that run within a web-browser
  • not really Java at all
  • available with original 1995 Java release

10
Java Application
11
Java Application
12
Java Applet
13
Java Applet
14
JavaScript
15
Introduction to Servlets
  • Servlets are Java programs that run inside a web
    server.
  • Servlets allow web servers to receive requests
    from clients (normally entered in a form on a web
    page).
  • Servlets can perform server-side processing such
    as interacting with a database or another
    application
  • Servlets can generate dynamic html based on
    server-side processing and return this to the
    client.

16
Servlet Howto
  • create an HTML file that invokes a servlet
    (usually through the FORM ACTION)
  • create a Java program that does the following
  • import javax.servlet.
  • import javax.servlet.http.
  • inherit from HttpServlet
  • override the doGet and doPost methods
  • write the response HTML file using a
    java.io.Printwriter to the HTTP response

17
Introduction to Servlets
  • A Simple Example
  • collect username and password
  • reply with welcome page
  • or Invalid Login if password is not verysecret

18
lt!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN"gt ltHTMLgt ltHEADgt
ltTITLEgtCCSC Tutorial Demolt/TITLEgt lt/HEADgt
ltBODYgt ltFORM NAME"addUser" METHOD"POST"
ACTION "/servlet/ccsc.LogonServlet"gt
ltBgtUser Name lt/Bgt ltINPUT NAME "userName"
TYPE "TEXT" MAXLENGTH "25" SIZE "15"gt
ltbrgt ltBgtPassword lt/Bgt ltINPUT NAME
"password" TYPE "password" VALUE
verysecret" MAXLENGTH "25" SIZE "15"gt
ltbrgt ltBgtltINPUT NAME "login" VALUE
"Login" TYPE "SUBMIT"gtlt/Bgt
lt/FORMgt ltscript language"JavaScript"gt
document.addUser.userName.focus()
lt/scriptgt lt/BODYgt lt/HTMLgt
19
package ccsc import
javax.servlet. import javax.servlet.http. publ
ic class LogonServlet extends HttpServlet
public void init(ServletConfig config) throws
ServletException super.init(config)
protected void processRequest(HttpServletRe
quest request, HttpServletResponse response)
throws ServletException, java.io.IOException
response.setContentType("text/html")
java.io.PrintWriter out response.getWriter()
String uName request.getParameter("userN
ame") String pWord request.getParameter
("password") if(pWord.equals("verysecret"
)) out.println("lthtmlgtltheadgtlttitlegtC
CSC Tutorial Demo Welcomelt/titlegtlt/headgt")
out.println("ltbodygtlth3gtWelcome " uName
"!lt/h3gtlt/bodygtlt/htmlgt") else
out.println("lthtmlgtltheadgtlttitlegtCCSC
Tutorial Demo Invalid Loginlt/titlegtlt/headgt")
out.println("ltbodygtltH3gtInvalid
Loginlt/H3gtlt/bodygtlt/htmlgt")
out.close() protected void
doGet(HttpServletRequest request,
HttpServletResponse response) throws
ServletException, java.io.IOException
processRequest(request, response)
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws
ServletException, java.io.IOException
processRequest(request, response)
20
(No Transcript)
21
JavaServer Pages
  • JavaServer Pages provide a layer above Servlets
    that allow you to mix HTML and calls to Java code
    in the web server.
  • JavaServer Pages are actually converted into
    Servlets, but JSPs are easier to work with for
    those familiar with HTML.
  • Servlets usually create Java objects as Java
    Beans to simplify JSP access.

22
Mixing JavaServer Pages with Servlets
web server
LogonServlet.class
web browser
request URL
welcome.jsp
response HTML
invalidLogin.html
23
package ccsc import
javax.servlet. import javax.servlet.http. publ
ic class LogonServlet extends HttpServlet ...
protected void processRequest(HttpServletRequest
request, HttpServletResponse
response) throws ServletException,
java.io.IOException response.setContentT
ype("text/html") String uName
request.getParameter("userName") String
pWord request.getParameter("password")
HttpSession session request.getSession(true)
if(pWord.equals("verysecret"))
session.setAttribute("uName",uName)
gotoPage("/welcome.jsp",request,response)
else gotoPage("/invalidLog
in.html",request,response)
private void gotoPage(String address,
HttpServletRequest request, HttpServletRespon
se response) throws ServletException,
java.io.IOException RequestDispatcher
dispatcher getServletContext().getRequestDispatc
her(address) dispatcher.forward(request,
response) ...
24
welcome.jsp
lt_at_page contentType"text/html"gt lthtmlgt ltheadgtltti
tlegtCCSC Tutorial Demo Welcomelt/titlegtlt/headgt ltbod
ygt lth3gtWelcome lt session.getAttribute("uName"
)gt!lt/h3gt lt/bodygt lt/htmlgt
25
(No Transcript)
26
JSP Howto
  • Create a file with a .jsp extension
  • include static HTML
  • include JSP Scripting Elements
  • Expressions lt expression gt
  • Scriptlets lt code gt
  • Declarations lt! Code gt
  • Directives lt_at_ variable directive gt
  • Comments lt-- JSP Comment --gt
  • Use pre-defined variables
  • request HttpServletRequest
  • response HttpServletResponse
  • session HttpSession
  • out PrintWriter

27
JSP Howto
  • Include another file
  • Import from the Java library
  • Declare and use a variable
  • Access session and request information

lt_at_ include file"commontop.html" gt
lt_at_ page import"java.util." gt
lt! private int accessCount 0 gt
ltH2gtAccesses to page since server reboot lt
accessCount gtlt/H2gt
ltH2gtCurrent time lt new java.util.Date()
gtlt/H2gt ltH2gtRemote Host lt request.getRemoteHos
t() gtlt/H2gt ltH2gtSession ID lt session.getID()
gtlt/H2gt
28
JSP Howto
  • Assuming a Servlet has done the following
  • In the JSP, access a Java object in the server

Vehicle v new Vehicle()
v.pic1Filename Honda1996.jpg v.year
1996 session.setAttribute("vehDetail",v)
ltjspuseBean id"vehDetail class
ccsc.Vehicle scope "session"/gt lt
out.println(ltH3gtYear vehDetail.year
lt/H3gt) out.println("ltIMG
SRC/jsp/dirs/vehiclePics/"
vehDetail.pic1Filename "
WIDTH300 HEIGHT180gt") gt
29
Advantages of using Servlets and JavaServer Pages
  • Java is widely known
  • Java has many applications
  • from GUIs to PDAs to Applets to Servlets
  • Java has a rich set of libraries
  • threads
  • networking (sockets, RMI, CORBA)
  • database connectivity through JDBC

30
Advantages of using Servlets and JavaServer Pages
  • Java is free
  • http//java.sun.com for Java SDK and J2EE
  • http//jakarta.apache.org for Tomcat
  • Java is portable
  • runs on many OSs and in many Servlet Engines
  • Java is efficient
  • multiple concurrent lightweight threads execute a
    single set of code within the web server
  • Java is secure
  • memory restrictions (array out-of-bounds)

31
Setting up the environment
  • Many Servlet engines
  • http//www.servlets.com/engines/index.html
  • Tomcat on Apache on Linux
  • Outsource it through web hosting
  • (3tec.com 20 7x4months for a semester)
  • Forte for Java has Tomcat built-in
  • http//www.sun.com/forte/ffj/

32
Setting up the environment
  • When using Tomcat (stand-alone or in FFJ)
  • place .html and .jsp files in home dir
  • place .class Servlet files in WEB-INF/classes

33
Setting up the environment
  • Tips to avoid Pitfalls
  • in Forte for Java
  • add all files through Template Wizard (File-gtNew)
  • set External Browser through Tools-gtOptions

34
Setting up the environment
  • Tips to avoid Pitfalls
  • in Forte for Java
  • Execute web page, dont run it

35
Sample Web Application
  • SeaGypsy Hotel Reservation System
  • Created by Junior/Senior level students
  • CSC361 Software Design and Development
  • January 2001

36
(No Transcript)
37
(No Transcript)
38
(No Transcript)
39
(No Transcript)
40
(No Transcript)
41
UML Class Diagram
42
UML Database Schema
43
UML Sequence Diagram
44
Design Issues
  • Good software design separates the presentation
    from the business logic from the data model.
  • Model/View/Controller, Mediator, and Façade
    design patterns can be used to improve
    maintainability and distribution of tasks

45
Design Issues
  • Web applications are often developed in a group
    whose members have a combination of skills
  • System Administrator (Unix, Apache, Tomcat)
  • Web Developer (HTML, Graphics Design)
  • Software Developer (Java)

46
Design Issues
  • Object-Oriented approach facilitates reuse,
    encapsulation, distribution of responsibilities.
  • Presentation layer (view) can primarily be
    restricted to JSP.
  • Business logic (controller) can primarily be
    restricted to Java using Servlets.
  • Data model (model) may be split between DB and
    Java.
  • Domain model objects dont care if they are being
    used in a web application or a stand-alone
    application.

47
Web Application Patterns
  • Action Controller
  • Each web page has a controller object that knows
    which model and view to use
  • Front Controller
  • A single object handles incoming requests and
    passes them to other objects

48
Web Application Patterns
  • Data Mapper
  • Each Domain Object has a Mapper object that knows
    how to store and retrieve it from the DB
  • See more at
  • http//martinfowler.com/isa/
  • For a framework that contains many patterns
  • See Expresso at http//www.jcorporate.com/

49
Use in curriculum
  • Software Design and Development
  • Networks
  • Web Application becomes client of a C server
  • Senior Seminars
  • On-line newspaper
  • Course Evaluation system

50
Resources
  • Core Servlets and JavaServer Pages
  • by Marty Hall
  • Core Web Programming
  • by Marty Hall and Larry Brown
  • http//java.sun.com
  • http//java.sun.com/webservices/docs/ea2/tutorial/
    index.html
  • http//www.wantjava.com/resources.jsp
  • http//www.servlets.com
  • http//jakarta.apache.org
Write a Comment
User Comments (0)
About PowerShow.com