Week5 Intro Server Side i

download Week5 Intro Server Side i

of 51

Transcript of Week5 Intro Server Side i

  • 8/11/2019 Week5 Intro Server Side i

    1/51

    Introduction toServer-Side Web Development

    Introduction to Server-Side Web

    Development

    Server-Side Web Development with Servlets and JSP; basic concepts and syntax

    16 th February 2006

    Bogdan L. [email protected]

  • 8/11/2019 Week5 Intro Server Side i

    2/51

    Introduction toServer-Side Web Development

    16 th February 2006 Bogdan L. Vrusias 2006 2

    Introduction Most web sites want to display dynamic content based on

    users requests and expectations.

    Most contents, such as images, text, and banner ads, areeasiest to build with HTML editors.

    We need to mix the "static" content of HTML files with"directives" for accessing or generating dynamic content.

    Introducing:

  • 8/11/2019 Week5 Intro Server Side i

    3/51

    Introduction toServer-Side Web Development

    16 th February 2006 Bogdan L. Vrusias 2006 3

    Contents History of JavaServer Pages (JSP)

    Fundamentals of server-side programming.

    Comparison to other server-side languages

    Fundamentals of JavaServer Pages / Servlets.

    Servlets

    JavaServer Pages

    Tomcat

  • 8/11/2019 Week5 Intro Server Side i

    4/51

    Introduction toServer-Side Web Development

    16 th February 2006 Bogdan L. Vrusias 2006 4

    History: The Beginning 1991 beginning of WWW beginning of HTML

    No dynamic pages the Webmaster

    Too many static HTML pages had to be created for each

    website. Need for change

  • 8/11/2019 Week5 Intro Server Side i

    5/51

    Introduction toServer-Side Web Development

    16 th February 2006 Bogdan L. Vrusias 2006 5

    History: First approach - CGI Solution C ommon G ateway Interface ( CGI ) technology

    created.

    Dynamic pagesusing Perl and C

    Problem these languages are not the simplest Need for change

    Java had the answer Servlets

  • 8/11/2019 Week5 Intro Server Side i

    6/51

    Introduction toServer-Side Web Development

    16 th February 2006 Bogdan L. Vrusias 2006 6

    History: Servlets and JSP Servlets are pure Java objects that generate HTML by writing it to a

    stream .

    The files do not contain any HTML tags; they simply contain Javacode which write out HTML (as strings) to produce the page that issent to the user's browser.

    but it was quite painful to develop any large scale application with a bunch of servlets running around doing println() statements.

    This is why Sun invented JSPs in the first place: to compete with theease-of-development of Microsoft's ASP files, which could combine

    HTML tags and code in a single file.

  • 8/11/2019 Week5 Intro Server Side i

    7/51

  • 8/11/2019 Week5 Intro Server Side i

    8/51

    Introduction toServer-Side Web Development

    16 th February 2006 Bogdan L. Vrusias 2006 8

    A dynamic Web Page Static web page is a page whose content consists of some HTML that

    was predetermined and was typed directly into a text editor and savedas " .htm" or " .html " file.

    Dynamic web page is a page whose content is generated at runtimedynamically.

    6. Browser processes HTMLand displays page.

    2. Client request web page

    1. Author writesinstructions

    3. Web serverlocates instructions

    file4. Web server

    processesinstructions tocreate HTML

    5. HTML stream returned to browser

  • 8/11/2019 Week5 Intro Server Side i

    9/51

    Introduction toServer-Side Web Development

    16 th February 2006 Bogdan L. Vrusias 2006 9

    Requests and Responses

  • 8/11/2019 Week5 Intro Server Side i

    10/51

    Introduction toServer-Side Web Development

    16 th February 2006 Bogdan L. Vrusias 2006 10

    Other Server-Side programming languages CGI

    Perl, C, C++

    ColdFusion Tags with encapsulated functionality

    ASP (Active Server Pages) and ASP .NET VBasic, JavaScript, C# ADO

    PHP (Personal Home Pages) Syntax similar to C and Perl Open-source, cross-platform

  • 8/11/2019 Week5 Intro Server Side i

    11/51

    Introduction toServer-Side Web Development

    16 th February 2006 Bogdan L. Vrusias 2006 11

    JSP vs .NET and ASP Similarities

    Create interactive pages with web-based applications Separates programming logic from page design through the use of

    component technology Faster and Easier alternative to older, more cryptic technologies

    such as CGI

    Differences (?) JSP: Platform and server Independent

    JSP can run on any web server

    Supported by large number of tools ASP: Relies on Microsoft platforms and servers

    Restricted to MS Windows based platforms because of ActiveXControls ( that is to change now? )

    Can be ported to different platforms using third party products, butActiveX must be present on the platform.

  • 8/11/2019 Week5 Intro Server Side i

    12/51

    Introduction toServer-Side Web Development

    16 th February 2006 Bogdan L. Vrusias 2006 12

    Why Use JSPs? Write once, run anywhere ( JAVA ).

    JSPs are entirely platform independent, both in dynamic Web pages and underlying server components.

    Emphasize components. JSPs encourage the use of reusable, cross-platform server

    components called JavaBeans. Therefore saves development time,while giving you the power and flexibility of the Java

    programming language.

    Provide a front door to the Enterprises Java platform. JSPs are an integral part of the Java Platform for the Enterprise,

    which brings Java technology to enterprise computing. You candevelop enterprise-wide or middle-tier server applications, using aJSP web site as a front end.

  • 8/11/2019 Week5 Intro Server Side i

    13/51

    Introduction toServer-Side Web Development

    16 th February 2006 Bogdan L. Vrusias 2006 13

    Advantages of Java According to java.sun.com:

    More mature, powerful, and scalable than Basic-based scriptinglanguages

    Java helps developers protect against system crashes, ASP and NT

    systems are susceptible to crashing Java helps with memory management by providing protection

    against memory leaks

    Well supported, large collection of APIs

  • 8/11/2019 Week5 Intro Server Side i

    14/51

    Introduction toServer-Side Web Development

    16 th February 2006 Bogdan L. Vrusias 2006 14

    JSP Model Architectures

    Model 1 Model 2

    BROWSER

    SERVLET(CONTROLLER)

    JSP(VIEW)

    JAVABEAN

    requestresponse

    JSP / Servlet Container

    DATADATA

    Data Tier

    BROWSER

    JSP PAGE

    JAVABEAN

    requestresponse

    JSP / Servlet Container

    DATADATA

    Data Tier

  • 8/11/2019 Week5 Intro Server Side i

    15/51

  • 8/11/2019 Week5 Intro Server Side i

    16/51

    Introduction toServer-Side Web Development

    16 th February 2006 Bogdan L. Vrusias 2006 16

    What are Servlets? Servlets are objects that reside on a Java enabled web

    server, that work on the request-response principle.

    Servlets are to the server-side what applets are to the clientside

    Servlets do not have a GUI associated with them

    Servlets are faster and cleaner than CGI scripts

    Servlets use a standard API

    Servlets provide all the advantages of Java

  • 8/11/2019 Week5 Intro Server Side i

    17/51

  • 8/11/2019 Week5 Intro Server Side i

    18/51

    Introduction toServer-Side Web Development

    16 th February 2006 Bogdan L. Vrusias 2006 18

    Servlet Code Sampleimport javax.servlet.*;import java.io.*;

    import java.util.*;

    public class SimpleServlet extends GenericServlet {

    public void service(ServletRequest request, ServletResponse response)

    throws ServletException, IOException {response.setContentType("text/html");

    printWriter pw = response.getWriter();

    pw.println("\n");

    for(int counter = 1; counter

  • 8/11/2019 Week5 Intro Server Side i

    19/51

    Introduction toServer-Side Web Development

    16 th February 2006 Bogdan L. Vrusias 2006 19

    Servlet Lifecycle1. When a server loads a servlet, it runs the servlet's init() method.

    Even though most servlets are run in multi-threaded servers, there areno concurrency issues during servlet initialization.

    2. After the server loads and initializes the servlet, the servlet is able tohandle client requests. It processes them in its service() method.

    3. Servlets run until they are removed from the service, by calling theservlet's destroy() method.

    NOTE: Servlets can run multiple service methods at a time. It isimportant, therefore, that service methods be written in a thread-safemanner. If, for some reason, a server should not run multiple servicemethods concurrently, the servlet should implement theSingleThreadModel interface.

  • 8/11/2019 Week5 Intro Server Side i

    20/51

    Introduction toServer-Side Web Development

    16 th February 2006 Bogdan L. Vrusias 2006 20

    Servlet Lifecycle Diagram

    Uninstantiated

    Instantiated

    Initialised

    ServicingRequests

    ProcessRequests

    ProcessRequests

    GarbageCollectionservice() destroy()

    init()

    doGet() doPost()

  • 8/11/2019 Week5 Intro Server Side i

    21/51

    Introduction toServer-Side Web Development

    16 th February 2006 Bogdan L. Vrusias 2006 21

    Servlet Architecture Overview The central abstraction in the Servlet API is the Servlet interface.

    All servlets implement this interface, either directly or, morecommonly, by extending a class that implements it such asHttpServlet .

    When a servlet accepts a call from a client, it receives two objects: oneis a ServletRequest and the other is a ServletResponse . The ServletRequest class encapsulates the communication from the

    client to the server The ServletResponse class encapsulates the communication from the

    servlet back to the client.

  • 8/11/2019 Week5 Intro Server Side i

    22/51

    Introduction toServer-Side Web Development

    16 th February 2006 Bogdan L. Vrusias 2006 22

    ServletRequest The ServletRequest interface allows the servlet access to

    information such as: the names of the parameters passed in by the client the protocol (scheme) being used by the client the names of the remote host that made the request and the server that

    received it.

    It also provides the servlet with access to the input stream,ServletInputStream , through which the servlet gets data fromclients that are using application protocols such as the http POST method.

    CLIENT SERVER

  • 8/11/2019 Week5 Intro Server Side i

    23/51

    Introduction toServer-Side Web Development

    16 th February 2006 Bogdan L. Vrusias 2006 23

    ServletResponse

    The ServletResponse interface gives the servlet methods forreplying to the client. It allows the servlet to: set the content length and mime type of the reply provides an output stream, ServletOutputStream , and a "writer"

    through which the servlet can send the reply data.

    Subclasses of ServletResponse give the servlet more protocol-specific capabilities. For example, HttpServletResponse contains methods that allow the servlet to manipulate HTTP-specificheader information.

    CLIENT SERVER

  • 8/11/2019 Week5 Intro Server Side i

    24/51

    Introduction toServer-Side Web Development

    16 th February 2006 Bogdan L. Vrusias 2006 24

    Interacting with Clients I Servlet writers who are developing HTTP servlets that

    specialize the HttpServlet class should override themethod or methods designed to handle the HTTPinteractions that their servlet will handle.

    The candidate methods include: doGet , for handling GET, conditional GET and HEAD requests doPost , for handling POST requests doPut , for handling PUT requests doDelete , for handling DELETE requests

  • 8/11/2019 Week5 Intro Server Side i

    25/51

    I d i

  • 8/11/2019 Week5 Intro Server Side i

    26/51

    Introduction toServer-Side Web Development

    16 th February 2006 Bogdan L. Vrusias 2006 26

    Reading Form Data Example - GET

    When submitted, the browser would execute the following URL:

    http://www.somewhere.com/servlet/aservlet?key1=x&key2=y

    Alternatively

    LINK

    I d i

  • 8/11/2019 Week5 Intro Server Side i

    27/51

    Introduction toServer-Side Web Development

    16 th February 2006 Bogdan L. Vrusias 2006 27

    Reading Form Data Example - POSTThe HTML

    ...

    Please Enter your name

    ...

    Example continues next page...

  • 8/11/2019 Week5 Intro Server Side i

    28/51

  • 8/11/2019 Week5 Intro Server Side i

    29/51

    Introd ction to

  • 8/11/2019 Week5 Intro Server Side i

    30/51

    Introduction toServer-Side Web Development

    16 th February 2006 Bogdan L. Vrusias 2006 30

    How Servlets relate to JSPs JSPs and servlets are two different ways to accomplish the

    same goal: generating dynamic HTML pages using Javacode. One puts Java code in your HTML, and one putsHTML in your Java code .

    Functionally, they are equivalent. In fact, under the covers,the web server takes a JSP and converts it to thecorresponding servlet and dynamically compiles it.

    BUT servlets have the following deficiencies: It is hard to write and maintain the HTML You cannot use standard HTML tools The HTML is inaccessible to non-Java developers

  • 8/11/2019 Week5 Intro Server Side i

    31/51

  • 8/11/2019 Week5 Intro Server Side i

    32/51

    Introduction to

  • 8/11/2019 Week5 Intro Server Side i

    33/51

    Introduction toServer-Side Web Development

    16 th February 2006 Bogdan L. Vrusias 2006 33

    Scriptlets

    e.g.

  • 8/11/2019 Week5 Intro Server Side i

    34/51

    Introduction toServer-Side Web Development

    16 th February 2006 Bogdan L. Vrusias 2006 34

    ExpressionsAn expression prints out a string value on the page's bufferoutput:

    Using scriplets this would look like:

    Introduction to

  • 8/11/2019 Week5 Intro Server Side i

    35/51

    Introduction toServer-Side Web Development

    16 th February 2006 Bogdan L. Vrusias 2006 35

    Declarations 0 );

    }

    %>

    Introduction to

  • 8/11/2019 Week5 Intro Server Side i

    36/51

    Introduction toServer-Side Web Development

    16 th February 2006 Bogdan L. Vrusias 2006 36

    CommentsThe comments are not printed on page's buffer, therefore are not viewed by theclient.

    Introduction to

  • 8/11/2019 Week5 Intro Server Side i

    37/51

    Introduction toServer-Side Web Development

    16 th February 2006 Bogdan L. Vrusias 2006 37

    Directives Here is what directives look like in a general form:

    There are three directives: specifies information that affects the page

    e.g.

    includes a file at the location of the include directive(parsed)e.g.

    allows the use of custom tags in the pagee.g.

    Introduction to

  • 8/11/2019 Week5 Intro Server Side i

    38/51

    Introduction toServer-Side Web Development

    16 th February 2006 Bogdan L. Vrusias 2006 38

    Page Directive

    Introduction to

  • 8/11/2019 Week5 Intro Server Side i

    39/51

    Introduction toServer-Side Web Development

    16 th February 2006 Bogdan L. Vrusias 2006 39

    Include Directive

    Use the include directive to include a file in the main JSPdocument at the time the document is translated into aservlet.

    What is included is the actual content of the respective file.

  • 8/11/2019 Week5 Intro Server Side i

    40/51

    Introduction to

  • 8/11/2019 Week5 Intro Server Side i

    41/51

    Introduction toServer-Side Web Development

    16 th February 2006 Bogdan L. Vrusias 2006 41

    Include Action Element The jsp:include action lets you include the output (not the actual code )

    of a page at request time:

    NOTE : Is it similar to ???

  • 8/11/2019 Week5 Intro Server Side i

    42/51

    Introduction to

  • 8/11/2019 Week5 Intro Server Side i

    43/51

    Server-Side Web Development

    16 th February 2006 Bogdan L. Vrusias 2006 43

    Implicit Objects and the JSP Environment The scriptlets and expressions written in a JSP page do not stand alone

    as a complete program they need an environment in which tooperate.

    The JSP container provides this environment and makes it accessibleto the page author through what are called implicit objects :

    request (HttpServletRequest ) response (HttpServletResponce ) application (ServletContext ) session (HttpSession ) out (JspWriter ) pageContext config page exception

    Introduction to

  • 8/11/2019 Week5 Intro Server Side i

    44/51

    Server-Side Web Development

    16 th February 2006 Bogdan L. Vrusias 2006 44

    Basic request Methods String getParameter (String name) Enumeration getParameterNames ()

    String getMethod () (e.g "POST", "GET")

    Cookie[] getCookies ()

    String getHeader(String name)

    Enumeration getHeaderNames()

    HttpSession getSession(boolean create)

    ...

    Introduction to

  • 8/11/2019 Week5 Intro Server Side i

    45/51

    Server-Side Web Development

    16 th February 2006 Bogdan L. Vrusias 2006 45

    Basic response Methods void sendRedirect () void addCookie (Cookie cookie)

    void setHeader(String name)

    void addHeader(String name)

    void sendError(int sc [, String msg])

    Introduction to

  • 8/11/2019 Week5 Intro Server Side i

    46/51

    Server-Side Web Development

    16 th February 2006 Bogdan L. Vrusias 2006 46

    Basic session Methods Object getAttribute (String name) void setAttribute (String name, Object value)

    Enumeration getAttributeNames ()

    int setMaxInactiveInterval ()

    int getMaxInactiveInterval()

    void invalidate()

    void isNew()

    void logout()

    String getId()

    Introduction to

  • 8/11/2019 Week5 Intro Server Side i

    47/51

    Server-Side Web Development

    16 th February 2006 Bogdan L. Vrusias 2006 47

    Basic application Methods String getRealPath (String path) Object getAttribute (String name)

    void setAttribute (String name , Object value)

    void removeAttribute (String name)

    Enumeration getAttributeNames ()

    void log(String msg)

    URL getResource(String path)

    Introduction to

  • 8/11/2019 Week5 Intro Server Side i

    48/51

    Server-Side Web Development

    16 th February 2006 Bogdan L. Vrusias 2006 48

    Basic out Methods void print (String value) void println (String value)

    void newLine ()

    void clear()

    Introduction to

  • 8/11/2019 Week5 Intro Server Side i

    49/51

    Server-Side Web Development

    16 th February 2006 Bogdan L. Vrusias 2006 49

    Tomcat

    Tomcat is the official JavaServer Pages Web server. Tomcat is the official reference implementation of the Java

    Servlet and JavaServer Pages technologies. Developedunder the Apache license in an open and participatory

    environment. Other popular Web servers are:

    Apache Java Application Server Macromedia JRun Jetty Internet Information Server (IIS) Jigsaw

    http://jakarta.apache.org/tomcat/

    http://java.sun.com/products/jsp/tomcat/
  • 8/11/2019 Week5 Intro Server Side i

    50/51

    Introduction to

  • 8/11/2019 Week5 Intro Server Side i

    51/51

    Server-Side Web Development

    Closing

    Questions???

    Remarks???

    Comments!!! Evaluation!