In the previous section, we used a java client for the root resource class. To package a client with the web application, a browser-based client such as a JSP client will be required. In this section, we will create a JSP client for the root resource class. Select File | New | Other, and in New, select Web | JSP File and click on Next, as shown here:
In New JSP File wizard, select the webapp
folder and specify File name as jaxrsclient.jsp
, as shown here. Now click on Next.
Select the
New JSP file (html) template, click on Finish. The jaxrsclient.jsp
file gets added to the webapp
folder, as shown here:
In the jaxrsclient.jsp
JSP client, the root resource class resource methods are invoked as in the Java client. The jaxrsclient.jsp
file is listed here:
<%@ page language="java" contentType="text/html; charset=ISO-8859- 1" pageEncoding="ISO-8859-1"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ page import="java.net.URI,javax.ws.rs.core.*,com.sun.jersey.api.client. *,com.sun.jersey.api.client.config.*"%> <html> <head> <meta http-equiv="Content-Type" content="text/xml; charset=windows-1252" /> <title>JAX-RS Client</title> </head> <body> <% ClientConfig clientconfig = new DefaultClientConfig(); Client client = Client.create(clientconfig); WebResource service = client.resource(UriBuilder.fromUri("http://localhost:8080/jboss-jaxrs").build()); out.println(service.path("jaxrs").path("helloworld").accept(MediaType.TEXT_PLAIN).get(String.class)); out.println(service.path("jaxrs").path("helloworld").accept(MediaType.TEXT_XML).get(String.class)); out.println(service.path("jaxrs").path("helloworld").accept(MediaType.TEXT_HTML).get(String.class)); %> </body> </html>