Your SlideShare is downloading. ×
0
Tutorial:  Server-Side Web Applications with Java, JSP and Tomcat Eran Toch December 2004 Methodologies in Information Sys...
Agenda <ul><li>Introduction and architecture </li></ul><ul><li>Installing Tomcat </li></ul><ul><li>Building a Web Applicat...
Web Application vs. Web Site Web Site Web Application
The Difference – cont’d <ul><li>Web sites are </li></ul><ul><ul><li>Static </li></ul></ul><ul><ul><li>File based </li></ul...
Java Web Application Architecture JSP Application Server Browser Servlet Java Classes / EJB HTTP / HTML DB / ERP /  Legacy
Agenda <ul><li>Introduction and architecture </li></ul><ul><li>Installing Tomcat </li></ul><ul><li>Building a Web Applicat...
Tomcat Installation <ul><li>Go to:  http://jakarta.apache.org/tomcat/ </li></ul><ul><li>Download the latest version (curre...
Tomcat and Java 5 <ul><li>Please note that Tomcat v. 5.5.4 requires J2SE 1.5 (also known as J2SE 5) or above.  </li></ul><...
Tomcat Installation – cont’d If you want Tomcat to startup every time the computer is rebooted, check the “service” option...
Tomcat Installation - Port You can configure the port that Tomcat will be using. If you want it to be available publicly (...
Running Tomcat <ul><li>Start Tomcat by running Tomcat monitor (Start Menu -> Apache Tomcat -> Monitor Tomcat), and click o...
Managing Tomcat <ul><li>Go to the management console by clicking on the “management” link in the Tomcat root homepage, or ...
Managing Tomcat Console Web Application management  Number of current sessions Start, stop, restart and un-deploy applicat...
Agenda <ul><li>Introduction and architecture </li></ul><ul><li>Installing Tomcat </li></ul><ul><li>Building a Web Applicat...
Building A Web Application <ul><li>Build the following folder structure under [TOMCAT-HOME]webapps </li></ul>Tomcat-home c...
Creating JSP Project in Eclipse <ul><li>Open a project for the JSP pages </li></ul><ul><li>Choose the type “Simple project...
Creating JSP Project – cont’d <ul><li>Choose the path to the jsp folder </li></ul><ul><li>Click “Finish” </li></ul>
Creating Java Classes Project <ul><li>Create a new project </li></ul><ul><li>Choose “Java Project” </li></ul>
Creating Java Project – cont’d <ul><li>Choose the path to the WEB-INF/classes folder </li></ul>
Creating Java Project – cont’d <ul><li>Make sure the output build path is in the WEB-INF classes folder </li></ul><ul><li>...
Better ways <ul><li>Download MyEclipse IDE extension from: http://www.myeclipseide.com/ </li></ul><ul><li>Download Lomboz ...
Agenda <ul><li>Introduction and architecture </li></ul><ul><li>Installing Tomcat </li></ul><ul><li>Building a Web Applicat...
Our “Phones” Application <ul><li>Simple application that displays phone records from the database </li></ul><ul><li>(stupi...
PhoneEntry.java package edu.technion.methodologies.phones ;   public class PhoneEntry  { private int id = 0 ; private Stri...
DBConnector.java <ul><li>See the advanced Java tutorial… </li></ul>
Writing Scriplets in JSP <ul><li>Write HTML as normal </li></ul><ul><li>Dynamic parts between <%  %>  </li></ul><ul><li>Eg...
JSP Scripting Elements <ul><li>Expressions   <%=  expression  %>   </li></ul><ul><ul><ul><li>evaluated and inserted into t...
JSP Directives <ul><li>There are two main types of directive:  </li></ul><ul><ul><li>page   </li></ul></ul><ul><ul><ul><li...
select_entry.jsp <ul><li><!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;> </li></ul><ul><li><HTML>...
select_entry.jsp – the HTML output
display_phone.jsp <!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;> <%@ page   import = &quot;edu.t...
display_phone.jsp – cont’d <ul><li><table  border = 0 > </li></ul><ul><ul><li><tr> </li></ul></ul><ul><ul><ul><li><td> Nam...
display_phone.jsp – the HTML Output
Agenda <ul><li>Introduction and architecture </li></ul><ul><li>Installing Tomcat </li></ul><ul><li>Building a Web Applicat...
JSP Include <ul><li>Inserting elements into the JSP page in complication time </li></ul><table  border = &quot;0&quot;   w...
JSP Include – cont’d … <BODY> <%@ include   file = &quot;header.jsp&quot; %> <% … We add the include declarative into disp...
Session and Application Objects <ul><li>A session is an object associated with a visitor.   </li></ul><ul><li>Data can be ...
Advanced Issues <ul><li>Servlets – have many advantages over JSP </li></ul><ul><ul><li>Inheritance </li></ul></ul><ul><ul>...
Agenda <ul><li>Introduction and architecture </li></ul><ul><li>Installing Tomcat </li></ul><ul><li>Building a Web Applicat...
Model 2 Architecture <ul><li>recommended approach for designing medium- and large-sized Web applications  </li></ul><ul><l...
Model 2 Architecture – cont’d .jsp .jsp Form request forward Database classes servlet VIEW CONTROLLER MODEL
Struts <ul><li>Struts is an open source framework created within the Jakarta project by the Apache Software Foundation </l...
References <ul><li>Core Servlets and JavaServer Pages, Vol. 1: Core Technologies, Second Edition, by Marty Hall, Larry Bro...
Upcoming SlideShare
Loading in...5
×

Ta Javaserverside Eran Toch

877

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
877
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Ta Javaserverside Eran Toch"

  1. 1. Tutorial: Server-Side Web Applications with Java, JSP and Tomcat Eran Toch December 2004 Methodologies in Information Systems Development
  2. 2. Agenda <ul><li>Introduction and architecture </li></ul><ul><li>Installing Tomcat </li></ul><ul><li>Building a Web Application (through Eclipse) </li></ul><ul><li>The Phones Web Application </li></ul><ul><ul><li>Writing JavaBeans </li></ul></ul><ul><ul><li>Writing JSPs </li></ul></ul><ul><li>Includes, session and application </li></ul><ul><li>Model 2 Architecture </li></ul>
  3. 3. Web Application vs. Web Site Web Site Web Application
  4. 4. The Difference – cont’d <ul><li>Web sites are </li></ul><ul><ul><li>Static </li></ul></ul><ul><ul><li>File based </li></ul></ul><ul><ul><li>May include client’s JavaScript code </li></ul></ul><ul><li>Web Applications are </li></ul><ul><ul><li>Dynamic (output depend on input) </li></ul></ul><ul><ul><li>Database based </li></ul></ul><ul><ul><li>Multi-layered </li></ul></ul><ul><ul><li>Session dependent (most of the times) </li></ul></ul><ul><ul><li>Personalized (some of the times) </li></ul></ul>
  5. 5. Java Web Application Architecture JSP Application Server Browser Servlet Java Classes / EJB HTTP / HTML DB / ERP / Legacy
  6. 6. Agenda <ul><li>Introduction and architecture </li></ul><ul><li>Installing Tomcat </li></ul><ul><li>Building a Web Application (through Eclipse) </li></ul><ul><li>The Phones Web Application </li></ul><ul><ul><li>Writing JavaBeans </li></ul></ul><ul><ul><li>Writing JSPs </li></ul></ul><ul><li>Includes, session and application </li></ul><ul><li>Model 2 Architecture </li></ul>
  7. 7. Tomcat Installation <ul><li>Go to: http://jakarta.apache.org/tomcat/ </li></ul><ul><li>Download the latest version (currently 5.5.4) by going to: Downloads -> Binaries -> Tomcat 5.5.4. </li></ul><ul><ul><li>For Windows, download the exe version – with a Windows setup. The direct link: http://apache.fresh.co.il/jakarta/tomcat-5/v5.5.4/bin/jakarta-tomcat-5.5.4.exe </li></ul></ul><ul><ul><li>For Linux, read the setup manual: http://jakarta.apache.org/tomcat/tomcat-5.5-doc/setup.html </li></ul></ul>
  8. 8. Tomcat and Java 5 <ul><li>Please note that Tomcat v. 5.5.4 requires J2SE 1.5 (also known as J2SE 5) or above. </li></ul><ul><li>If you want to use J2SE 1.4, download Tomcat 5.0.+ </li></ul>
  9. 9. Tomcat Installation – cont’d If you want Tomcat to startup every time the computer is rebooted, check the “service” option. If you wish to use Tomcat for only for development, it’s best to leave the service option unchecked.
  10. 10. Tomcat Installation - Port You can configure the port that Tomcat will be using. If you want it to be available publicly (behind firewalls etc), change the default port to 80. Otherwise, leave it as it is (880). You can always change it later, in the [TOMCAT_HOME]/conf/server.xml configuration file.
  11. 11. Running Tomcat <ul><li>Start Tomcat by running Tomcat monitor (Start Menu -> Apache Tomcat -> Monitor Tomcat), and click on Start. </li></ul><ul><ul><li>Point your browser to: http://localhost:8080 . If Tomcat works, you should see something like this </li></ul></ul><ul><ul><li>If not, check out the logs (C:[TOMCAT_HOME]logsstdout) and see what went wrong. </li></ul></ul>
  12. 12. Managing Tomcat <ul><li>Go to the management console by clicking on the “management” link in the Tomcat root homepage, or directly by going to: http :// localhost : 8080 / manager / html . </li></ul><ul><li>The default username is “admin”, and the default password is ”” </li></ul><ul><li>You can change it by changing the [TOMCAT_HOME]conf omcat-users.xml. </li></ul>
  13. 13. Managing Tomcat Console Web Application management Number of current sessions Start, stop, restart and un-deploy applications
  14. 14. Agenda <ul><li>Introduction and architecture </li></ul><ul><li>Installing Tomcat </li></ul><ul><li>Building a Web Application (through Eclipse) </li></ul><ul><li>The Phones Web Application </li></ul><ul><ul><li>Writing JavaBeans </li></ul></ul><ul><ul><li>Writing JSPs </li></ul></ul><ul><li>Includes, session and application </li></ul><ul><li>Model 2 Architecture </li></ul>
  15. 15. Building A Web Application <ul><li>Build the following folder structure under [TOMCAT-HOME]webapps </li></ul>Tomcat-home conf common webapps ... phones WEB-INF lib classes jsp This is the hard way… There are better
  16. 16. Creating JSP Project in Eclipse <ul><li>Open a project for the JSP pages </li></ul><ul><li>Choose the type “Simple project” </li></ul>
  17. 17. Creating JSP Project – cont’d <ul><li>Choose the path to the jsp folder </li></ul><ul><li>Click “Finish” </li></ul>
  18. 18. Creating Java Classes Project <ul><li>Create a new project </li></ul><ul><li>Choose “Java Project” </li></ul>
  19. 19. Creating Java Project – cont’d <ul><li>Choose the path to the WEB-INF/classes folder </li></ul>
  20. 20. Creating Java Project – cont’d <ul><li>Make sure the output build path is in the WEB-INF classes folder </li></ul><ul><li>Necessary libraries could be added now </li></ul>
  21. 21. Better ways <ul><li>Download MyEclipse IDE extension from: http://www.myeclipseide.com/ </li></ul><ul><li>Download Lomboz plugin for Eclipse at: http://www.objectlearn.com/index.jsp </li></ul><ul><li>Both of these plugins will give you: </li></ul><ul><ul><li>Easier deployment of projects </li></ul></ul><ul><ul><li>JSP debugging </li></ul></ul><ul><ul><li>JSP error detection (important!) </li></ul></ul>
  22. 22. Agenda <ul><li>Introduction and architecture </li></ul><ul><li>Installing Tomcat </li></ul><ul><li>Building a Web Application (through Eclipse) </li></ul><ul><li>The Phones Web Application </li></ul><ul><ul><li>Writing JavaBeans </li></ul></ul><ul><ul><li>Writing JSPs </li></ul></ul><ul><li>Includes, session and application </li></ul><ul><li>Model 2 Architecture </li></ul>
  23. 23. Our “Phones” Application <ul><li>Simple application that displays phone records from the database </li></ul><ul><li>(stupid, shameless) Architecture: </li></ul>select_entry.jsp display_phone.jsp PhoneEntry.java DBConnector.java Database
  24. 24. PhoneEntry.java package edu.technion.methodologies.phones ;   public class PhoneEntry { private int id = 0 ; private String name ; private String phone ; private String email ; public void load(int id ) { DBConnector.loadPhone(id, this ); } public void save () { if (id == 0 ) { id = DBConnector.insertPhone(this ); } else { DBConnector.updatePhone(id, this ); } } //getters and setters… }
  25. 25. DBConnector.java <ul><li>See the advanced Java tutorial… </li></ul>
  26. 26. Writing Scriplets in JSP <ul><li>Write HTML as normal </li></ul><ul><li>Dynamic parts between <% %> </li></ul><ul><li>Eg </li></ul><ul><li><HTML> </li></ul><ul><li><BODY> Hello!  The time is now <%= new java.util.Date() %> </li></ul><ul><li></BODY> </li></ul><ul><li></HTML> </li></ul>
  27. 27. JSP Scripting Elements <ul><li>Expressions <%=  expression  %> </li></ul><ul><ul><ul><li>evaluated and inserted into the output </li></ul></ul></ul><ul><li>Scriptlets <%  code  %> </li></ul><ul><ul><ul><li>inserted into the servlet's service method </li></ul></ul></ul><ul><li>Declarations <%!  code  %> </li></ul><ul><ul><ul><li>inserted into the body of the servlet class, outside of any existing methods </li></ul></ul></ul>
  28. 28. JSP Directives <ul><li>There are two main types of directive: </li></ul><ul><ul><li>page </li></ul></ul><ul><ul><ul><li>lets you do things like import classes, customize the servlet superclass etc </li></ul></ul></ul><ul><ul><li>Include </li></ul></ul><ul><ul><ul><li>lets you insert a file into the servlet class at the time the JSP file is translated into a servlet. </li></ul></ul></ul><ul><li><%@ directive attribute=&quot;value&quot; %> </li></ul>
  29. 29. select_entry.jsp <ul><li><!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;> </li></ul><ul><li><HTML> </li></ul><ul><li><HEAD> </li></ul><ul><li><TITLE> Phones Application </TITLE> </li></ul><ul><li></HEAD> </li></ul><ul><li><BODY> </li></ul><ul><li><form action = &quot;display_phone.jsp&quot; method = &quot;post&quot; > </li></ul><ul><li><table border = 0 > </li></ul><ul><ul><li><tr> </li></ul></ul><ul><ul><ul><li><td> ID: </td> </li></ul></ul></ul><ul><ul><ul><li><td><input type = &quot;text&quot; name = &quot;id&quot; ></td> </li></ul></ul></ul><ul><ul><li></tr> </li></ul></ul><ul><ul><li><tr> </li></ul></ul><ul><ul><ul><li><td> &nbsp; </td> </li></ul></ul></ul><ul><ul><ul><li><td><input type = &quot;submit&quot; ></td> </li></ul></ul></ul><ul><ul><li></tr> </li></ul></ul><ul><li></table> </li></ul><ul><li></form> </li></ul><ul><li></BODY> </li></ul><ul><li></HTML> </li></ul>HTML Header HTML form Nested HTML table structure HTML text input
  30. 30. select_entry.jsp – the HTML output
  31. 31. display_phone.jsp <!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;> <%@ page import = &quot;edu.technion.methodologies.phones.*&quot; %> <HTML> <HEAD> <TITLE> Phone Entry </TITLE> </HEAD> <BODY> <% String idParam = request.getParameter( &quot;id&quot; ); int id = 0; if (idParam != null ){ id = Integer.parseInt(idParam); } PhoneEntry phoneEntry = new PhoneEntry(); phoneEntry.load(id); %> Handling the request Creating the JavaBean Importing libraries
  32. 32. display_phone.jsp – cont’d <ul><li><table border = 0 > </li></ul><ul><ul><li><tr> </li></ul></ul><ul><ul><ul><li><td> Name: </td> </li></ul></ul></ul><ul><ul><ul><li><td><b> <%= phoneEntry.getName() %> </b></td> </li></ul></ul></ul><ul><ul><li></tr> </li></ul></ul><ul><ul><li><tr> </li></ul></ul><ul><ul><ul><li><td> Phone: </td> </li></ul></ul></ul><ul><ul><ul><li><td> <%= phoneEntry.getPhone() %> </td> </li></ul></ul></ul><ul><ul><li></tr> </li></ul></ul><ul><ul><li><tr> </li></ul></ul><ul><ul><ul><li><td> Email: </td> </li></ul></ul></ul><ul><ul><ul><li><td> <%= phoneEntry.getEmail() %> </td> </li></ul></ul></ul><ul><ul><li></tr> </li></ul></ul><ul><li></table> </li></ul><ul><li></BODY> </li></ul><ul><li></HTML> </li></ul>
  33. 33. display_phone.jsp – the HTML Output
  34. 34. Agenda <ul><li>Introduction and architecture </li></ul><ul><li>Installing Tomcat </li></ul><ul><li>Building a Web Application (through Eclipse) </li></ul><ul><li>The Phones Web Application </li></ul><ul><ul><li>Writing JavaBeans </li></ul></ul><ul><ul><li>Writing JSPs </li></ul></ul><ul><li>Includes, session and application </li></ul><ul><li>Model 2 Architecture </li></ul>
  35. 35. JSP Include <ul><li>Inserting elements into the JSP page in complication time </li></ul><table border = &quot;0&quot; width = &quot;100%&quot; cellpadding = &quot;4&quot; > <tr bgcolor = &quot;99CCFF&quot; > <td><a href = &quot;select_entry.jsp&quot; > home </a></td> </tr> </table> Included JSP file
  36. 36. JSP Include – cont’d … <BODY> <%@ include file = &quot;header.jsp&quot; %> <% … We add the include declarative into display_phone.jsp And we get:
  37. 37. Session and Application Objects <ul><li>A session is an object associated with a visitor.  </li></ul><ul><li>Data can be put in the session and retrieved from it </li></ul><ul><li>Sessions are timed-out – the default is ~25 minutes </li></ul><ul><li>The application object is accessed similarly, but is unique and global for the web application </li></ul>//Setting an object within the session session.setAttribute( &quot;last_searched_id&quot; , new Integer(id)); //Getting an object from the session Integer lastID = (Integer)session.getAttribute( &quot;last_searched_id&quot; ); out.println(lastID);
  38. 38. Advanced Issues <ul><li>Servlets – have many advantages over JSP </li></ul><ul><ul><li>Inheritance </li></ul></ul><ul><ul><li>Class structure </li></ul></ul><ul><li>Tag libraries </li></ul><ul><li>Enterprise Java Beans </li></ul><ul><li>J2EE Frameworks (WebLogic, Web Sphere, JBoss) </li></ul>
  39. 39. Agenda <ul><li>Introduction and architecture </li></ul><ul><li>Installing Tomcat </li></ul><ul><li>Building a Web Application (through Eclipse) </li></ul><ul><li>The Phones Web Application </li></ul><ul><ul><li>Writing JavaBeans </li></ul></ul><ul><ul><li>Writing JSPs </li></ul></ul><ul><li>Includes, session and application </li></ul><ul><li>Model 2 Architecture </li></ul>
  40. 40. Model 2 Architecture <ul><li>recommended approach for designing medium- and large-sized Web applications </li></ul><ul><li>An implementation of the Model-View-Controller (MVC) pattern </li></ul><ul><li>Applications have 3 layers </li></ul><ul><ul><li>Model : containing all data and operations </li></ul></ul><ul><ul><ul><ul><ul><li>JSP classes or beans </li></ul></ul></ul></ul></ul><ul><ul><li>View : creating various presentations </li></ul></ul><ul><ul><ul><ul><ul><li>JSP – focus on static data </li></ul></ul></ul></ul></ul><ul><ul><li>Controller : receiving requests, updating the model, and delegating to views </li></ul></ul><ul><ul><ul><ul><ul><li>servlet </li></ul></ul></ul></ul></ul>
  41. 41. Model 2 Architecture – cont’d .jsp .jsp Form request forward Database classes servlet VIEW CONTROLLER MODEL
  42. 42. Struts <ul><li>Struts is an open source framework created within the Jakarta project by the Apache Software Foundation </li></ul><ul><li>Supports Model-View-Controller (MVC) design pattern </li></ul><ul><li>http://struts.apache.org/ </li></ul>
  43. 43. References <ul><li>Core Servlets and JavaServer Pages, Vol. 1: Core Technologies, Second Edition, by Marty Hall, Larry Brown </li></ul><ul><li>Sun’s JSP Tutorial: http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/JSPIntro.html </li></ul><ul><li>JavaBeans Tutorial: http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/JSPBeans.html </li></ul><ul><li>A set of advanced (and untidy) tutorials: http://www.coreservlets.com/ </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×