30
1 Java Server Programming Lecture 1 focuses on: Introduction to web services Web Services using Axis The bigger Picture: Introduction to J2EE Java Servlets Java Server Pages (JSP) Servlets/JSP

1 Java Server Programming zLecture 1 focuses on: yIntroduction to web services y Web Services using Axis y The bigger Picture: Introduction to J2EE y Java

Embed Size (px)

Citation preview

Page 1: 1 Java Server Programming zLecture 1 focuses on: yIntroduction to web services y Web Services using Axis y The bigger Picture: Introduction to J2EE y Java

1

Java Server Programming

Lecture 1 focuses on: Introduction to web services Web Services using Axis The bigger Picture: Introduction to J2EE Java Servlets Java Server Pages (JSP) Servlets/JSP

Page 2: 1 Java Server Programming zLecture 1 focuses on: yIntroduction to web services y Web Services using Axis y The bigger Picture: Introduction to J2EE y Java

2

What is a web service?

A web Service is a server side application component accessible via web protocols (HTTP over TCP)

It provides services and data to remote clients and other applications

It provides a generic and standardized support for client-server paradigm accessible via the web

The idea behind web services came about to allow big corps like Microsoft to provide a web service registry, and then you'd pay (on a per-use basis) for every web service you wanted to use (as opposed to having individual applications installed on your computer

Page 3: 1 Java Server Programming zLecture 1 focuses on: yIntroduction to web services y Web Services using Axis y The bigger Picture: Introduction to J2EE y Java

3

How do web services work? Clients communicate with the web service via XML

messages based on a protocol called SOAP (encoding and decoding messages in XML is supported by Apache Axis)

web service registry (UDDI)

ClientWeb service

Provider

Publish the web service(WSDL)

Find the Web service (WSDL)

(SOAP)Messages

Page 4: 1 Java Server Programming zLecture 1 focuses on: yIntroduction to web services y Web Services using Axis y The bigger Picture: Introduction to J2EE y Java

4

More on Web Services

loosely coupled, reusable components

encapsulate discrete functionality

distributed

programmatically accessible over standard internet protocols

add new level of functionality on top of the current web

Web services are self-contained and self-describing

Web services can be discovered using UDDI

Page 5: 1 Java Server Programming zLecture 1 focuses on: yIntroduction to web services y Web Services using Axis y The bigger Picture: Introduction to J2EE y Java

5

Standard Protocols used by Web Services

UDDI -- The "Universal Description, Discovery and Integration" protocol. A protocol for publishing web service descriptions

WSDL – “Web Service Description Language” is a description language: using XML that describes exactly what your web service does

SOAP – “Simple Object Access Protocol” A transport protocol that sends XML messages using HTTP (which runs on top of TCP, usually on port 80)

Page 6: 1 Java Server Programming zLecture 1 focuses on: yIntroduction to web services y Web Services using Axis y The bigger Picture: Introduction to J2EE y Java

6

What is SOAP?

The basic Web services platform is XML plus HTTP.

SOAP stands for Simple Object Access Protocol SOAP is a communication protocol SOAP is for communication between applications SOAP is a format for sending messages SOAP is designed to communicate via Internet SOAP is platform independent SOAP is language independent SOAP is based on XML SOAP is simple and extensible SOAP allows you to get around firewalls

Page 7: 1 Java Server Programming zLecture 1 focuses on: yIntroduction to web services y Web Services using Axis y The bigger Picture: Introduction to J2EE y Java

7

The Promise of Web Servicesweb-based SOA as new system design paradigm

Page 8: 1 Java Server Programming zLecture 1 focuses on: yIntroduction to web services y Web Services using Axis y The bigger Picture: Introduction to J2EE y Java

8

Apache eXtensible Interaction System (Axis)

Axis supports the interaction between the client and the server (the web service)

Axis is an implementation of the SOAP protocol. It shields the developer from the details of dealing with SOAP and WSDL

You use Axis on the server side to write your web service (and deploy it as a Tomcat webapp)

At the client side, Axis sends SOAP messages to invoke the methods of the server (using remote procedure calls)

Axis lets the client make the method calls on the web service object as if it were a local object (AXIS generates a WSDL for the web service)

Page 9: 1 Java Server Programming zLecture 1 focuses on: yIntroduction to web services y Web Services using Axis y The bigger Picture: Introduction to J2EE y Java

9

import java.util.*;

public class NHLService {

HashMap standings = new HashMap();

public NHLService() {

// NHL - part of the standings as per 04/07/2002

standings.put("atlantic/philadelphia", "1");

standings.put("atlantic/ny islanders", "2");

standings.put("atlantic/new jersey", "3");

standings.put("central/detroit", "1");

standings.put("central/chicago", "2");

standings.put("central/st.louis", "3");

}

public String getCurrentPosition(String division, String team) {

String p = (String)standings.get(division + '/' + team);

return (p == null) ? "Team not found" : p;

}

}

Example: A simple WebService. Letsthe user gives the name of one of the teams in the U.S. National Hockey League, and the service returns the team's current position.

Page 10: 1 Java Server Programming zLecture 1 focuses on: yIntroduction to web services y Web Services using Axis y The bigger Picture: Introduction to J2EE y Java

10

How to delpoy and use a web service

The steps needed to create and use the "getCurrentPosition" web service. First you copy the NHLService.java file into

the Axis directory on your web server Then you rename the file to

NHLService.jws (JWS stands for Java Web Service).

The web service is now deployed

Page 11: 1 Java Server Programming zLecture 1 focuses on: yIntroduction to web services y Web Services using Axis y The bigger Picture: Introduction to J2EE y Java

11

package hansen.playground;

import org.apache.axis.client.Call;

import org.apache.axis.client.Service;

import javax.xml.rpc.namespace.QName;

import java.net.*;

public class NHLServiceClient {

public static void main(String [] args) throws Exception {

Service service = new Service();

Call call = (Call)service.createCall();

String endpoint = "http://localhost:8080/axis/NHLService.jws";

call.setTargetEndpointAddress(new URL(endpoint));

call.setOperationName(new QName("getCurrentPosition"));

String division = args[0];

String team = args[1];

String position =

(String)call.invoke(new Object [] {new String(division), new String(team)});

System.out.println("Got result : " + position);}}

The clientneeds to specify is the URL of the jws-file and the name of the method to invoke,Prepare the Arguments of theMethod andInvoke it

Page 12: 1 Java Server Programming zLecture 1 focuses on: yIntroduction to web services y Web Services using Axis y The bigger Picture: Introduction to J2EE y Java

12

The Bigger Picture: Java 2 Enterprise Edition J2EE

J2EE Architecture

Page 13: 1 Java Server Programming zLecture 1 focuses on: yIntroduction to web services y Web Services using Axis y The bigger Picture: Introduction to J2EE y Java

13

J2EE Container Architecture

Page 14: 1 Java Server Programming zLecture 1 focuses on: yIntroduction to web services y Web Services using Axis y The bigger Picture: Introduction to J2EE y Java

14

Container Service APIsExample: create audio component, publish its

name in a naming service (JNDI) available to your application. This provides a simple method to access the service APIs

Page 15: 1 Java Server Programming zLecture 1 focuses on: yIntroduction to web services y Web Services using Axis y The bigger Picture: Introduction to J2EE y Java

15

Java ServletsServlets are small server-side programs

Page 16: 1 Java Server Programming zLecture 1 focuses on: yIntroduction to web services y Web Services using Axis y The bigger Picture: Introduction to J2EE y Java

16

Accessing ServletsThe Java Servlet API provides a simple

framework for building applications on web servers

Page 17: 1 Java Server Programming zLecture 1 focuses on: yIntroduction to web services y Web Services using Axis y The bigger Picture: Introduction to J2EE y Java

17

The Servlet Life Cycle

Page 18: 1 Java Server Programming zLecture 1 focuses on: yIntroduction to web services y Web Services using Axis y The bigger Picture: Introduction to J2EE y Java

18

Example of a servlet

Page 19: 1 Java Server Programming zLecture 1 focuses on: yIntroduction to web services y Web Services using Axis y The bigger Picture: Introduction to J2EE y Java

19

Servlet code cont.

Page 20: 1 Java Server Programming zLecture 1 focuses on: yIntroduction to web services y Web Services using Axis y The bigger Picture: Introduction to J2EE y Java

20

Example: HelloServelet

Page 21: 1 Java Server Programming zLecture 1 focuses on: yIntroduction to web services y Web Services using Axis y The bigger Picture: Introduction to J2EE y Java

21

The HelloServer servlet output

Page 22: 1 Java Server Programming zLecture 1 focuses on: yIntroduction to web services y Web Services using Axis y The bigger Picture: Introduction to J2EE y Java

22

Using Form DataExample: An HTML Form With Three

Parameters

<FORM ACTION="/servlet/coreservlets.ThreeParams">First Parameter: <INPUT TYPE="TEXT" NAME="param1"><BR>Second Parameter: <INPUT TYPE="TEXT" NAME="param2"><BR>Third Parameter: <INPUT TYPE="TEXT" NAME="param3"><BR><CENTER><INPUT TYPE="SUBMIT"></CENTER></FORM>

Page 23: 1 Java Server Programming zLecture 1 focuses on: yIntroduction to web services y Web Services using Axis y The bigger Picture: Introduction to J2EE y Java

23

Example: The ThreeParams Servlet

Page 24: 1 Java Server Programming zLecture 1 focuses on: yIntroduction to web services y Web Services using Axis y The bigger Picture: Introduction to J2EE y Java

24

The Result

Page 25: 1 Java Server Programming zLecture 1 focuses on: yIntroduction to web services y Web Services using Axis y The bigger Picture: Introduction to J2EE y Java

25

Java Server PagesJSP , an extension of the servlet technology,

is a text based document (name.jsp) that contains two parts HTML or XML for static content JSP tags and scriplets in Java that

generates the dynamic contentThe web container converts the JSP page

into a servlet class and compiles itExample of scriplets:

<%! private int someField = 5; %><%! private void someMethod(...) {...} %>

Page 26: 1 Java Server Programming zLecture 1 focuses on: yIntroduction to web services y Web Services using Axis y The bigger Picture: Introduction to J2EE y Java

26

JSP• JavaServer Pages (JSP) lets you separate the dynamic part of your pages from the static HTML. • Simply write the regular HTML in the normal manner, using whatever Web-page-building tools you normally use• then enclose the code for the dynamic parts in special tags, most of which start with "<%" and end with "%>".

•For example, here is a section of a JSP page that results in something like "Thanks for ordering Core Web Programming" for a URL of http://host/OrderConfirmation.jsp?title=Core+Web+Programming: Thanks for ordering <I><%= request.getParameter("title") %></I>

Page 27: 1 Java Server Programming zLecture 1 focuses on: yIntroduction to web services y Web Services using Axis y The bigger Picture: Introduction to J2EE y Java

27

<HTML><TITLE>INPUT FORM</TITLE><BODY>First Parameter-: <%=request.getParameter("param1")%> <BR>Second Parameter-:<%=request.getParameter("param2")%> <BR>Third Parameter-: <%=request.getParameter("param3")%> <BR>

</BODY></HTML>

The ThreeParams example in JSP

Page 28: 1 Java Server Programming zLecture 1 focuses on: yIntroduction to web services y Web Services using Axis y The bigger Picture: Introduction to J2EE y Java

28

Servlets/JSP

Page 29: 1 Java Server Programming zLecture 1 focuses on: yIntroduction to web services y Web Services using Axis y The bigger Picture: Introduction to J2EE y Java

29

Servlets/JSP

Page 30: 1 Java Server Programming zLecture 1 focuses on: yIntroduction to web services y Web Services using Axis y The bigger Picture: Introduction to J2EE y Java

30

Servlets/JSP Example