0

I've created a very basic portlet, which displays a String on-screen.
I have another web application hosted on separate VM which offers a REST api that I'd like to consume.
Understand I cannot make AJAX calls to remote web app (without some effort e.g proxy etc).
But what would be best approach to submitting HTTP GET requests to this remote web app?
I've considered both JSP and Servlets but not sure how to invoke the servlet from inside TestPortletView.xhtml and then display results?

Contents of TestPortletView.html ..

<%@page session="false" contentType="text/html" pageEncoding="ISO-8859-1" import="java.util.*,javax.portlet.*,com.ibm.test.*" %>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>        
<%@taglib uri="http://www.ibm.com/xmlns/prod/websphere/portal/v6.1/portlet-client-model" prefix="portlet-client-model" %>        
<portlet:defineObjects/>
<portlet-client-model:init>
      <portlet-client-model:require module="ibm.portal.xml.*"/>
      <portlet-client-model:require module="ibm.portal.portlet.*"/>   
</portlet-client-model:init> 


<DIV style="margin: 6px">

<H3 style="margin-bottom: 3px">Welcome!</H3>
This is my basic view mode page.<BR>
</DIV>

Project setup ..

enter image description here

bobbyrne01
  • 6,295
  • 19
  • 80
  • 150

2 Answers2

0

There are a few ways to do this:

  1. Have your portlet make calls to the remote web service directly. Get the response, parse it and do what you need to it, then set the data via request.setAttribute (if its a view) or response.setRenderParameter (if its responding to an action) and your page can then display it. This is by far the easiest as your have fewer parts to manage.

  2. Use a UI framework on the front end that allows for you to do AJAX calls (like jQuery). Then your HTML page can make requests to a servlet, that servlet can then do the work making the actual calls to the remote API and return a chunk of data in the response which your page can use. This would be the way to go if you had Java based logic that you needed the servlet to use (since your front end will only have Javascript to work with) AND you wanted it to be a bit more dynamic.

  3. Also using a UI framework, if you don't have any Java based logic and can do it all in Javascript, then you can potentially have all of your logic in your front end code and just make requests to the remote app directly.

Steve Swinsburg
  • 592
  • 2
  • 14
0

From your code snippet, I'm assuming you're using Websphere Portal (the taglib uri). WebSphere Portal offers a feature called the Ajax Proxy. You can enable and configure the proxy to let you make cross domain requests through Portal. Essentially you send an AJAX request to the Portal that specifies the request you want to send to the other domain. Portal then sends that request on the browsers behalf and returns the response. From the browser's perspective you're only sending AJAX requests to the original domain.

The other option is modify your server with REST API to allow CORS. Here is an an existing answer on SO to give you an idea. The implementation will depend on how your REST service is implemented.

Community
  • 1
  • 1
Nick Roth
  • 3,057
  • 2
  • 15
  • 15