X

Geertjan's Blog

  • August 10, 2005

Call Web Services Directly From JSP Pages!

Geertjan Wielenga
Product Manager
Something I've been looking forward to is now available in the NetBeans IDE Dev daily builds:

That's pretty cool. It saves me from having to create a servlet in order to call a web service operation -- I can just use the JSP page that is created automatically whenever I create a web application. However, I imagine that you'd only do this in small applications or in order to quickly test something. In general, using servlets seems a more robust solution. But, anyway, let's go ahead and re-implement the FeedReader Client for Dummies (which is an example of a small application) directly in a JSP page:

  1. Create a web application (Ctrl-Shift-N) and make sure that the Sun Java System Application Server is your target server.

  2. Right-click the web application and use the New File wizard to generate a web service client that references this WSDL file:

    http://www.webservicex.net/RssToHTML.asmx?WSDL

  3. In the default index.jsp, right-click and choose the menu item shown in the illustration above. Select the getHTML operation in the Select Operation to Invoke dialog box and click OK.

    Now you'll see that the IDE generates three pieces of code: after the closing <html> tag, you see two private methods getRSStoHTML and getRSStoHTMLSoap(). Don't worry about these -- they're just there to make the connection to the web service. However, this is the interesting part, within the <body>tags, the code that invokes the web service:

       <%
    // TODO compute web service operation arguments here, e.g.:
    // String arg0 = request.getParameter("arg0");
    try {
    out.println("result = "+
    getRSStoHTMLSoap().getHTML(/\* TODO enter operation arguments \*/));
    } catch(java.rmi.RemoteException ex) {
    // TODO handle remote exception
    }
    %>

    All you need to do here, for the simple feedreader client, is to specify the URL to the feed. So, let's use the NetBeans Feed again:

    http://www.netbeans.org/rss-091.xml

  4. Change this line:

    getRSStoHTMLSoap().getHTML(/\* TODO enter operation arguments \*/));

    to this:

    getRSStoHTMLSoap().getHTML("http://www.netbeans.org/rss-091.xml"));

  5. Right-click the project and choose Run Project. This is the result (click to enlarge):


I ended FeedReader Client for Dummies by asking 'if there are simpler ways to create a feedreader client, I'd like to hear about it'. Well, I guess I now have!

Join the discussion

Comments ( 1 )
  • guest Wednesday, October 5, 2005
    y
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.