Servlet and JSP Programming: An Introduction - PowerPoint PPT Presentation

About This Presentation
Title:

Servlet and JSP Programming: An Introduction

Description:

throws IOException, ServletException { res.setContentType('text/html' ... throws ServletException, java.io.IOException { response.setContentType ('text/html' ... – PowerPoint PPT presentation

Number of Views:69
Avg rating:3.0/5.0
Slides: 22
Provided by: spirospapa
Learn more at: http://www.cs.cmu.edu
Category:

less

Transcript and Presenter's Notes

Title: Servlet and JSP Programming: An Introduction


1
Servlet and JSP ProgrammingAn Introduction
  • Spiros Papadimitriou
  • spapadim_at_cs.cmu.edu

2
Overview
  • Introduction
  • Static vs. Dynamic
  • Servlets
  • Java Server Pages (JSP)

3
Introduction
  • What you know
  • Java
  • JDBC
  • What well tell you
  • How to use Java inside a webserver!
  • Why you might care
  • Building non-trivial web interfaces and
    applications
  • In particular database-backed web applications

4
Plain Old Static Pages
  • Static webpages
  • Plain files stored in the filesystem
  • Webserver accepts pathnames
  • Returns contents of corresponding file
  • Boring!
  • Cant generate customized contentalways serve
    the same static pages

5
Static Pages
6
Dynamic Content
  • CGI Easy fix
  • Common Gateway Interface
  • Oldest standard
  • But at least a standard!
  • Inefficient
  • No persistent state
  • Forward requests to external programs
  • Spawn one process for each new request (ouch!)
  • Communicate via
  • Standard input/output
  • Environment variables
  • Process terminates after request is handled

7
CGI
8
CGI
9
Servlets to the rescue
  • Little Java programs
  • Contain application-specific code
  • Web server does generic part of request handling
  • Servlets run in the web server and do some of
    the handling
  • Highlights
  • Standard!
  • Efficiency (much better than CGI)
  • Security (Java!)
  • Persistence (handle multiple requests)

10
Servlets
STATUS ltresponse foogt
res ltrespose foogt
11
Servlets
res ltresponse bargt
STATUS ltresponse bargt
12
Servlet Example
  • package pkg
  • import java.io.
  • import javax.servlet.
  • import javax.servlet.http.
  • public class Servlet extends HttpServlet
  • public void doGet(HttpServletRequest req,
  • HttpServletResponse res)
  • throws IOException, ServletException
  • res.setContentType("text/html")
  • PrintWriter out res.getWriter()
  • out.println("lthtmlgtltheadgtlttitlegtSample
    Servlet")
  • out.println("lt/titlegtlt/headgtltbodygt")
  • out.println("lth1gtHello World at "
  • req.getRequestURI() "
    !lt/h1gt")
  • out.println(ltpgtKey is "
    req.getParameter("KEY"))
  • out.println(lt/pgtlt/bodygtlt/htmlgt")

13
More Servlets
  • A catalog of servlet methods
  • init(config)
  • service(req, res)
  • doGet(req, res)
  • doPut(req, res)
  • doDelete(req, res)
  • etc
  • A catalog of request methods
  • getParameter(name)
  • getParameterNames(), getParameterValues(name)
  • getCookies()
  • A catalog of response methods
  • sendRedirect(url), sendError(errCode, errStr)
  • setContentType(mimeType)
  • addCookie(cookieObj)

14
JSP Sugar
  • Printing to output not really a special case when
    writing heaps of HTML!
  • Well why not make that the common case in the
    syntax and program statements the special case?
  • And while were at it, why not hide the
    compilation steps (let the server do it)?
  • Now things look more like plain old HTML (only
    they arent they can produce dynamic content and
    really are Java programs)

15
First JSP Example (1/3)
  • lt_at_ page importjava.io. gt
  • lthtmlgt
  • ltheadgtlttitlegtSample JSPlt/titlegtlt/headgt
  • lt String s JSP gt
  • ltbodygt
  • lth1gtHello lt s gt World!lt/h1gt
  • ltpgtlt out.print(Scriptlet Statement Hello!)
    gtlt/pgt
  • ltpgtKey is lt request.getParameter(KEY) gtlt/pgt
  • lt/bodygt
  • lt/htmlgt
  • Core syntactic elements
  • Directives lt_at_ page gt
  • Declarations lt! gt (outside service method)
  • Scriptlets lt gt
  • Expressions lt gt

16
First JSP Example (2/3)
  • package jsp._jsp
  • // line/jsp/hello.jsp1
  • import java.io.
  • import javax.servlet.
  • import javax.servlet.http.
  • import javax.servlet.jsp.
  • public class _hello_2ejsp
  • extends org.gjt.jsp.HttpJspPageImpl
  • implements org.gjt.jsp.GnuJspPage
  • public void _jspService (HttpServletRequest
    request,
  • HttpServletResponse
    response)
  • throws ServletException, java.io.IOException
  • response.setContentType ("text/html")
  • HttpSession session pageContext.getSession
    ()

17
First JSP Example (3/3)
try // line/jsp/hello.jsp2
out.print("lthtmlgt\n") //
line/jsp/hello.jsp4 String s "GNUJSP"
// line/jsp/hello.jsp5
out.print("\nltbodygt") //
line/jsp/hello.jsp6 out.print(s)
// line/jsp/hello.jsp7
out.print(Scriptlet Statement Hello!)
// line/jsp/hello.jsp8
out.print(request.getParameter(KEY)) //
line/jsp/hello.jsp8 out.print(lt/pgt\nlt/bod
ygt) catch (Throwable e)
18
Second JSP Example (1/2)
  • lt_at_ page importjava.io. gt
  • lt_at_ page importjava.util. gt
  • lt_at_ page importjava.sql. gt
  • lt!
  • Static final String dbHost gt
  • Static final String oracleDriver
  • gt
  • lt
  • String courseId request.getParameter(courseid
    )
  • Class.forName(oracleDriver)
  • Connection conn DriverManager.getConnection()
  • Statement stmt con.createStatement()
  • String qry SELECT STUDENT.SID, SNAME,
    COURSE.CID
  • FROM STUDENT, COURSE, TAKE
  • WHERE TAKE.CID courseId
    AND
  • STUDENT.SID TAKE.SID AND
  • COURSE.CID TAKE.CID
  • ResultSet res conn.executeQuery(qry)
  • gt

19
Second JSP Example (2/2)
  • lthtmlgt
  • ltheadgtlttitlegtStudent Informationlt/titlegtlt/headgt
  • ltbodygt
  • lth1gtStudent Information for lt courseId gtlt/h1gt
  • ltpgtlttable border1gt
  • lttrgtltthgtCourseIDlt/thgtltthgtStudentlt/thgtltthgtStudentID
    lt/thgtlt/trgt
  • lt while (rs.next()) gt
  • lttrgt
  • lttdgtlt rs.getString(cid) gtlt/tdgt
  • lttdgtlt rs.getString(sname) gtlt/tdgt
  • lttdgtlt rs.getString(sid) gtlt/tdgt
  • lt/trgt
  • lt // end while gt
  • lt/tablegtlt/pgt
  • lt/bodygt
  • lt/htmlgt

20
Alternatives
  • Dynamically loadable server modules
  • Web server offers standard API for request
    handlers
  • Request handlers are dynamic libraries
  • Loaded into server address space (security?!)
  • Very efficient!
  • Better suited for generic server extensions than
    application-specific logic
  • CGI flavors
  • Persistent CGI (PCGI) / FastCGI
  • Too little, too late
  • Modern web publishing systems
  • Essentially databases that speak HTTP

21
More Information
  • Servlets and JSPs
  • http//java.sun.com/products/servlet/
  • http//java.sun.com/products/jsp/
  • JDBC
  • http//java.sun.com/products/jdbc/
Write a Comment
User Comments (0)
About PowerShow.com