Java Software Solutions Foundations of Program Design

advertisement

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

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

2

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)

Find the Web service (WSDL) web service registry

(UDDI)

Client

(SOAP)

Messages

Publish the web service

(WSDL)

Web service

Provider

3

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 selfdescribing

 Web services can be discovered using UDDI

4

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)

5

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

6

The Promise of Web

Services

web-based SOA as new system design paradigm

7

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)

8

import java.util.*;

Example: A simple Web

Service. Lets the user gives the name of one of the teams in the 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"); U.S. National

Hockey League, and the service standings.put("central/st.louis", "3");

} public String getCurrentPosition(String division, String team) {

String p = (String)standings.get(division + '/' + team); returns the team's current position.

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

}

}

9

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

10

package hansen.playground;

The client import org.apache.axis.client.Call; needs to specify import org.apache.axis.client.Service; import javax.xml.rpc.namespace.QName; is the URL of import java.net.*; the jws-file public class NHLServiceClient { and the name public static void main(String [] args) throws Exception {

Service service = new Service(); of the method

Call call = (Call)service.createCall(); to invoke,

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

Prepare the call.setTargetEndpointAddress(new URL(endpoint)); call.setOperationName(new QName("getCurrentPosition")); Arguments of the

String division = args[0];

Method and

String team = args[1];

Invoke it

String position =

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

String(team)});

11

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

The Bigger Picture: Java 2

Enterprise Edition J2EE

 J2EE Architecture

12

J2EE Container Architecture

13

Container Service APIs

 Example: 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

14

Java Servlets

 Servlets are small server-side programs

15

Accessing Servlets

 The Java Servlet API provides a simple framework for building applications on web servers

16

The Servlet Life Cycle

17

Example of a servlet

18

Servlet code cont.

19

Example: HelloServelet

20

The HelloServer servlet output

21

Using Form Data

Example:

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>

22

Example: The ThreeParams

Servlet

23

The Result

24

Java Server Pages

 JSP , 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 content

 The web container converts the JSP page into a servlet class and compiles it

Example of scriplets:

<%! private int someField = 5; %>

<%! private void someMethod(...) {...} %>

25

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+Program ming :

The ThreeParams example in JSP

<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>

27

Servlets/JSP

28

Servlets/JSP

29

Servlets/JSP Example

30

Download