ppt - Sergio Barbosa Villas-Boas (sbVB) home page

36
1 www.sbVB.com.br Java Part 2: java for web Version 8.0 of March 6 th , 2009 • By Sergio Barbosa Villas-Boas • www.sbVB.com.br [email protected] Jorge Lopes de Souza Leão • www.gta.ufrj.br/~leao [email protected] duke

description

 

Transcript of ppt - Sergio Barbosa Villas-Boas (sbVB) home page

Page 1: ppt - Sergio Barbosa Villas-Boas (sbVB) home page

1

www.sbVB.com.br

Java

• Part 2: java for web• Version 8.0 of March 6th, 2009• By

– Sergio Barbosa Villas-Boas• www.sbVB.com.br• [email protected]

– Jorge Lopes de Souza Leão• www.gta.ufrj.br/~leao• [email protected]

duke

Page 2: ppt - Sergio Barbosa Villas-Boas (sbVB) home page

2

www.sbVB.com.br

Network programming

socket

Page 3: ppt - Sergio Barbosa Villas-Boas (sbVB) home page

3

www.sbVB.com.br

Basics

• TCP• UDP• Connection

– server(IP,Port)– client(IP,Port)

Page 4: ppt - Sergio Barbosa Villas-Boas (sbVB) home page

4

www.sbVB.com.br

Sequential server

client server

accept

connect

read

send

process

send

read

Page 5: ppt - Sergio Barbosa Villas-Boas (sbVB) home page

5

www.sbVB.com.br

Web Browser java Applet

web &Applet

Page 6: ppt - Sergio Barbosa Villas-Boas (sbVB) home page

6

www.sbVB.com.br

libraries for java applet

• AWT and Swing are ok– AWT is more traditional, but we will use

Swing (more powerful) for java applets.

• SWT is not ok (because it would only work if the user has a dll (or so or something equivalent) in his/her path.– Since that can’t be assumed, for

practical purposes SWT is not allowed to be used as java web applets.

Page 7: ppt - Sergio Barbosa Villas-Boas (sbVB) home page

7

www.sbVB.com.br

<!– test_applet.html --><html><head><title>Applet Test</title></head><applet code ="HelloApplet.class" width=700 height=400> </applet></body></html>

// HelloApplet.javapublic class HelloApplet extends Applet { public void paint(Graphics g) { g.drawString("Hello Applet",20,20); }}

Applet

HelloApplet

http://…/test_applet.html

Hello Applet

Java applet (hello applet)

Page 8: ppt - Sergio Barbosa Villas-Boas (sbVB) home page

8

www.sbVB.com.br

Sun’s Java Certification

Page 9: ppt - Sergio Barbosa Villas-Boas (sbVB) home page

9

www.sbVB.com.br

References

• Erich's Java cheat sheet for C++ programmershttp://www4.ncsu.edu/~kaltofen/courses/Languages/JavaExamples/cpp_vs_java/

• Tutorialhttp://www.java2s.com/Tutorial/Java/CatalogJava.htmhttp://www.e-learningcenter.com/java2.htm

Page 10: ppt - Sergio Barbosa Villas-Boas (sbVB) home page

10

www.sbVB.com.br

j2ee jboss

j2eejboss

Page 11: ppt - Sergio Barbosa Villas-Boas (sbVB) home page

11

www.sbVB.com.br

Some web sites for JBOSS

• http://www.jboss.org/• http://sourceforge.net/projects/jboss/• http://www.opentier.net/• http://java.sun.com/xml/ns/j2ee/

Page 12: ppt - Sergio Barbosa Villas-Boas (sbVB) home page

12

www.sbVB.com.br

What is JBOSS?

• JBOSS is an application server compliant with J2EE.

• It is a free substitute for commercial J2EE application servers such as IBM’s WebSphere (http://www.ibm.com/websphere/wic).

• Using JBOSS one can develop cross-platform client-server applications with object-oriented approach.– Usually developing EJB’s (Entrerprise Java Beans).– Mostly the client-server application is web based.

Page 13: ppt - Sergio Barbosa Villas-Boas (sbVB) home page

13

www.sbVB.com.br

Installing JBOSS in Windows

• Install the JVM for Windows.• Copy the JBOSS distribution to a

directory.• Run JBOSS “by hand” by opening a

console and typing “run –c all”• Transform JBOSS to a Windows

service with Alexandria.• Use the Windows service tool

Page 14: ppt - Sergio Barbosa Villas-Boas (sbVB) home page

14

www.sbVB.com.br

Java web kit

java + jsp + servlet + tomcat

+ jsf

Page 15: ppt - Sergio Barbosa Villas-Boas (sbVB) home page

15

www.sbVB.com.br

Make sure that tomcat is running (v 6.0.14)

Page 16: ppt - Sergio Barbosa Villas-Boas (sbVB) home page

16

www.sbVB.com.br

Tomcat architecture

webbrowser

tomcat

servlet

jsp BO

DAO

Page 17: ppt - Sergio Barbosa Villas-Boas (sbVB) home page

17

www.sbVB.com.br

MVC architecture with Tomcat

webbrowser

tomcat

controllerservlethtml

(form)request

responsehtml

Control Model(business

rules)

View(JSP)

Login

AppHandler

App

. . .

DAO

. . .Handler

LoginHandler

Appjsp

. . .jsp

Loginjsp

Page 18: ppt - Sergio Barbosa Villas-Boas (sbVB) home page

18

www.sbVB.com.br

Control Tieractivities of Controller Servlet

• read the form (either normal or multi-part)• save form variables in session

– important information must not stored as local objects or as fields (attributes) of the Controller Servlet itself. If done like this, different users will share the same information.

• determine handler to go• send to handler• The controller is most often almost done

for your project

Page 19: ppt - Sergio Barbosa Villas-Boas (sbVB) home page

19

www.sbVB.com.br

Model TierActivities of handler

• implement the business rules• does not know where the attributes come from

– (could have been html or other source)• might access database• prepare the output

– save as session or page attributes the variables to the view tier• indicate the jsp output

– data can be transferred through a bean or tag. • The model tier should not deal with http directly or with

appearance.• Classes usually do not inherit from others.• If database is to be accessed, classes should be specially

developed to provide this access.– These classes are DAO (Data Access Objects).– If the database is to be exchanged, the only tier to be this one. For

instance, hibernate should be written inside DAO classes.• Model classes can be tested locally without concern of web

appearance.• The developer should talk to the system analyst.

Page 20: ppt - Sergio Barbosa Villas-Boas (sbVB) home page

20

www.sbVB.com.br

View Tieractivities of jsp

• The developer uses html, javascript.– Eventually, also uses beans and

tags.

• The developer should have sense of good appearance for a web page.

Page 21: ppt - Sergio Barbosa Villas-Boas (sbVB) home page

21

www.sbVB.com.br

Session of Tomcat

• Each user has an identification.– Variables can’t be stored in the processRequest

method, nor can be stored as attributes (fields) in the MyHttpServlet class.

• Ways to store information that persist through more than one web page.– cookie– session

• Tomcat uses cookies automatically to control the sessions.

• A Session object is created in Tomcat for each active session of the system.– One of the session parameters is its timeout. If the

user takes too long to use the session, it expires.

Page 22: ppt - Sergio Barbosa Villas-Boas (sbVB) home page

22

www.sbVB.com.br

Request of Tomcat

• The request object is constant for each request.

Page 23: ppt - Sergio Barbosa Villas-Boas (sbVB) home page

23

www.sbVB.com.br

Application of Tomcat

• The application object remains alive while Tomcat itself is alive.

• To save into the application object, is usual for global variables.

Page 24: ppt - Sergio Barbosa Villas-Boas (sbVB) home page

24

www.sbVB.com.br

MVC architecture

• MVC = model – view – controller • Custom Bean classes should implement

java.io.Serializable, so that when you close the Tomcat, the server state can be saved to disk.– Serializable beans allow load balance of multiple

servers.

• Custom Bean classes should have get’s and set’s for its attributes, to fit for automatic jsp processing.– In netbeans, use <alt><ins> to automatically

code for sets and gets.

Page 25: ppt - Sergio Barbosa Villas-Boas (sbVB) home page

25

www.sbVB.com.br

SOAP & Web Services

SOAP & Web Services

Page 26: ppt - Sergio Barbosa Villas-Boas (sbVB) home page

26

www.sbVB.com.br

Web services

• Inspired in CORBA• Based in

– xml– http– neutral with respect to

programming language

Page 27: ppt - Sergio Barbosa Villas-Boas (sbVB) home page

27

www.sbVB.com.br

optional

port 80 (http)XML ->XML<-

web services(apache)

client ofweb services

Page 28: ppt - Sergio Barbosa Villas-Boas (sbVB) home page

28

www.sbVB.com.br

A new java-based web system based on mercury framework

new system

Page 29: ppt - Sergio Barbosa Villas-Boas (sbVB) home page

29

www.sbVB.com.br

mercury framework features

• Elegantly simple– easy and fast to learn

• i18n (internationalization)– To add support to any language requires little else than translating a list of text

message• Elegant permission management

– soft link of use case and actor• Allow multipart forms

– easy implementation of file upload• Ajax

– It is possible to update pieces of web pages using ajax• Isolation of design

– MVC– jsp pages that have design can (should) be edited using specialized design software,

such as Dreamweaver• Multi client capability

– Easy do produce output for different clients. E.g. Modern Web browser, obsolete web browser, cell-phone web browser.

• Induce usage of tests– The system to be developed is to have tests to help debug / maintain / document it.

Page 30: ppt - Sergio Barbosa Villas-Boas (sbVB) home page

30

www.sbVB.com.br

mercury framework & MVC architecture

tomcat

(Mercury,i.e. already

done)controller

servlet

html / jsp(form)

request

responsehtml / jsp

Control Model(business

rules)

View(JSP)

Login

AppHandler

App

. . .

DAO

. . .Handler

LoginHandler

Appjsp

. . .jsp

Loginjsp

Every jsp or html pagemust have a form withaction to controller (of Mercury)

based ontargetHandlers

Page 31: ppt - Sergio Barbosa Villas-Boas (sbVB) home page

31

www.sbVB.com.br

Setup for the new system

• The system should have a name. In our case: testWeb.

• The system should include libraries: – mercury-1.0.jar– commons-fileupload-1.2.jar– commons-io-1.3.1.jar

• Every jsp or html page must have a form with action to the same servlet: controller (of Mercury)

Page 32: ppt - Sergio Barbosa Villas-Boas (sbVB) home page

32

www.sbVB.com.br

Permission styles

• Case 1 (auto-menu)– if you don’t have permission, you don’t see the

option• Case 2 (auto grayed menu)

– if you don’t have permission, you see the option as grayed, and clicks don’t work

• Case 3 (full menu)– if you don’t have permission, you see the option,

can click, but the answer of the click is a message telling that you don’t have the permission.

• Case 4 (honey pot problem)– if you don’t have permission, you click and see a

false page; while someone receives a message telling about the access that was made.

Page 33: ppt - Sergio Barbosa Villas-Boas (sbVB) home page

33

www.sbVB.com.br

Basic system structure

• Firm name: acme• First menu options

– finance.jsp– people_01.jsp– people_02.jsp– sales.jsp– production.jsp

Page 34: ppt - Sergio Barbosa Villas-Boas (sbVB) home page

34

www.sbVB.com.br

Request-handler map

• login.jsp -> pluto.LoginHandler• finance.jsp

-> acme.finance.FinanceHandler• people_01.jsp ->

acme.people.PeopleHandler• people_02.jsp /• sales.jsp -> acme.sales.SalesHandler• production.jsp ->

acme.production.ProductionHandler

Page 35: ppt - Sergio Barbosa Villas-Boas (sbVB) home page

35

www.sbVB.com.br

Request-handler map

• Every web page that places request must do it by using a form, and include as an item of the form a element type=“hidden” name=“targetHandler” value=<related_handler>

<form action="controller" method="post" name="..."><input type="hidden" name="targetHandler" value="acme.finance.FinanceHandler"></form>

acme.finance.FinanceHandler fh = Class.forname(...). getInstance();

Page 36: ppt - Sergio Barbosa Villas-Boas (sbVB) home page

36

www.sbVB.com.br

Mercury database

• login: mercury• pwd: mercuryPwd