Intro to j Sps and Serv Lets

download Intro to j Sps and Serv Lets

of 23

Transcript of Intro to j Sps and Serv Lets

  • 7/31/2019 Intro to j Sps and Serv Lets

    1/23

    Saturday, October 11, 2008

    Xabriel J. Collazo MojicaTA for ICOM 5016

    University of Puerto Rico at Mayagez

    Introduction to JavaServer

    Pages & Servlets

    1

  • 7/31/2019 Intro to j Sps and Serv Lets

    2/23

    Saturday, October 11, 2008

    TheoryMVC Pattern

    JSPs & Servlets denedThings you need to know

    Project Hierarchy

    Practice Java on a JSP

    Scriplets, EL & Standard ActionsYour projects rst phaseQuick workshop

    Agenda

    2

  • 7/31/2019 Intro to j Sps and Serv Lets

    3/23

    Saturday, October 11, 2008

    MVC Pattern in a Web context

    From [2]

    3

  • 7/31/2019 Intro to j Sps and Serv Lets

    4/23

    Saturday, October 11, 2008

    MVC Pattern in Java EE context

    From [1]

    4

  • 7/31/2019 Intro to j Sps and Serv Lets

    5/23

    Saturday, October 11, 2008

    Servlets are Java programming language classesthat dynamically process requests and constructresponses.

    JSP pages are text-based documents thatexecute as servlets but allow a more naturalapproach to creating static content.

    Suns definitions

    From [1]

    5

  • 7/31/2019 Intro to j Sps and Serv Lets

    6/23

    Saturday, October 11, 2008

    Youll need to learn a few things:HTML : Hyper Text Markup Language

    Markup language for Web pagesCSS : Cascade Style Sheets

    Used to describe the presentation of a documentwritten in a markup language

    Servlet API & JSP APIUsed to make al l the nif ty dynamic tricks.

    SQL : Structured Query LanguageThe core of your project.

    Things you need to know

    6

    http://en.wikipedia.org/wiki/SQLhttp://tomcat.apache.org/tomcat-5.5-doc/servletapi/index.htmlhttp://tomcat.apache.org/tomcat-5.5-doc/jspapi/http://en.wikipedia.org/wiki/SQLhttp://en.wikipedia.org/wiki/SQLhttp://tomcat.apache.org/tomcat-5.5-doc/jspapi/http://tomcat.apache.org/tomcat-5.5-doc/jspapi/http://tomcat.apache.org/tomcat-5.5-doc/servletapi/index.htmlhttp://tomcat.apache.org/tomcat-5.5-doc/servletapi/index.htmlhttp://en.wikipedia.org/wiki/Cascading_Style_Sheetshttp://en.wikipedia.org/wiki/Cascading_Style_Sheetshttp://en.wikipedia.org/wiki/HTMLhttp://en.wikipedia.org/wiki/HTML
  • 7/31/2019 Intro to j Sps and Serv Lets

    7/23

    Saturday, October 11, 2008

    and perhaps:A little JavaScript

    A scripting language. For dynamic stu f on the clientside; its what Gmail and Facebook rely on.

    EL & JSTLNew way to do Java without Java inside JSPs.

    Things you may need to know

    7

    http://java.sun.com/products/jsp/jstl/reference/docs/index.htmlhttp://java.sun.com/products/jsp/jstl/reference/docs/index.htmlhttp://en.wikipedia.org/wiki/Unified_Expression_Languagehttp://en.wikipedia.org/wiki/Unified_Expression_Languagehttp://en.wikipedia.org/wiki/Javascripthttp://en.wikipedia.org/wiki/Javascript
  • 7/31/2019 Intro to j Sps and Serv Lets

    8/23

    Saturday, October 11, 2008

    Eclipse / Sun Project Hierarchy

    Sun

    8

  • 7/31/2019 Intro to j Sps and Serv Lets

    9/23

    Saturday, October 11, 2008

    Project > Properties > Java Build Path >Libraries

    For example, if it is theServlet API we want toadd, we click on AddExternal JARs andbrowse to the jar le.

    Note that in the case of Tomcat, the jar we wantis at: /tomcat_root/common/lib/servlet-api.jar

    How to add aReference inEclipse

    9

  • 7/31/2019 Intro to j Sps and Serv Lets

    10/23

    Saturday, October 11, 2008

    You can add Java to a JSP in three di f erent ways:As a Directive

    As a Scriplet

    As an Expression

    ava on a JSP

    10

  • 7/31/2019 Intro to j Sps and Serv Lets

    11/23

    Saturday, October 11, 2008

    Imports a package to use it inside the JSP.

    Embeds another JSP (or HTML [or anything!]) intothe current JSP. Great to avoid duplication code!

    Relevant JSP directives

    11

  • 7/31/2019 Intro to j Sps and Serv Lets

    12/23

    Saturday, October 11, 2008

    Actual code from MY class project ( Fall 2007)

    ava inside JSPs?

    12

  • 7/31/2019 Intro to j Sps and Serv Lets

    13/23

    Saturday, October 11, 2008

    So the guys behind all this stu f , after quite awhile, realized that is bad practice to embed Javain JSPs.

    What did they do? They invented anotherpseudo-language and a big library:

    Standard ActionsEL = Expression LanguageStandard Tag Library : JSTL

    What should we use then?

    13

  • 7/31/2019 Intro to j Sps and Serv Lets

    14/23

    Saturday, October 11, 2008

    EL : A simpler way to invoke Java codeExample:

    email: ${applicationScope.mail}

    Standard Actions : Cute ways to write Java (I haveno better denition!)

    Example:

    EL & Standard Actions

    14

  • 7/31/2019 Intro to j Sps and Serv Lets

    15/23

    Saturday, October 11, 2008

    JSTL : Everyday stu f (like loops & conditionals)Example:

    ${person.name} ${person.age} ${person.height}

    15

    STL

  • 7/31/2019 Intro to j Sps and Serv Lets

    16/23

    Saturday, October 11, 2008

    Quick comparison: Scriplets vs.

    16

  • 7/31/2019 Intro to j Sps and Serv Lets

    17/23

    Saturday, October 11, 2008

    Quick comparison: EL vs. SA

    17

  • 7/31/2019 Intro to j Sps and Serv Lets

    18/23

    Saturday, October 11, 2008

    The course professors dont care how you do itas long as it uses Java, JSPs and Servlets.

    So, use whatever you want: scriplets, EL,anything.

    But be advised!

    I recommend you use EL, JSTL and/or StandardActions!In that order of preference!

    What about YOUR project

    18

  • 7/31/2019 Intro to j Sps and Serv Lets

    19/23

    Saturday, October 11, 2008

    Nothing of this applies to the rst phase of yourproject! Lucky you!

    All you need to do for now is some dummy JSPpages with servlets that redirect to more dummy

    JSPs.

    Anyway

    19

  • 7/31/2019 Intro to j Sps and Serv Lets

    20/23

    Saturday, October 11, 2008

    protected void doGet(HttpServletRequestrequest, HttpServletResponse response)throws ServletException, IOException {

    String addr = aJSPAddress" ;RequestDispatcher dispatcher =

    request.getRequestDispatcher(addr);

    dispatcher.forward(request,response);

    }

    Your Servlets (for now :)

    20

  • 7/31/2019 Intro to j Sps and Serv Lets

    21/23

    Saturday, October 11, 2008

  • 7/31/2019 Intro to j Sps and Serv Lets

    22/23

    Saturday, October 11, 2008

    Heres where you actually do some work

    WhatYouNeedToDoForFirstPhase

    Workshop

    22

  • 7/31/2019 Intro to j Sps and Serv Lets

    23/23

    Saturday, October 11, 2008

    [1] The Java EE 5 Tutorial. http://java.sun.com/ javaee/5/docs/tutorial/doc/index.html .

    [2] BetterExplained MVC tutorial. http://

    betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/

    Book: Head First Servlets & JSP. OReilly. Some year :)

    All other links direct to the Wikipedia articles on therespective subjects.

    References & Further Info

    23

    http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/http://java.sun.com/javaee/5/docs/tutorial/doc/index.htmlhttp://java.sun.com/javaee/5/docs/tutorial/doc/index.htmlhttp://java.sun.com/javaee/5/docs/tutorial/doc/index.htmlhttp://java.sun.com/javaee/5/docs/tutorial/doc/index.html