Presentation is loading. Please wait.

Presentation is loading. Please wait.

J0 1 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialitica in Informatica – Università di Trento.

Similar presentations


Presentation on theme: "J0 1 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialitica in Informatica – Università di Trento."— Presentation transcript:

1 J0 1 Marco Ronchetti - ronchet@dit.unitn.it ronchet@dit.unitn.it Basi di Dati Web e Distribuite – Laurea Specialitica in Informatica – Università di Trento Servlets

2 J0 2 Marco Ronchetti - ronchet@dit.unitn.it ronchet@dit.unitn.it Basi di Dati Web e Distribuite – Laurea Specialitica in Informatica – Università di Trento 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 ).

3 J0 3 Marco Ronchetti - ronchet@dit.unitn.it ronchet@dit.unitn.it Basi di Dati Web e Distribuite – Laurea Specialitica in Informatica – Università di Trento 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.

4 J0 4 Marco Ronchetti - ronchet@dit.unitn.it ronchet@dit.unitn.it Basi di Dati Web e Distribuite – Laurea Specialitica in Informatica – Università di Trento 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()

5 J0 5 Marco Ronchetti - ronchet@dit.unitn.it ronchet@dit.unitn.it Basi di Dati Web e Distribuite – Laurea Specialitica in Informatica – Università di Trento Applet Lifecycle Whenever its needed, at lower priority init() stop() destroy() start() handleEvent() Multithreading! Actually, more threads are active behind the scenes. paint()

6 J0 6 Marco Ronchetti - ronchet@dit.unitn.it ronchet@dit.unitn.it Basi di Dati Web e Distribuite – Laurea Specialitica in Informatica – Università di Trento 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);

7 J0 7 Marco Ronchetti - ronchet@dit.unitn.it ronchet@dit.unitn.it Basi di Dati Web e Distribuite – Laurea Specialitica in Informatica – Università di Trento 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; }

8 J0 8 Marco Ronchetti - ronchet@dit.unitn.it ronchet@dit.unitn.it Basi di Dati Web e Distribuite – Laurea Specialitica in Informatica – Università di Trento Applets-Event handling 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; }

9 J0 9 Marco Ronchetti - ronchet@dit.unitn.it ronchet@dit.unitn.it Basi di Dati Web e Distribuite – Laurea Specialitica in Informatica – Università di Trento 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 http://java.sun.com/docs/books/tutorial/servlets/overview/index.html

10 J0 10 Marco Ronchetti - ronchet@dit.unitn.it ronchet@dit.unitn.it Basi di Dati Web e Distribuite – Laurea Specialitica in Informatica – Università di Trento 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.

11 J0 11 Marco Ronchetti - ronchet@dit.unitn.it ronchet@dit.unitn.it Basi di Dati Web e Distribuite – Laurea Specialitica in Informatica – Università di Trento Applets vs. Servlets AppletServlet Gira:ClientServer Ha un main:NO Estende:java.applet.Appletjavax.servlet.http. HttpServlet GraficaSINO Cuore:handleEvent()service()

12 J0 12 Marco Ronchetti - ronchet@dit.unitn.it ronchet@dit.unitn.it Basi di Dati Web e Distribuite – Laurea Specialitica in Informatica – Università di Trento 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

13 J0 13 Marco Ronchetti - ronchet@dit.unitn.it ronchet@dit.unitn.it Basi di Dati Web e Distribuite – Laurea Specialitica in Informatica – Università di Trento service() This code is part of the class HttpServlet protected void service (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Stringmethod = req.getMethod (); if (method.equals ("GET")) { longifModifiedSince; longlastModified; longnow; 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); }

14 J0 14 Marco Ronchetti - ronchet@dit.unitn.it ronchet@dit.unitn.it Basi di Dati Web e Distribuite – Laurea Specialitica in Informatica – Università di Trento service() } else if (method.equals ("HEAD")) { longlastModified; 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"); }

15 J0 15 Marco Ronchetti - ronchet@dit.unitn.it ronchet@dit.unitn.it Basi di Dati Web e Distribuite – Laurea Specialitica in Informatica – Università di Trento 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";

16 J0 16 Marco Ronchetti - ronchet@dit.unitn.it ronchet@dit.unitn.it 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 response.setContentType("text/html"); // then write the data of the response out = response.getWriter(); out.println(" "); out.println(title); out.println(" "); out.println(" " + title + " "); out.println(" This is output from SimpleServlet."); out.println(" "); out.close(); }

17 J0 17 Marco Ronchetti - ronchet@dit.unitn.it ronchet@dit.unitn.it Basi di Dati Web e Distribuite – Laurea Specialitica in Informatica – Università di Trento Esempio: ShowParameters 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 (" " +title+ " + " \n" + " " + title + " \n" + " \n" + " Parameter Name Parameter Value(s)");

18 J0 18 Marco Ronchetti - ronchet@dit.unitn.it ronchet@dit.unitn.it Basi di Dati Web e Distribuite – Laurea Specialitica in Informatica – Università di Trento Esempio: ShowParameters Enumeration paramNames = request.getParameterNames(); while(paramNames.hasMoreElements()) { String paramName = (String)paramNames.nextElement(); out.print(" " + paramName + "\n "); String[] paramValues = request.getParameterValues(paramName); if (paramValues.length == 1) { String paramValue = paramValues[0]; if (paramValue.length() == 0) out.println(" No Value "); else out.println(paramValue); } else { out.println(" "); for(int i=0; i " +paramValues[i]); } out.println(" "); } out.println(" \n "); }

19 J0 19 Marco Ronchetti - ronchet@dit.unitn.it ronchet@dit.unitn.it Basi di Dati Web e Distribuite – Laurea Specialitica in Informatica – Università di Trento Esempio: ShowParameters public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }

20 J0 20 Marco Ronchetti - ronchet@dit.unitn.it ronchet@dit.unitn.it Basi di Dati Web e Distribuite – Laurea Specialitica in Informatica – Università di Trento Esempio: ShowParameters A Sample FORM using POST A Sample FORM using POST Item Number: Quantity: Price Each: First Name: Last Name: Middle Initial: Shipping Address:

21 J0 21 Marco Ronchetti - ronchet@dit.unitn.it ronchet@dit.unitn.it Basi di Dati Web e Distribuite – Laurea Specialitica in Informatica – Università di Trento Esempio: ShowParameters Credit Card: Visa Master Card American Express Discover Java SmartCard Credit Card Number: Repeat Credit Card Number:


Download ppt "J0 1 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialitica in Informatica – Università di Trento."

Similar presentations


Ads by Google