1.78k likes | 2.01k Views
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. Web Applications. J2EE. php. Websphere Weblogic. CVS. Visual Age. .net. ant. RMI. Eclipse. JDO.
E N D
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
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
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)
Precision This course is about the application architecture, not the formatting of the pages, for which there are many specialized editors!
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
Calling an HTML Page Browser (client) Tomcat, Apache (server) HTML: Test.html <A HREF="URL">link</a> Internet
Servlet handle data entered in such fields Submit button
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
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.
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");
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
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>
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, . . .
How to create all that stuff ? • WebSphere, JBossIDE, WebLogic, JBuilderbased on wizards • Optimal-J, Arc Stylerbased on diagrams (MDA) • WebLangour simple programming language
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) { ... }
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) { ... }
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; } }
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
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
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
Session public public void doGet ( HttpServletRequest request, HttpServletResponse response) { HttpSession session = request.getSession(); MyForm myForm = new MyForm(); session.setAttribute("myForm", myForm ); session.getAttribute("someName"); . . .
JSP: Java Server Page (WebLang generates JSP on their own and as Struts companion)
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
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>
Page JSP <%! declarations %> <--! Methods, attributes: Attention, a single servlet object is shared by all clients -->
Page JSP Value of the attribute: <%= attribute %> <%= myClass.text() %> <% // Java code out.println( "Text <p>" ); %>
URL of a JSP <form action="http://localhost:8080/ Exercice3-WEB/File.jsp"> <input type="text" name="attribute"/> <input type="submit"/> </form>
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
JSP statement <%@ pageimport="packName.*" %>
Applet <jsp:plugin type="applet" code="package.HorlogeApplet" width="370" height="50"> </jsp:plugin>
Importing a tag definition: <%@ taglib uri = "pack/myTag.tld" prefix = "tag"%> use: <tag:myTag/>
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; } }
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>
Where is the compiled JSP file ? C:\jboss-4.0.0\server\standard\work…
Database access with the helpof persistent objects EJB: Enterprise Java Beans
Client-server Application (J2EE) Server JBoss Container Container Client Session EJB Entity EJB Client proxies DB Container Network Entity EJB
Web Application (J2EE) JBoss Server Container Container Session EJB Entity EJB DB Client Tomcat Browser Servlet proxies Container Entity EJB x.html Network
CMP entity beans (Container Managed Persistency) BMP, Bean Managed Persistency will not be studied as there are better alternatives
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
Parts of a CMP entity bean EJBContainer servlet other EJB XxxxLocalHomeProxy create findByPrimaryKey XxxxEntityBean Xxxx ejbCreate ejbPostCreate ejbFindByPrimaryKey ejbLoad ejbStore userMethods XxxxLocalObjectProxy userMethods
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
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
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) ;
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 { }
Control files generated by the xDoclets • META-INF • ejb-jar.xml • jboss.xml • jbosscmp-jdbc.xml
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; . . .
Specification of CMP entity beans inWebLang CMP bean: attributes methods finders / creators relations
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 }