300 likes | 445 Views
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. What is a web service?.
E N D
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
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
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) Find the Web service (WSDL) Publish the web service (WSDL) Web service Provider Client (SOAP) Messages
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
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)
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
The Promise of Web Services web-based SOA as new system design paradigm
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)
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 Web Service. Lets the 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.
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
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 client needs to specify is the URL of the jws-file and the name of the method to invoke, Prepare the Arguments of the Method and Invoke it
The Bigger Picture: Java 2 Enterprise Edition J2EE • J2EE Architecture
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
Java Servlets • Servlets are small server-side programs
Accessing Servlets • The Java Servlet API provides a simple framework for building applications on web servers
Using Form DataExample: An HTML Form With ThreeParameters <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>
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(...) {...} %>
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>
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>