Download
building web applications with java n.
Skip this Video
Loading SlideShow in 5 Seconds..
Building Web Applications with Java PowerPoint Presentation
Download Presentation
Building Web Applications with Java

Building Web Applications with Java

174 Vues Download Presentation
Télécharger la présentation

Building Web Applications with Java

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Building Web Applications with Java EPFL - Ecole Polytechnique Fédérale de Lausanne (Swiss Federal Institute of Technology) Claude Petitpierre, Olivier Buchwalder, Paul-Louis Meylan

  2. Web Applications

  3. J2EE php Websphere Weblogic CVS Visual Age .net ant RMI Eclipse JDO Hybernate Java MySQL Javascript HTML Entity Bean Servlet Cookie JSP JBoss MDBean Tomcat taglib XML Struts

  4. Web server (J2EE, JBoss) • Servlets, Struts • objects to access databases • Development environment • wizards • diagrams • WebLang (language developed at the EPFL) • Software engineering • geography (collaboration diagrams) • behavior (use cases, finite state machine) • data (class diagrams)

  5. Precision This course is about the application architecture, not the formatting of the pages, for which there are many specialized editors!

  6. Content Servlets 7 Javascript 118 JSP 23 Struts CMP beans 36 structure (MVC) 124 Relationships Java beans 130 DB – object views 54 JSP (in-out) 138 operations 67 FSM 146 Finders 77 Data transfers 151 Transactions 90 Hibernate 3 155 Java client 98 SQL beans 160 JMS 99 RMI modules 168 Architecture 110 Software Engineering 171

  7. Calling an HTML Page Browser (client) Tomcat, Apache (server) HTML: Test.html <A HREF="URL">link</a> Internet

  8. Servlet handle data entered in such fields Submit button

  9. Calling a Servlet Browser JBoss Server servlet: Test.java HTML page <A HREF="URL">link</a> <FORM ACTION="URL"> <INPUT TYPE = text NAME="field"> </FORM> Internet HTML page

  10. Javadoc J2EE Structure of a Servlet public class Test extends HttpServlet { public void doGet ( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { response.setContentType("text/html"); out.println ("<html><body>"); out.println ("<h1>Title</h1></body>"); } } Produces the html page automatically when the method returns.

  11. Retrieval of the parameter value within the servlet(see J2EE Javadoc) <INPUT type = text name = "txtInp"> in the browser . . . yyy http://www.epfl.ch/servletName?txtInp=yyy String valueParam; in the servlet valueParam = request.getParameter("txtInp");

  12. Loading a Servlet into Tomcat-JBoss WEB-INF/jboss-web.xml WEB-INF/web.xml WEB-INF/classes/packageName/MyServlet.class jar cf Test-WEB.war WEB-INF In order to load the servlet in the server, one must transfer the Test-WEB.war into the deploy folder of Tomcat or JBoss

  13. Content of web.xml <servlet> <servlet-name>MyServletServlet</servlet-name> <display-name>MyServlet</display-name> <description><![CDATA[A simple Servlet]]></description> <servlet-class>serv.MyServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>MyServletServlet</servlet-name> <url-pattern>/MyServlet</url-pattern> </servlet-mapping>

  14. Read by xDoclets to create web.xml import javax.rmi.PortableRemoteObject; . . . /** * @web.servlet * name = "MyServletServlet" * display-name = "TestServlet" * description = "A simple Servlet" * @web.servlet-mapping * url-pattern = "/MyServlet" */ public class TestServlet extends HttpServlet { public void doGet (HttpServletRequest request, . . .

  15. How to create all that stuff ? • WebSphere, JBossIDE, WebLogic, JBuilderbased on wizards • Optimal-J, Arc Stylerbased on diagrams (MDA) • WebLangour simple programming language

  16. What should a generator produce ?Here is our choice: html: InputForms <FORM action= "test"> a b m1 c b m2 servlet: Test.java doGet() { . . . } m1(int a, String b) { ... } m2(String c, long d) { ... }

  17. With more details html: InputForms <FORM action= "test"> <INPUT ... name="a"> <INPUT ... name="b"> <SUBMIT name="m1"> </FORM> <FORM action= "test"> <INPUT ... name="c"> <INPUT ... name="b"> <SUBMIT name="m2"> </FORM> servlet: Test.java doGet() { a = getParameters(“a”); m1(a, b); or m2(c, d); } m1(int a, String b) { ... } m2(String c, long d) { ... }

  18. determines the HTML form Servlet Module in WebLang servlet TestServlet { package packageName; int attribute; // delicate public void add( PrintWriter out, int number) { attribute += number; out.println(attribute); } public void sub(int number) { attribute = number; } }

  19. compilation web.xml file.war calls the servlet C://jboss-4.0.0/server/standard/deploy Output of the WebLang Compiler Test.la • MyServlet.java (with the xdoclets (@web...) • xdoclet-build.xml • packaging-build.xml • README/MyServlet.html

  20. Exercise 1 • Create a servlet that returns the current dateClient Server html: showDate servlet: showDate show() ; Automatically created by WebLang Specify in a WebLang module

  21. Session Objectsin servlets Client 2 Client 2 Serveur action form 1 Client 1 (cookie) action form 1 Servlet y = session.getAttribute("xx"); action form 3 action form 4 xx action form 1 Client 2 (cookie) action form 1 action form 3 action form 4 xx

  22. Session public public void doGet ( HttpServletRequest request, HttpServletResponse response) { HttpSession session = request.getSession(); MyForm myForm = new MyForm(); session.setAttribute("myForm", myForm ); session.getAttribute("someName"); . . .

  23. JSP: Java Server Page (WebLang generates JSP on their own and as Struts companion)

  24. html html Use of a JSP JSP compiler Java compiler Execution JSP code + html Servlet Java source Servlet Java code server browser http://www.ch/xxxx.jsp

  25. Example of a JSP page(details follow) <html> <head><title>File</title></head> <body> <h1>Example</h1> <%! declarations %> <% code Java %> Nouvelle valeur de l'attribut: <%= attribute %> <p> <form action="http://localhost:8080/Exercice3-WEB/File.jsp"> <input type="text" name="attribute"/> <input type="submit"/> </form> </body> </html>

  26. Page JSP <%! declarations %> <--! Methods, attributes: Attention, a single servlet object is shared by all clients -->

  27. Page JSP Value of the attribute: <%= attribute %> <%= myClass.text() %> <% // Java code out.println( "Text <p>" ); %>

  28. URL of a JSP <form action="http://localhost:8080/ Exercice3-WEB/File.jsp"> <input type="text" name="attribute"/> <input type="submit"/> </form>

  29. Exercise 2 • Create a JSP that display the date and has a link pointing to the same JSPClient Server jsp: jspDate <h1>…Date…</h1> <a href="This JSP"> Show date </a> html: jspDate See this link ! Automatically created by WebLang Specify in a JSP WebLang module

  30. JSP statement <%@ pageimport="packName.*" %>

  31. Applet <jsp:plugin type="applet" code="package.HorlogeApplet" width="370" height="50"> </jsp:plugin>

  32. Importing a tag definition: <%@ taglib uri = "pack/myTag.tld" prefix = "tag"%> use: <tag:myTag/>

  33. Corresponding class tag package pack; import java.io.IOException; /** * @jsp.tag * name = "myTag" * description = "A test class for tags" */ public class myTag extends TagSupport { public int doStartTag() { JspWriter out = pageContext.getOut(); out.println("I am a tag"); return 1; } }

  34. Description of a tag: MyTag.tld Created by the xDoclets ?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"> <taglib> <tlib-version>1.0</tlib-version> <jsp-version>1.2</jsp-version> <short-name>xdoclet-build</short-name> <tag> <name>myTag</name> <tag-class>pack.myTag</tag-class> <description><![CDATA[A test class for tags]]></description> </tag> </taglib>

  35. Where is the compiled JSP file ? C:\jboss-4.0.0\server\standard\work…

  36. Database access with the helpof persistent objects EJB: Enterprise Java Beans

  37. Client-server Application (J2EE) Server JBoss Container Container Client Session EJB Entity EJB Client proxies DB Container Network Entity EJB

  38. Web Application (J2EE) JBoss Server Container Container Session EJB Entity EJB DB Client Tomcat Browser Servlet proxies Container Entity EJB x.html Network

  39. CMP entity beans (Container Managed Persistency) BMP, Bean Managed Persistency will not be studied as there are better alternatives

  40. Files generatedby WebLangand by the xDoclets • src • /control/ejb • Game.java • GameCMP.java • /control/proxy • GameLocalHomeProxy.java • GameLocalObjectProxy.java • GameRemoteHomeProxy.java • GameRemoteObjectProxy.java • sources of the beans • interfaces of the proxies

  41. Parts of a CMP entity bean EJBContainer servlet other EJB XxxxLocalHomeProxy create findByPrimaryKey XxxxEntityBean Xxxx ejbCreate ejbPostCreate ejbFindByPrimaryKey ejbLoad ejbStore userMethods XxxxLocalObjectProxy userMethods

  42. 1 2 3 XxxxObjectProxy userMethods EJB XxxxObjectProxy userMethods EJB XxxxObjectProxy userMethods EJB Instantiation of a persistent object InitialContext lookup XxxxHomeProxy create findByPrimaryKey client servlet bean

  43. Lookup to get a home proxy TownLocalHomeProxy townLocalHomeProxy = null; Context context = new InitialContext(); Object ref = context.lookup("store_PersonLocal"); townLocalHomeProxy = (TownLocalHomeProxy) PortableRemoteObject.narrow ( ref, TownLocalHomeProxy.class ); // narrow = cast from object to TownLocalHomeProxy

  44. CMP entity bean: Attributes /** * A pair of getter / setter for each attribute * * @ejb.persistent-field * @ejb.interface-method * view-type = "both“ */ public abstract String getAtt() ; /** * @ejb.interface-method * view-type = "both“ */ public abstract void setAtt(String att) ;

  45. CMP entity bean (beginning) . . . /** * Attribute Pk is used as primary key by default. * @ejb.persistent-field */ public abstract java.lang.Long getPk(); public abstract void setPk(java.lang.Long pk); /** * Default remove method * @throws RemoveException * @ejb.remove-method */ public void ejbRemove() throws RemoveException { }

  46. Control files generated by the xDoclets • META-INF • ejb-jar.xml • jboss.xml • jbosscmp-jdbc.xml

  47. Class specifying a CMP entity bean(continuation) package store.ejb; import javax.ejb.EntityBean; import javax.rmi.PortableRemoteObject; /** * @ejb.bean * name = "Customer" * display-name = "CMP Entity Bean" * description = "Description of the CMP Entity Bean" * view-type = "both" * type = "CMP" * primkey-field = "pk" * jndi-name = "store_CustomerRemote"  * local-jndi-name = "store_CustomerLocal" * @ejb.pk * class ="java.lang.Long" */ public abstract class Customer implements EntityBean { EntityContext entityContext; . . .

  48. Specification of CMP entity beans inWebLang CMP bean: attributes methods finders / creators relations

  49. Definition of an object in WebLang cmpbean Town { package geo; String name; // DBattributes int numero; public void myMet (String s) { System.out.println(s); } // creators - finders }