J0 1 Marco Ronchetti - [email protected] Servlets e JSP.

52
J0 1 Marco Ronchetti - [email protected] Servlets e JSP

Transcript of J0 1 Marco Ronchetti - [email protected] Servlets e JSP.

Page 1: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J01

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

Servlets e JSP

Page 2: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J02

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

Applets

Special Java programs (without a “main”) callable from HTML and executed in a graphic context.

They can be executed by:

a Java enabled Web Browser;

ad-hoc programs (e.g. Sun AppletViewer).

Page 3: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J03

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

Applets

Every applet is implemented by creating a subclass of the Applet class.

The hierarchy determines much of what an applet can do and how.

Page 4: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J04

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

Applet Lifecycle

An applet can react to major events in the following ways:

It can initialize itself. init() It can start running. start() It can draw some graphics. paint() It can respond to user-generated events

(Mouse, keyboard, menus…). handleEvent() It can stop running. stop() It can perform a final cleanup, in preparation

for being unloaded. destroy()

Page 5: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J05

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

Applet Lifecycle

Whenever it’s needed,at lower priority

init()

stop()

destroy()

start()

handleEvent()

Multithreading!

Actually, more threads are active behind the scenes.

paint()

Page 6: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J06

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

handleEvent()

This code is part of the AWT (1.0 Event Model)

public boolean handleEvent(Event evt) {switch (evt.id) {

case Event.MOUSE_ENTER: return mouseEnter(evt, evt.x, evt.y);

case Event.MOUSE_EXIT: return mouseExit(evt, evt.x, evt.y);

case Event.MOUSE_MOVE: return mouseMove(evt, evt.x, evt.y);case Event.MOUSE_DOWN: return mouseDown(evt, evt.x,

evt.y);case Event.MOUSE_DRAG: return mouseDrag(evt, evt.x, evt.y);case Event.MOUSE_UP: return mouseUp(evt, evt.x, evt.y);

Page 7: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J07

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

handleEvent()

case Event.KEY_PRESS: case Event.KEY_ACTION: return keyDown(evt, evt.key);

case Event.KEY_RELEASE: case Event.KEY_ACTION_RELEASE: return keyUp(evt,

evt.key); case Event.ACTION_EVENT: return action(evt, evt.arg);

case Event.GOT_FOCUS: return gotFocus(evt,

evt.arg); case Event.LOST_FOCUS: return lostFocus(evt,

evt.arg);}

return false; }

Page 8: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J08

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

Applets-Event handlingTo react to an event, an applet must override

either the appropriate event-specific method or the handleEvent method.

For example, adding the following code to the Simple applet makes it respond to mouse clicks.

import java.awt.Event; . . . public boolean mouseDown(Event event, int x, int y)

{ addItem("click!... "); return true;

}

Page 9: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J09

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

Servlets (JDK 1.2)Servlets are modules that extend Java-enabled web

servers. For example, a servlet might be responsible for taking data in an HTML order-entry form and applying the business logic used to update a company's order

database.

Servlets are to servers what applets are to browsers. Unlike applets, however, servlets have no graphical

user interface. For a full tutorial, see http://java.sun.com/docs/books/tutorial/

servlets/overview/index.html

Page 10: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J010

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

Other uses of servlets

Allowing collaboration between people. A servlet can handle multiple requests concurrently, and can synchronize requests. This allows servlets to

support systems such as on-line conferencing.  

Forwarding requests. Servlets can forward requests to other servers and servlets. Thus servlets can be used to balance load

among several servers that mirror the same content, and to partition a single logical service over several

servers, according to task type or organizational boundaries.

 

Page 11: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J011

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

Applets vs. Servlets   Applet Servlet

Gira: Client Server

Ha un main:

NO NO

Estende: java.applet.Applet javax.servlet.http.HttpServlet

Grafica SI NO

Cuore: handleEvent() service()

Page 12: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J012

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

Servlet Lifecycle

init()

destroy()

service(HttpServletRequest r, HttpServletResponse p)

Chiamato solo la prima volta che la Servlet viene caricato in memoria!

doGet()

doPost()

doXXX()

Solo quando serve scaricare dalla memoria!

Se la Servlet implements SingleThreadModel non ci saranno esecuzioni simultanee di codice

Page 13: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J013

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

service()This code is part of the class HttpService

protected void service (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String method = req.getMethod (); if (method.equals ("GET")) { long ifModifiedSince = req.getDateHeader ("If-Modified-Since"); long lastModified = getLastModified (req); maybeSetLastModified (resp, lastModified); if (ifModifiedSince == -1 || lastModified == -1) doGet (req, resp); else {

long now = System.currentTimeMillis (); if (now < ifModifiedSince || ifModifiedSince < lastModified) doGet (req, resp); else resp.sendError

(HttpServletResponse.SC_NOT_MODIFIED); }

Page 14: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J014

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

service() } else if (method.equals ("HEAD")) { long lastModified; lastModified = getLastModified (req); maybeSetLastModified (resp, lastModified); doHead (req, resp); } else if (method.equals ("POST")) { doPost (req, resp); } else if (method.equals ("PUT")) { doPut(req, resp); } else if (method.equals ("DELETE")) { doDelete(req, resp); } else if (method.equals ("OPTIONS")) { doOptions(req,resp); } else if (method.equals ("TRACE")) { doTrace(req,resp); } else { resp.sendError (HttpServletResponse.SC_NOT_IMPLEMENTED,

"Method '" + method + "' is not defined in RFC 2068"); } }

Page 15: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J015

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

A taste of servlet programming-1 public class SimpleServlet extends HttpServlet { /** Handle the HTTP GET method by building a simple web page. */ public void doGet (HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException {

PrintWriter out; String title = "Simple Servlet Output";

Page 16: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J016

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

A taste of servlet programming-2

// set content type and other response header fields first

response.setContentType("text/html"); // then write the data of the response out = response.getWriter();

out.println("<HTML><HEAD><TITLE>"); out.println(title); out.println("</TITLE></HEAD><BODY>");

out.println("<H1>" + title + "</H1>"); out.println("<P>This is output from

SimpleServlet."); out.println("</BODY></HTML>");

out.close(); }

}

Page 17: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J017

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

Esempio: ShowParameterspackage coreservlets;import java.io.*;import javax.servlet.*;import javax.servlet.http.*;import java.util.*;public class ShowParameters extends HttpServlet { public void doGet(HttpServletRequest request

HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title = "Reading All Request Parameters"; out.println(ServletUtilities.headWithTitle(title) + "<BODY BGCOLOR=\"#FDF5E6\">\n" + "<H1 ALIGN=CENTER>" + title + "</H1>\n" + "<TABLE BORDER=1 ALIGN=CENTER>\n" + "<TR BGCOLOR=\"#FFAD00\">\n" + "<TH>Parameter Name<TH>Parameter Value(s)");

Page 18: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J018

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

Esempio: ShowParametersEnumeration paramNames = request.getParameterNames(); while(paramNames.hasMoreElements()) { String paramName = (String)paramNames.nextElement(); out.print("<TR><TD>" + paramName + "\n<TD>"); String[] paramValues =

request.getParameterValues(paramName); if (paramValues.length == 1) { String paramValue = paramValues[0]; if (paramValue.length() == 0) out.println("<I>No Value</I>"); else out.println(paramValue); } else { out.println("<UL>"); for(int i=0; i<paramValues.length; i++) {out.println("<LI>"

+paramValues[i]); } out.println("</UL>"); } } out.println("</TABLE>\n</BODY></HTML>"); }

Page 19: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J019

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

Esempio: ShowParameters

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}

Page 20: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J020

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

Esempio: ShowParameters<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML><HEAD> <TITLE>A Sample FORM using POST </TITLE></HEAD><BODY BGCOLOR="#FDF5E6"><H1 ALIGN="CENTER">A Sample FORM using POST</H1>

<FORM ACTION="/servlet/coreservlets.ShowParameters“ METHOD="POST”>

Item Number: <INPUT TYPE="TEXT" NAME="itemNum"><BR> Quantity: <INPUT TYPE="TEXT" NAME="quantity"><BR> Price Each: <INPUT TYPE="TEXT" NAME="price" VALUE="$"><BR> <HR> First Name: <INPUT TYPE="TEXT" NAME="firstName"><BR> Last Name: <INPUT TYPE="TEXT" NAME="lastName"><BR> Middle Initial: <INPUT TYPE="TEXT" NAME="initial"><BR> Shipping Address: <TEXTAREA NAME="address" ROWS=3

COLS=40></TEXTAREA><BR>

Page 21: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J021

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

Esempio: ShowParameters

Credit Card:<BR> &nbsp;&nbsp;<INPUT TYPE="RADIO" NAME="cardType“

VALUE="Visa">Visa<BR> &nbsp;&nbsp;<INPUT TYPE="RADIO" NAME="cardType" VALUE="Master Card">Master Card<BR> &nbsp;&nbsp;<INPUT TYPE="RADIO" NAME="cardType" VALUE="Amex">American Express<BR> &nbsp;&nbsp;<INPUT TYPE="RADIO" NAME="cardType“

VALUE="Discover">Discover<BR> &nbsp;&nbsp;<INPUT TYPE="RADIO" NAME="cardType" VALUE="Java SmartCard">Java SmartCard<BR> Credit Card Number: <INPUT TYPE="PASSWORD" NAME="cardNum"><BR> Repeat Credit Card Number: <INPUT TYPE="PASSWORD" NAME="cardNum"><BR><BR> <CENTER><INPUT TYPE="SUBMIT" VALUE="Submit

Order"></CENTER></FORM></BODY></HTML>

Page 22: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J022

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

Cookies: perché?

Identificazione di un utente in una sessione di e-commerce.

Customizzazione di un sitoPubblicità mirata

Eliminazione di username e password

Page 23: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J023

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

Cookies: i metodi public void setComment(String c)public String getComment()

public void setVersion(int c)public int getVersion ()Version 0: Netscape standardVersion 1: RFC 2109

Page 24: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J024

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

Cookies: i metodi public void setMaxAge(int c)public int getMaxAge()

Positive value: secondi di vita0: delete cookieNegative value: finchè dura la sessione del browser

Page 25: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J025

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

Cookies: i metodi public void setDomain(String c)public String getDomain()

public void setPath(int c)public int getPath()

Page 26: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J026

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

Cookies: esempio

Cookie userCookie = new Cookie(“user”,”uid1234”);userCookie.setMaxAge(60*60*24*365);response.addCookie(userCookie);

Page 27: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J027

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

SetCookiesimport java.io.*; import javax.servlet.*; import javax.servlet.http.*;/** Sets six cookies: three that apply only to the current session * (regardless of how long that session lasts) and three that persist

for an hour * (regardless of whether the browser is restarted).*/public class SetCookies extends HttpServlet { public void doGet(HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException { for(int i=0; i<3; i++) { // Default maxAge is -1, indicating cookie // applies only to current browsing session. Cookie cookie = new Cookie("Session-Cookie-" + i, "Cookie-Value-S" + i); response.addCookie(cookie);

Page 28: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J028

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

cookie = new Cookie("Persistent-Cookie-" + i,"Cookie-Value-P" + i);

// Cookie is valid for an hour, regardless of whether // user quits browser, reboots computer, or whatever. cookie.setMaxAge(3600); response.addCookie(cookie); } response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title = "Setting Cookies"; out.println (ServletUtilities.headWithTitle(title) + "<BODY BGCOLOR=\"#FDF5E6\">\n" +"<H1

ALIGN=\"CENTER\">" + title + "</H1>\n" +"There are six cookies associated with this page.\n" +

"</BODY></HTML>"); }}

SetCookies

Page 29: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J029

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

import java.io.*; import javax.servlet.*; import javax.servlet.http.*;/** Creates a table of the cookies associated with the current page.

*/public class ShowCookies extends HttpServlet { public void doGet(HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title = "Active Cookies"; out.println(ServletUtilities.headWithTitle(title) + "<BODY BGCOLOR=\"#FDF5E6\">\n" + "<H1 ALIGN=\"CENTER\">" + title + "</H1>\n" + "<TABLE BORDER=1 ALIGN=\"CENTER\">\n" + "<TR BGCOLOR=\"#FFAD00\">\n" + " <TH>Cookie Name\n" + " <TH>Cookie Value");

ShowCookies

Page 30: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J030

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

Cookie[] cookies = request.getCookies(); Cookie cookie; for(int i=0; i<cookies.length; i++) { cookie = cookies[i]; out.println("<TR>\n" + " <TD>" + cookie.getName() + "\n" + " <TD>" + cookie.getValue()); } out.println("</TABLE></BODY></HTML>"); }}

ShowCookies

Page 31: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J031

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

String sessionID = makeUniqueString();Hashtable sessionInfo = new Hashtable();Hashtable globalTable = getTableStoringSession();globalTable.put(sessionID,sessionInfo);Cookie sessionCookie=new Cookie(“SessionID”,sessionID);sessionCookie.setPath(“/”);response.addCookie(sessionCookie);

Session tracking using cookies

Page 32: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J032

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

HttpSession session = request.getSession(true);ShoppingCart cart =

(ShoppingCart)session.getValue(“carrello”); // 2.1// 2.2

(ShoppingCart)session.getAttribute(“carrello”);if (cart==null) {

cart=new ShoppingCart(); session.putValue(“carrello”,cart); //2.1//2.2 session.putValue(“carrello”,cart);}doSomeThingWith(cart);

Session tracking API

Page 33: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J033

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it public void removeValue(String name); //2.1

public void removeAttribute(String name); //2.2

public String[] getValueNames() //2.1public Enumeration getAttributeNames() //2.2

Session tracking API

Page 34: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J034

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

public long getCreationTime();public long getLastAccessdTime();Secondi dal 1.1.1970, mezzanottepublic void removeAttribute(String name);

public int getMaxInactiveInterval();public void setMaxInactiveInterval(int sec);

public void invalidate();

Session tracking API

Page 35: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J035

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

import java.io.*; import javax.servlet.*; import javax.servlet.http.*;import java.net.*; import java.util.*;/** Simple example of session tracking. */public class ShowSession extends HttpServlet { public void doGet(HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title = "Session Tracking Example"; HttpSession session = request.getSession(true); String heading; // Use getAttribute instead of getValue in version 2.2. Integer accessCount =

(Integer)session.getValue("accessCount");

ShowSession

Page 36: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J036

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

if (accessCount == null) { accessCount = new Integer(0); heading = "Welcome Newcomer"; } else { heading = "Welcome Back"; accessCount = new Integer(accessCount.intValue() +

1); } // Use setAttribute instead of putValue in version 2.2. session.putValue("accessCount", accessCount);

ShowSession

Page 37: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J037

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

out.println(ServletUtilities.headWithTitle(title) + "<BODY BGCOLOR=\"#FDF5E6\">\n" + "<H1 ALIGN=\"CENTER\">" + heading + "</H1>\n" + "<H2>Information on Your Session:</H2>\n" + "<TABLE BORDER=1 ALIGN=\"CENTER\">\n" + "<TR BGCOLOR=\"#FFAD00\">\n" + " <TH>Info Type<TH>Value\n" + "<TR>\n" +" <TD>ID\n" +" <TD>" + session.getId() + "\n"

+ "<TR>\n" +" <TD>Creation Time\n" + " <TD>" + new Date(session.getCreationTime()) + "\n" + "<TR>\n" +" <TD>Time of Last Access\n" + " <TD>" +new Date(session.getLastAccessedTime()) + "\

n" + "<TR>\n" +" <TD>Number of Previous Accesses\n" +"

<TD>" + accessCount + "\n" + "</TABLE>\n" +"</BODY></HTML>"); }

ShowSession

Page 38: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J038

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it /** Handle GET and POST requests identically. */

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}

ShowSession

Page 39: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J039

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

<html> <body> <% out.println(“Hello World”); %> </body></html>

Simple.jsp

Page 40: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J040

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

JSP Lifecycle

Browser Servlet generato

Servlet compilato

Pagina JSPServer Web

Page 41: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J041

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

<%@ DIRETTIVA {attributo=valore} %> Esempi:<%@ page language=java %>

<%@ page import=java.awt.*,java.util.* session=true %>

Direttive

Page 42: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J042

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

<%! DICHIARAZIONE %> Esempi:<%! String nome=pippo %>

<%! public String getName() {return nome} %>

Dichiarazioni

Page 43: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J043

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

<%= ESPRESSIONE%> Esempi:<%= getName() %>

<%@ page import=java.util.* %>Sono le <%= new Date().toString(); %>

Espressioni

Page 44: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J044

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

<html><body> <%! String nome=pippo %><%! public String getName() {return nome} %><H1>Buongiorno <%= getName() %></H1> </body></html>

Esempio

Page 45: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J045

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it <jsp:AZIONE>

Esempi:<jsp:forward page=URL>

<jsp:include page=URL flush=true>

<jsp:useBean>

Azioni

Page 46: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J046

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

<% SCRIPTLET %> Esempi:<% z=z+1; %><% // Get the Employee's Name from the request out.println("<b>Employee: </b>" + request.getParameter("employee")); // Get the Employee's Title from the request out.println("<br><b>Title: </b>" + request.getParameter("title")); %>

Scriptlet

Page 47: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J047

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

out Writerrequest HttpServletRequestresponse HttpServletResponsesession HttpSessionpage this nel Servletapplication servlet.getServletContext

area condivisa tra i servlet

config ServletConfigexception solo nella errorPagepageContext sorgente degli oggetti,

raramente usato

Oggetti predefiniti

Page 48: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J048

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

<%@ page errorPage="errorpage.jsp" %><html> <head> <title>UseRequest</title> </head> <body> <% // Get the User's Name from the request out.println("<b>Hello: " +

request.getParameter("user") + "</b>"); %> </body></html>

request

Page 49: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J049

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

<%@ page errorPage="errorpage.jsp" %>

<html> <head> <title>UseSession</title> </head> <body> <% // Try and get the current count from the session Integer count =

(Integer)session.getAttribute("COUNT"); // If COUNT is not found, create it and add it to the

session if ( count == null ) { count = new Integer(1); session.setAttribute("COUNT", count); }

session

Page 50: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J050

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it else {

count = new Integer(count.intValue() + 1); session.setAttribute("COUNT", count); } // Get the User's Name from the request out.println("<b>Hello you have visited this

site: " + count + " times.</b>"); %> </body></html>

session

Page 51: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J051

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it

include file=“URL”

taglib uri=“URL” prefix=“prefisso”definisce un meccanismo di estensione delle tag esistenti!

Direttive

Page 52: J0 1 Marco Ronchetti - ronchet@altavista.it Servlets e JSP.

J052

Marc

o R

onch

ett

i

-

ronch

et@

alt

avis

ta.it jsp:useBean

jsp:setProperty

jsp:getProperty

jsp:forward

jsp:plugin

Azioni