V Corso Sodalia

Download Report

Transcript V Corso Sodalia

Marco Ronchetti - [email protected] 
“Basi di Dati Web e Distribuite” – Laurea Specialitica in Informatica – Università di Trento
J0
1
Servlets
“Basi di Dati Web e Distribuite” – Laurea Specialitica in Informatica – Università di Trento
Marco Ronchetti - [email protected] 
Applets
J0
2
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).
“Basi di Dati Web e Distribuite” – Laurea Specialitica in Informatica – Università di Trento
Marco Ronchetti - [email protected] 
Applets
J0
3
Every applet is implemented by creating a
subclass of the Applet class.
The hierarchy determines much of what an
applet can do and how.
“Basi di Dati Web e Distribuite” – Laurea Specialitica in Informatica – Università di Trento
Marco Ronchetti - [email protected] 
Applet Lifecycle
J0
4
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()
“Basi di Dati Web e Distribuite” – Laurea Specialitica in Informatica – Università di Trento
Marco Ronchetti - [email protected] 
Applet Lifecycle
init()
start()
handleEvent()
stop()
J0
5
Multithreading!
destroy()
Whenever it’s needed,
at lower priority
paint()
Actually, more threads are active
behind the scenes.
“Basi di Dati Web e Distribuite” – Laurea Specialitica in Informatica – Università di Trento
Marco Ronchetti - [email protected] 
handleEvent()
J0
6
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);
“Basi di Dati Web e Distribuite” – Laurea Specialitica in Informatica – Università di Trento
handleEvent()
Marco Ronchetti - [email protected] 
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:
case Event.GOT_FOCUS:
case Event.LOST_FOCUS:
}
return false;
}
J0
7
return action(evt, evt.arg);
return gotFocus(evt, evt.arg);
return lostFocus(evt, evt.arg);
“Basi di Dati Web e Distribuite” – Laurea Specialitica in Informatica – Università di Trento
Marco Ronchetti - [email protected] 
Applets-Event handling
J0
8
To 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;
}
“Basi di Dati Web e Distribuite” – Laurea Specialitica in Informatica – Università di Trento
Marco Ronchetti - [email protected] 
Servlets (JDK 1.2)
J0
9
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
“Basi di Dati Web e Distribuite” – Laurea Specialitica in Informatica – Università di Trento
Marco Ronchetti - [email protected] 
Other uses of servlets
J0
10
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.
“Basi di Dati Web e Distribuite” – Laurea Specialitica in Informatica – Università di Trento
Marco Ronchetti - [email protected] 
Applets vs. Servlets
J0
11
Applet
Servlet
Gira:
Client
Server
Ha un main:
NO
NO
Estende:
java.applet.Applet
Grafica
SI
javax.servlet.http.
HttpServlet
NO
Cuore:
handleEvent()
service()
“Basi di Dati Web e Distribuite” – Laurea Specialitica in Informatica – Università di Trento
Marco Ronchetti - [email protected] 
Servlet Lifecycle
J0
12
Chiamato solo la prima volta che la
Servlet viene caricato in memoria!
init()
doXXX()
service(HttpServletRequest r,
HttpServletResponse p)
doGet()
doPost()
Se la Servlet implements SingleThreadModel non
ci saranno esecuzioni simultanee di codice
destroy()
Solo quando serve scaricare dalla
memoria!
“Basi di Dati Web e Distribuite” – Laurea Specialitica in Informatica – Università di Trento
service()
Marco Ronchetti - [email protected] 
This code is part of the class HttpServlet
J0
13
protected void service (HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String
method = req.getMethod ();
if (method.equals ("GET")) {
long
ifModifiedSince; long
lastModified; long
now;
ifModifiedSince = req.getDateHeader ("If-Modified-Since");
lastModified = getLastModified (req);
maybeSetLastModified (resp, lastModified);
if (ifModifiedSince == -1 || lastModified == -1)
doGet (req, resp);
else {
now = System.currentTimeMillis ();
if (now < ifModifiedSince || ifModifiedSince < lastModified)
doGet (req, resp);
else
resp.sendError (HttpServletResponse.SC_NOT_MODIFIED);
}
“Basi di Dati Web e Distribuite” – Laurea Specialitica in Informatica – Università di Trento
service()
Marco Ronchetti - [email protected] 
} 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");
}
J0
14
}
“Basi di Dati Web e Distribuite” – Laurea Specialitica in Informatica – Università di Trento
Marco Ronchetti - [email protected] 
A taste of servlet programming-1
J0
15
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";
“Basi di Dati Web e Distribuite” – Laurea Specialitica in Informatica – Università di Trento
A taste of servlet programming-2
// set content type and other response header fields first
Marco Ronchetti - [email protected] 
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();
}
J0
16
}
“Basi di Dati Web e Distribuite” – Laurea Specialitica in Informatica – Università di Trento
Marco Ronchetti - [email protected] 
Esempio: ShowParameters
J0
17
package 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 ("<HTML><HEAD><TITLE>" +title+ “</TITLE></HEAD>" +
"<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)");
“Basi di Dati Web e Distribuite” – Laurea Specialitica in Informatica – Università di Trento
Marco Ronchetti - [email protected] 
Esempio: ShowParameters
J0
18
Enumeration 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>");
}
“Basi di Dati Web e Distribuite” – Laurea Specialitica in Informatica – Università di Trento
Marco Ronchetti - [email protected] 
Esempio: ShowParameters
J0
19
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
“Basi di Dati Web e Distribuite” – Laurea Specialitica in Informatica – Università di Trento
Marco Ronchetti - [email protected] 
Esempio: ShowParameters
J0
20
<!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>
“Basi di Dati Web e Distribuite” – Laurea Specialitica in Informatica – Università di Trento
Marco Ronchetti - [email protected] 
Esempio: ShowParameters
J0
21
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>