Java support for WWW - PowerPoint PPT Presentation

About This Presentation
Title:

Java support for WWW

Description:

A facility to retrieve objects from the network. Decodes the object based on its extension ... Can be extended to any object type that you want ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 26
Provided by: bab6
Category:

less

Transcript and Presenter's Notes

Title: Java support for WWW


1
Java support for WWW
  • Babak Esfandiari (sources Qusay Mahmoud, Roger
    Impey, textbook)

2
Java support
  • The URL class
  • Applets
  • Servlets and JSPs

3
The URL class
  • A facility to retrieve objects from the network
  • Decodes the object based on its extension
  • For example, a .gif file will generate an Image
    object
  • Can be extended to any object type that you want
  • The objects must obviously be addressable by a
    URL
  • So far, support for http and file
  • Can be extended to support ftp and others

4
URL class example
  • URL url new URL(http//java.sun.com/index.html)
  • InputStream in url.openStream()
  • Or
  • URL url new URL(http//java.sun.com/duke.gif)
  • Image im (Image) url.getContent()
  • An HTTP connection is made
  • See full URL doc

5
Java Applets
  • Client-side
  • See code sample in course web site

6
Servlets
  • What is a servlet?
  • Servlets vs. CGI scripts
  • How do they work?
  • Example

7
What is a Servlet?
  • A server-side technology
  • Designed to overcome some limitations of existing
    technologies (e.g. CGI is stateless)
  • Characteristics
  • A light-weight task that can be executed as a
    thread
  • A servlet can remain in memory (a CGI script
    terminates when it finished)
  • Advantages
  • A servlet can service multiple client requests
  • Can handle multiple clients without
    reloading/reinitialization

8
Servlets.
  • Servlets are written in Java
  • Can be used to
  • Generate dynamic contents
  • Talk to databases
  • Work with cookies
  • Session management

9
How do they work?
HTTP
Receive Request
Engine
Create Thread
Servlet
Gen Response
Send Response
10
Servlet Framework
  • The package javax.servlet
  • At the top level there are three interfaces
  • ServletConfig, Servlet, Serializable
  • The servlet interface
  • init()
  • service()
  • destroy()
  • getServiceConfig()
  • getServiceInfo()

11
The GenericServlet
  • It is an abstract class that implements Servlet
  • public abstract GenericServlet implements
    Servlet, ServletConfig, Serializable
  • void init()
  • abstract void service()
  • void destroy()
  • ServletConfig getServletConfig()
  • String getServiceInfo()
  • void log()

12
The HttpServlet
  • It is an abstract class that extends the
    GenericServlet abstract class
  • It provides a convenient framework for handling
    the HTTP protocol
  • These two classes (GenericServlet and
    HttpServlet) ease the task of writing servlets
  • In the simplest case, you need to provide
    implementation for service()

13
Life cycle of a servlet
  • Servlet is loaded into memory by server init()
  • Servlet processes client requests service()
  • Servlet is remove by server destroy()
  • service() is responsible for handling incoming
    client requests
  • public void service(ServiceRequest request,
    ServletResponse response) throws
    ServletException, IOException
  • Delegates HTTP requests doGet() doPost()

14
Retrieving Parameters
  • Use
  • public String getParameter(String name)
  • public String getParameterValues(String name)
  • So if you have a parameter name username in a
    form then to retrieve the value use
  • String name request.getParameter(username)

15
Example
  • Consider the following form
  • ltform action"RequestParamExample" methodPOSTgt
  • First Name ltinput typetext size20
    namefirstnamegt
  • ltbrgt
  • Last Name ltinput typetext size20
    namelastnamegt
  • ltbrgt
  • ltinput typesubmitgt
  • lt/formgt

16
Example.
  • In a browser, this would look like
  • When Submit Query is clicked we have the
    output
  • First Name Qusay
  • Last Name Mahmoud

17
Example
  • The servlet
  • public class RequestParams extends HttpServlet
  • public doPost(HttpServletRequest re,
    HttpServletResponse response)
  • PrintWriter out response.getWriter()
  • out.println("lthtmlgtltbodygtltheadgtlttitlegttest
    lt/titlegtlt/head")
  • out.println("ltbodygt")
  • String firstName req.getParameter("first
    name")
  • String lastName req.getParameter("lastna
    me")
  • out.println("First Name " firstName
    "ltbrgt")
  • out.println("Last Name " lastName)
  • out.println("lt/bodygtlt/htmlgt")

18
Servlets for WAP
  • It is possible to use Servlets to generate WML
  • PrintWriter out response.getWriter()
  • out.println("lt?xml version\"1.0\"?gt")
  • out.println("lt!DOCTYPE wml etc")
  • out.println("ltwmlgt")
  • out.println("ltcard title\"MobileDate\"gt")
  • out.println(" ltp align\"center\"gt")
  • out.println("Date and Time Serviceltbr/gt")
  • out.println("Date is " new java.util.Date())
  • etc      

19
What else?
  • Programming cookies and keeping track of sessions
    is easy with Servlets.APIs provided for this
  • Explore Cookies and Session Management on your
    own!
  • Something to think about handheld devices do not
    support cookies, so how do you keep track of
    sessions??

20
JSP
  • Server-side technology
  • Enables you to embed Java code within an HTML
    document
  • JSP documents have the extension .jsp
  • When an HTTP request is received, the compilation
    engine converts the JSP document into a Java
    Servlet then the servlet will be loaded
  • Java code is embedded between lt and gt

21
Example
  • // file hello.jsp
  • lthtmlgtltheadgtlttitlegtexamplelt/titlegtlt/headgt
  • ltbodygt
  • lt String visitor request.getParameter(user)
  • if (visitor null) visitor there
  • gt
  • Hello, lt visitor gt!
  • lt/bodygt
  • lt/htmlgt

22
Example
  • Request parameters are passed into JSP pages
    using normal HTTP parameter mechanisms (using GET
    and POST)
  • Therefore, for the hello.jsp
  • If invoked with http//host/hello.jsp it will
    print Hello, World!
  • If invoked with http//host/hello.jsp?userMary,
    it will print Hello, Mary!

23
JSP for WAP
  • lt?xml versiongt
  • lt!DOCTYPEetcgt
  • lt response.setContentTyp("text/vnd.wap.wml")
  • gt
  • ltwmlgt ltcard title"MobileDate"
  • Date and Time Serviceltbr/gt
  • Date is lt new java.util.Date() gt
  • lt/cardgt
  • lt/wmlgt
  • Or you can use the PAGE directive

24
What else?
  • Additional characters may appear after the
    initial lt, such as !, , or _at_ to further
    prescribe the meaning of the tag
  • lt! Double radius 3.5 gt
  • lt 2 Math.PI radius gt
  • lt_at_ include filenotice.html gt
  • It is possible to create re-usable components
    with JSP using Java Beans
  • Explore on your own!

25
HTTP Servers
  • If you dont have access to an HTTP server, Id
    recommend that you install Jakarta-Tomcat for
    your personal use .so you explore Servlets and
    JSP further.
  • http//jakarta.apache.org/tomcat
Write a Comment
User Comments (0)
About PowerShow.com