Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Marco Ronchetti - [email protected] “Basi di Dati Web e Distribuite” – Laurea Specialitica in Informatica – Università di Trento J0 1 JSP “Basi di Dati Web e Distribuite” – Laurea Specialitica in Informatica – Università di Trento Marco Ronchetti - [email protected] Simple.jsp J0 2 <html> <body> <% out.println(“Hello World”); %> </body> </html> “Basi di Dati Web e Distribuite” – Laurea Specialitica in Informatica – Università di Trento Marco Ronchetti - [email protected] JSP Lifecycle Server Web Pagina JSP Browser Servlet generato Servlet compilato J0 3 “Basi di Dati Web e Distribuite” – Laurea Specialitica in Informatica – Università di Trento Marco Ronchetti - [email protected] JSP nuts and bolts J0 4 Syntactic elements: <%@ directives %> <%! declarations %> <% scriptlets %> <%= expressions %> <jsp:actions/> Implicit Objects: •request •response •pageContext •session •application •out •config •page “Basi di Dati Web e Distribuite” – Laurea Specialitica in Informatica – Università di Trento Marco Ronchetti - [email protected] Scriptlets J0 5 A scriptlet is a block of Java code executed during the request-processing time. In Tomcat all the scriptlets gets put into the service() method of the servlet. They are therefoe processed for ecery request that the servlet receives. “Basi di Dati Web e Distribuite” – Laurea Specialitica in Informatica – Università di Trento Marco Ronchetti - [email protected] Scriptlet J0 6 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")); %> “Basi di Dati Web e Distribuite” – Laurea Specialitica in Informatica – Università di Trento Declarations Marco Ronchetti - [email protected] A declaration is a block of Java code used to: J0 7 define class-wide variables and methods in the generated servlet. They are initialized when the JSP page is initialized. “Basi di Dati Web e Distribuite” – Laurea Specialitica in Informatica – Università di Trento Dichiarazioni Marco Ronchetti - [email protected] <%! DICHIARAZIONE %> J0 8 Esempi: <%! String nome=“pippo”; %> <%! public String getName() {return nome;} %> “Basi di Dati Web e Distribuite” – Laurea Specialitica in Informatica – Università di Trento Directives Marco Ronchetti - [email protected] A directive is used as a message mechanism to: J0 9 pass information from the JSP code to the container Main directives: page include (for including other STATIC resources at compilation time) taglib (for including custom tag libraries) “Basi di Dati Web e Distribuite” – Laurea Specialitica in Informatica – Università di Trento Direttive Marco Ronchetti - [email protected] <%@ DIRETTIVA {attributo=valore} %> J0 10 main attributes: <%@ page language=java session=true %> <%@ page import=java.awt.*,java.util.* %> <%@ page isThreadSafe=false %> <%@ page errorPage=URL %> <%@ page isErrorPage=true %> “Basi di Dati Web e Distribuite” – Laurea Specialitica in Informatica – Università di Trento Marco Ronchetti - [email protected] Expressions J0 11 An expression is a shorthand notation that sends the evaluated Java expression back to the client (in the form of a String). Esempi: <%= getName() %> <%@ page import=java.util.* %> Sono le <%= new Date().toString(); %> “Basi di Dati Web e Distribuite” – Laurea Specialitica in Informatica – Università di Trento Marco Ronchetti - [email protected] Expressions J0 12 <html><body> <%! String nome=“pippo” %> <%! public String getName() {return nome;} %> <H1> Buongiorno <%= getName() %> </H1> </body></html> “Basi di Dati Web e Distribuite” – Laurea Specialitica in Informatica – Università di Trento Marco Ronchetti - [email protected] Standard actions Standard action are tags that affect the runtime behavior of the JSP and the response sent back to the client. <jsp:include page=“URL” /> For including STATIC or DYNAMIC resources at request time <jsp:forward page=“URL” /> J0 13 “Basi di Dati Web e Distribuite” – Laurea Specialitica in Informatica – Università di Trento Marco Ronchetti - [email protected] What is a Java bean? J0 14 A bean is a Java class that: • Has a void constructor • Has private instance variables with setter and getter methods public class SimpleBean { private int counter; SimpleBean() {counter=0;} int getCounter() {return counter;} void setCounter(int c) {counter=c;} } “Basi di Dati Web e Distribuite” – Laurea Specialitica in Informatica – Università di Trento Marco Ronchetti - [email protected] Standard actions involving beans J0 15 <jsp:useBean id=“name” class=“fully_qualified_pathname” scope=“{page|request|session|application}” /> <jsp:setProperty name=“nome” property=“value” /> <jsp:getProperty name=“nome” property=“value” /> “Basi di Dati Web e Distribuite” – Laurea Specialitica in Informatica – Università di Trento Marco Ronchetti - [email protected] Oggetti predefiniti J0 16 out request response session page application Writer HttpServletRequest HttpServletResponse HttpSession this nel Servlet servlet.getServletContext area condivisa tra i servlet config exception pageContext ServletConfig solo nella errorPage sorgente degli oggetti, raramente usato “Basi di Dati Web e Distribuite” – Laurea Specialitica in Informatica – Università di Trento Marco Ronchetti - [email protected] request J0 17 <%@ 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> “Basi di Dati Web e Distribuite” – Laurea Specialitica in Informatica – Università di Trento session Marco Ronchetti - [email protected] <%@ page errorPage="errorpage.jsp" %> J0 18 <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); } “Basi di Dati Web e Distribuite” – Laurea Specialitica in Informatica – Università di Trento Marco Ronchetti - [email protected] session J0 19 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> “Basi di Dati Web e Distribuite” – Laurea Specialitica in Informatica – Università di Trento Marco Ronchetti - [email protected] Common JSP patterns J0 20 Page-centric (client-server) CLIENT JSP or Servlet SERVER CLIENT Enterprise JavaBeans DB “Basi di Dati Web e Distribuite” – Laurea Specialitica in Informatica – Università di Trento Marco Ronchetti - [email protected] Common JSP patterns J0 21 Page-centric 1 (client-server) Page View request JSP response Business Processing “Basi di Dati Web e Distribuite” – Laurea Specialitica in Informatica – Università di Trento Marco Ronchetti - [email protected] Common JSP patterns J0 22 Page-centric 2 (client-server) Page View with Bean request JSP response Worker Bean Business Processing “Basi di Dati Web e Distribuite” – Laurea Specialitica in Informatica – Università di Trento Marco Ronchetti - [email protected] Common JSP patterns J0 23 Dispatcher (n-tier) Mediating JSP Mediator - View request response service Presentation JSP Worker bean service Presentation JSP Worker bean service Presentation JSP Business Processing