Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tutorial: Server-Side Web Applications with Java, JSP and Tomcat Eran Toch December 2004 Methodologies in Information Systems Development.

Similar presentations


Presentation on theme: "Tutorial: Server-Side Web Applications with Java, JSP and Tomcat Eran Toch December 2004 Methodologies in Information Systems Development."— Presentation transcript:

1 Tutorial: Server-Side Web Applications with Java, JSP and Tomcat Eran Toch December 2004 Methodologies in Information Systems Development

2 2 Server-Side Web Applications with Java, JSP and Tomcat – Eran Toch Methodologies in Information System Development Agenda Introduction and architecture Installing Tomcat Building a Web Application (through Eclipse) The Phones Web Application –Writing JavaBeans –Writing JSPs Includes, session and application Model 2 Architecture

3 3 Server-Side Web Applications with Java, JSP and Tomcat – Eran Toch Methodologies in Information System Development Web Application vs. Web Site Web SiteWeb Application

4 4 Server-Side Web Applications with Java, JSP and Tomcat – Eran Toch Methodologies in Information System Development The Difference – cont’d Web sites are –Static –File based –May include client’s JavaScript code Web Applications are –Dynamic (output depend on input) –Database based –Multi-layered –Session dependent (most of the times) –Personalized (some of the times)

5 5 Server-Side Web Applications with Java, JSP and Tomcat – Eran Toch Methodologies in Information System Development JSP Java Web Application Architecture Application Server Browser Servlet Java Classes / EJB HTTP / HTML DB / ERP / Legacy

6 6 Server-Side Web Applications with Java, JSP and Tomcat – Eran Toch Methodologies in Information System Development Agenda Introduction and architecture Installing Tomcat Building a Web Application (through Eclipse) The Phones Web Application –Writing JavaBeans –Writing JSPs Includes, session and application Model 2 Architecture

7 7 Server-Side Web Applications with Java, JSP and Tomcat – Eran Toch Methodologies in Information System Development Tomcat Installation Go to: http://jakarta.apache.org/tomcat/http://jakarta.apache.org/tomcat/ Download the latest version (currently 5.5.4) by going to: Downloads -> Binaries -> Tomcat 5.5.4. –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 http://apache.fresh.co.il/jakarta/tomcat- 5/v5.5.4/bin/jakarta-tomcat-5.5.4.exe –For Linux, read the setup manual: http://jakarta.apache.org/tomcat/tomcat-5.5- doc/setup.html

8 8 Server-Side Web Applications with Java, JSP and Tomcat – Eran Toch Methodologies in Information System Development Tomcat and Java 5 Please note that Tomcat v. 5.5.4 requires J2SE 1.5 (also known as J2SE 5) or above. If you want to use J2SE 1.4, download Tomcat 5.0.+

9 9 Server-Side Web Applications with Java, JSP and Tomcat – Eran Toch Methodologies in Information System Development 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 Server-Side Web Applications with Java, JSP and Tomcat – Eran Toch Methodologies in Information System Development 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/serv er.xml configuration file.

11 11 Server-Side Web Applications with Java, JSP and Tomcat – Eran Toch Methodologies in Information System Development Running Tomcat Start Tomcat by running Tomcat monitor (Start Menu -> Apache Tomcat -> Monitor Tomcat), and click on Start. –Point your browser to: http://localhost:8080. If Tomcat works, you should see something like this http://localhost:8080 –If not, check out the logs (C:\[TOMCAT_HOME]\logs\s tdout) and see what went wrong.

12 12 Server-Side Web Applications with Java, JSP and Tomcat – Eran Toch Methodologies in Information System Development Managing Tomcat 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. http://localhost:8080/manager/html The default username is “admin”, and the default password is ”” You can change it by changing the [TOMCAT_HOME]\conf\tomcat-users.xml.

13 13 Server-Side Web Applications with Java, JSP and Tomcat – Eran Toch Methodologies in Information System Development Managing Tomcat Console Web Application management Number of current sessions Start, stop, restart and un-deploy applications

14 14 Server-Side Web Applications with Java, JSP and Tomcat – Eran Toch Methodologies in Information System Development Agenda Introduction and architecture Installing Tomcat Building a Web Application (through Eclipse) The Phones Web Application –Writing JavaBeans –Writing JSPs Includes, session and application Model 2 Architecture

15 15 Server-Side Web Applications with Java, JSP and Tomcat – Eran Toch Methodologies in Information System Development Building A Web Application Build the following folder structure under [TOMCAT-HOME]\webapps Tomcat-home confcommonwebapps... phones WEB-INF lib classes jsp This is the hard way… There are better

16 16 Server-Side Web Applications with Java, JSP and Tomcat – Eran Toch Methodologies in Information System Development Creating JSP Project in Eclipse 1.Open a project for the JSP pages 2.Choose the type “Simple project”

17 17 Server-Side Web Applications with Java, JSP and Tomcat – Eran Toch Methodologies in Information System Development Creating JSP Project – cont’d Choose the path to the jsp folder Click “Finish”

18 18 Server-Side Web Applications with Java, JSP and Tomcat – Eran Toch Methodologies in Information System Development Creating Java Classes Project Create a new project Choose “Java Project”

19 19 Server-Side Web Applications with Java, JSP and Tomcat – Eran Toch Methodologies in Information System Development Creating Java Project – cont’d Choose the path to the WEB- INF/classes folder

20 20 Server-Side Web Applications with Java, JSP and Tomcat – Eran Toch Methodologies in Information System Development Creating Java Project – cont’d Make sure the output build path is in the WEB-INF classes folder Necessary libraries could be added now

21 21 Server-Side Web Applications with Java, JSP and Tomcat – Eran Toch Methodologies in Information System Development Better ways Download MyEclipse IDE extension from: http://www.myeclipseide.com/ http://www.myeclipseide.com/ Download Lomboz plugin for Eclipse at: http://www.objectlearn.com/index.jsp http://www.objectlearn.com/index.jsp Both of these plugins will give you: –Easier deployment of projects –JSP debugging –JSP error detection (important!)

22 22 Server-Side Web Applications with Java, JSP and Tomcat – Eran Toch Methodologies in Information System Development Agenda Introduction and architecture Installing Tomcat Building a Web Application (through Eclipse) The Phones Web Application –Writing JavaBeans –Writing JSPs Includes, session and application Model 2 Architecture

23 23 Server-Side Web Applications with Java, JSP and Tomcat – Eran Toch Methodologies in Information System Development Our “Phones” Application Simple application that displays phone records from the database (stupid, shameless) Architecture: select_entry.jspdisplay_phone.jsp PhoneEntry.java DBConnector.java Database

24 24 Server-Side Web Applications with Java, JSP and Tomcat – Eran Toch Methodologies in Information System DevelopmentPhoneEntry.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 Server-Side Web Applications with Java, JSP and Tomcat – Eran Toch Methodologies in Information System Development DBConnector.java See the advanced Java tutorial…

26 26 Server-Side Web Applications with Java, JSP and Tomcat – Eran Toch Methodologies in Information System Development Writing Scriplets in JSP Write HTML as normal Dynamic parts between Eg Hello! The time is now

27 27 Server-Side Web Applications with Java, JSP and Tomcat – Eran Toch Methodologies in Information System Development JSP Scripting Elements Expressions evaluated and inserted into the output Scriptlets inserted into the servlet's service method Declarations inserted into the body of the servlet class, outside of any existing methods

28 28 Server-Side Web Applications with Java, JSP and Tomcat – Eran Toch Methodologies in Information System Development JSP Directives There are two main types of directive: –page lets you do things like import classes, customize the servlet superclass etc –Include lets you insert a file into the servlet class at the time the JSP file is translated into a servlet.

29 29 Server-Side Web Applications with Java, JSP and Tomcat – Eran Toch Methodologies in Information System Development select_entry.jsp Phones Application ID: HTML Header HTML form Nested HTML table structure HTML text input

30 30 Server-Side Web Applications with Java, JSP and Tomcat – Eran Toch Methodologies in Information System Development select_entry.jsp – the HTML output

31 31 Server-Side Web Applications with Java, JSP and Tomcat – Eran Toch Methodologies in Information System Development display_phone.jsp Phone Entry <% String idParam = request.getParameter("id"); 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 Server-Side Web Applications with Java, JSP and Tomcat – Eran Toch Methodologies in Information System Development display_phone.jsp – cont’d Name: Phone: Email:

33 33 Server-Side Web Applications with Java, JSP and Tomcat – Eran Toch Methodologies in Information System Development display_phone.jsp – the HTML Output

34 34 Server-Side Web Applications with Java, JSP and Tomcat – Eran Toch Methodologies in Information System Development Agenda Introduction and architecture Installing Tomcat Building a Web Application (through Eclipse) The Phones Web Application –Writing JavaBeans –Writing JSPs Includes, session and application Model 2 Architecture

35 35 Server-Side Web Applications with Java, JSP and Tomcat – Eran Toch Methodologies in Information System Development JSP Include Inserting elements into the JSP page in complication time home Included JSP file

36 36 Server-Side Web Applications with Java, JSP and Tomcat – Eran Toch Methodologies in Information System Development JSP Include – cont’d … <% … We add the include declarative into display_phone.jsp And we get:

37 37 Server-Side Web Applications with Java, JSP and Tomcat – Eran Toch Methodologies in Information System Development Session and Application Objects A session is an object associated with a visitor. Data can be put in the session and retrieved from it Sessions are timed-out – the default is ~25 minutes The application object is accessed similarly, but is unique and global for the web application //Setting an object within the session session.setAttribute("last_searched_id", new Integer(id)); //Getting an object from the session Integer lastID = (Integer)session.getAttribute("last_searched_id"); out.println(lastID);

38 38 Server-Side Web Applications with Java, JSP and Tomcat – Eran Toch Methodologies in Information System Development Advanced Issues Servlets – have many advantages over JSP –Inheritance –Class structure Tag libraries Enterprise Java Beans J2EE Frameworks (WebLogic, Web Sphere, JBoss)

39 39 Server-Side Web Applications with Java, JSP and Tomcat – Eran Toch Methodologies in Information System Development Agenda Introduction and architecture Installing Tomcat Building a Web Application (through Eclipse) The Phones Web Application –Writing JavaBeans –Writing JSPs Includes, session and application Model 2 Architecture

40 40 Server-Side Web Applications with Java, JSP and Tomcat – Eran Toch Methodologies in Information System Development Model 2 Architecture recommended approach for designing medium- and large-sized Web applications An implementation of the Model-View- Controller (MVC) pattern Applications have 3 layers –Model: containing all data and operations »JSP classes or beans –View: creating various presentations »JSP – focus on static data –Controller: receiving requests, updating the model, and delegating to views »servlet

41 41 Server-Side Web Applications with Java, JSP and Tomcat – Eran Toch Methodologies in Information System Development Model 2 Architecture – cont’d.jsp Form request forward Database classes servlet VIEW CONTROLLER MODEL

42 42 Server-Side Web Applications with Java, JSP and Tomcat – Eran Toch Methodologies in Information System Development Struts Struts is an open source framework created within the Jakarta project by the Apache Software Foundation Supports Model-View-Controller (MVC) design pattern http://struts.apache.org/

43 43 Server-Side Web Applications with Java, JSP and Tomcat – Eran Toch Methodologies in Information System Development References Core Servlets and JavaServer Pages, Vol. 1: Core Technologies, Second Edition, by Marty Hall, Larry Brown Sun’s JSP Tutorial: http://java.sun.com/j2ee/tutorial/1_3- fcs/doc/JSPIntro.html http://java.sun.com/j2ee/tutorial/1_3- fcs/doc/JSPIntro.html JavaBeans Tutorial: http://java.sun.com/j2ee/tutorial/1_3- fcs/doc/JSPBeans.html http://java.sun.com/j2ee/tutorial/1_3- fcs/doc/JSPBeans.html A set of advanced (and untidy) tutorials: http://www.coreservlets.com/ http://www.coreservlets.com/


Download ppt "Tutorial: Server-Side Web Applications with Java, JSP and Tomcat Eran Toch December 2004 Methodologies in Information Systems Development."

Similar presentations


Ads by Google