You are on page 1of 23

Introduction to JavaServer Pages & Servlets

Xabriel J. Collazo Mojica TA for ICOM 5016 University of Puerto Rico at Mayagez

Saturday, October 11, 2008

Agenda
Theory

MVC Pattern JSPs & Servlets dened Things you need to know Project Hierarchy

Practice

Java on a JSP Scriplets, EL & Standard Actions Your projects rst phase Quick workshop
Saturday, October 11, 2008

MVC Pattern in a Web context

From [2]
Saturday, October 11, 2008

MVC Pattern in Java EE context

From [1]
Saturday, October 11, 2008

Suns definitions
Servlets are Java programming language classes

that dynamically process requests and construct responses.

JSP pages are text-based documents that

execute as servlets but allow a more natural approach to creating static content.

From [1]
Saturday, October 11, 2008

Things you need to know


Youll need to learn a few things:
HTML : Hyper Text Markup Language Markup language for Web pages CSS : Cascade Style Sheets
Used to describe the presentation of a document

written in a markup language

Servlet API & JSP API Used to make all the nifty dynamic tricks. SQL : Structured Query Language
The core of your project.

Saturday, October 11, 2008

Things you may need to know


and perhaps:
A little JavaScript A scripting language. For dynamic stuff on the client side; its what Gmail and Facebook rely on. EL & JSTL
New way to do Java without Java inside JSPs.

Saturday, October 11, 2008

Eclipse / Sun Project Hierarchy

Sun

Saturday, October 11, 2008

How to add a Reference in Eclipse


Project > Properties > Java Build Path > Libraries

For example, if it is the Servlet API we want to add, we click on Add External JARs and browse to the jar le.

Note that in the case of Tomcat, the jar we want is at: /tomcat_root/ common/lib/servletapi.jar

Saturday, October 11, 2008

Java on a JSP
You can add Java to a JSP in three different ways:
As a Directive <%@ page import=foo.* %> As a Scriplet

<% out.println(foo.javaGoesHere())

; %>

As an Expression <%= foo.javaGoesHere() %>

Saturday, October 11, 2008

10

Relevant JSP directives


<%@ page import=foo.* %>
Imports a package to use it inside the JSP.

<%@ include le=foo.jsp %>


Embeds another JSP (or HTML [or anything!]) into

the current JSP. Great to avoid duplication code!

Saturday, October 11, 2008

11

Java inside JSPs?

Actual code from MY class project ( Fall 2007)

<%if(request.getParameter("mainTable") == null){ %> <jsp:include page="include/indexMainTableRoundTrip.jsp" /> <%} else if(request.getParameter("mainTable").equalsIgnoreCase("OneWay")){ %> <jsp:include page="include/indexMainTableOneWay.jsp" /> <%} else if(request.getParameter("mainTable").equalsIgnoreCase("MultiWay")){ %> <jsp:include page="include/indexMainTableMultiWay.jsp" /> <%} else{ %> <jsp:include page="include/indexMainTableRoundTrip.jsp" /> <%} %>

Saturday, October 11, 2008

12

What should we use then?


So the guys behind all this stuff, after quite a

while, realized that is bad practice to embed Java in JSPs.

What did they do? They invented another

pseudo-language and a big library:

Standard Actions EL = Expression Language Standard Tag Library : JSTL


Saturday, October 11, 2008

13

EL & Standard Actions


EL : A simpler way to invoke Java code
Example: email: ${applicationScope.mail}

Standard Actions : Cute ways to write Java (I have

no better denition!)
Example:

<jsp:getProperty name="student" property="rstName"/>

Saturday, October 11, 2008

14

JSTL
JSTL : Everyday stuff (like loops & conditionals)
Example:

<table> <c:forEach var="person" items="${people.people}"> <tr> <td>${person.name}</td> <td>${person.age}</td> <td>${person.height}</td> </tr> </c:forEach> </table>
Saturday, October 11, 2008

15

Quick comparison: Scriplets vs.

Saturday, October 11, 2008

16

Quick comparison: EL vs. SA

Saturday, October 11, 2008

17

What about YOUR project


The course professors dont care how you do it

as 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 Standard

Actions! In that order of preference!

Saturday, October 11, 2008

18

Anyway
Nothing of this applies to the rst phase of your

project! Lucky you!

All you need to do for now is some dummy JSP

pages with servlets that redirect to more dummy JSPs.

Saturday, October 11, 2008

19

Your Servlets (for now :)


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String addr = aJSPAddress"; RequestDispatcher dispatcher = request .getRequestDispatcher(addr); dispatcher.forward(request, response); }
Saturday, October 11, 2008

20

Your JSPs (for now :)


<html> <form name=someName" action="./ ServletAddress"> First name: <input type="text" name="firstname"><br> Last name: <input type="text" name="lastname"><br> <input type="submit" </form> </html> name="Submit">

Saturday, October 11, 2008

21

WhatYouNeedToDoForFirstPhase Workshop
Heres where you actually do some work

Saturday, October 11, 2008

22

References & Further Info


[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-railsunderstanding-models-views-and-controllers/

Book: Head First Servlets & JSP. OReilly. Some year :) All other links direct to the Wikipedia articles on the

respective subjects.

Saturday, October 11, 2008

23

You might also like