Share
Explore

Student lab: Simplest introduction to the spark of Java Web Services Remote method invocation

Introductory lab on Java Web Services and Remote Method Invocation: A simple "Hello World" style web service.
Learning outcomes:
This lab will introduce students to the basic concepts without overwhelming them with complex business logic.
Here's a simple, engaging lab:
Lab Title: "Remote Greeting Service"
Objective:
Create a simple web service that allows clients to send their name and receive a personalized greeting in return.
Steps:
Create the Web Service Interface:
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;

@WebService
public interface GreetingService {
@WebMethod
String getGreeting(@WebParam(name = "name") String name);
}
Implement the Web Service:
java
Copy
import javax.jws.WebService;

@WebService(endpointInterface = "GreetingService")
public class GreetingServiceImpl implements GreetingService {
public String getGreeting(String name) {
return "Hello, " + name + "! Welcome to Java Web Services!";
}
}
Publish the Web Service:
java
Copy
import javax.xml.ws.Endpoint;

public class GreetingPublisher {
public static void main(String[] args) {
Endpoint.publish("http://localhost:8080/greetingservice", new GreetingServiceImpl());
System.out.println("Greeting Service is published!");
}
}
Create a Client to consume the Web Service:
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import java.net.URL;

public class GreetingClient {
public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/greetingservice?wsdl");
QName qname = new QName("http://impl.service.example.com/", "GreetingServiceImplService");
Service service = Service.create(url, qname);
GreetingService greetingService = service.getPort(GreetingService.class);
String name = "Alice"; // Students can change this to their own name
System.out.println(greetingService.getGreeting(name));
}
}

Lab Tasks for Students:

Implement the GreetingService interface and GreetingServiceImpl class.
Create the GreetingPublisher to publish the web service.
Run the publisher and verify that the service is running (they should see the "Greeting Service is published!" message).
Implement the GreetingClient to consume the web service.
Run the client and verify that it receives the correct greeting.
Modify the getGreeting method to include the current time in the greeting.
(Optional) Create a simple GUI for the client using Java Swing or JavaFX.
This lab introduces several key concepts:
Web Service annotations (@WebService, @WebMethod, @WebParam)
Publishing a web service
Creating a client to consume a web service
Basic remote method invocation
It's simple enough for beginners to understand and implement quickly, but also provides room for creativity and extension. The personalized greeting aspect makes it more engaging than a standard "Hello World" example.
To make the lab more interactive, you could have students pair up and try to consume each other's web services by exchanging localhost URLs. This would reinforce the concept of distributed computing and network communication.
CopyRetry
Claude does not have the ability to run the code it generates yet.

Does this use a Java Application Server
The example I provided doesn't explicitly use a Java Application Server, but it's important to clarify this point.
Lightweight Deployment: The example uses the javax.xml.ws.Endpoint class to publish the web service. This approach creates a lightweight HTTP server and deploys the web service using it. It's suitable for testing and development but not for production environments.
Java Application Server Alternative: In a more realistic scenario, especially for production, you would deploy your web service to a Java Application Server. Common choices include:
Apache Tomcat
GlassFish
WildFly (formerly JBoss)
IBM WebSphere
Oracle WebLogic
Modifying the Lab for Application Server Deployment: To adapt this lab to use a Java Application Server like Tomcat, you would: a. Package the service in a WAR (Web Application Archive) file. b. Create a web.xml deployment descriptor or use annotations. c. Deploy the WAR file to the application server.
Here's how you could modify the lab to use Tomcat:
Add a web.xml file in the WEB-INF directory:
xml
Copy
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>
<servlet>
<servlet-name>GreetingService</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>GreetingService</servlet-name>
<url-pattern>/greetingservice</url-pattern>
</servlet-mapping>
</web-app>
Create a sun-jaxws.xml file in the WEB-INF directory:
xml
Copy
<?xml version="1.0" encoding="UTF-8"?>
<endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime" version="2.0">
<endpoint
name="GreetingService"
implementation="GreetingServiceImpl"
url-pattern="/greetingservice"/>
</endpoints>
Package these files along with your compiled classes into a WAR file.
Deploy the WAR file to Tomcat's webapps directory.
Modify the client to use the new URL:
java
Copy
URL url = new URL("http://localhost:8080/your_war_name/greetingservice?wsdl");
Lab Extension for Application Server:
Install Apache Tomcat on their development machines.
Package the web service as a WAR file.
Deploy the WAR file to Tomcat.
Start Tomcat and verify the web service is running.
Modify the client to connect to the Tomcat-hosted service.
This extension would introduce students to:
Java Application Servers
WAR file structure
Deployment descriptors
The deployment process
While this adds complexity, it provides a more realistic scenario of how web services are typically deployed in production environments. You could offer this as an advanced option or a follow-up lab to the simpler Endpoint-based version.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.