Unit Convertor end to end scenario

Author: Lukas Jungmann
Last update: $Date: 2009/10/29 16:50:43 $, $Revision: 1.1.1.1 $
Introduction:

This end to end scenario should test Web service client creation. The scenario covers developing of web service client with simple message handler.
The document is not intended as test specification for these features but describes developing web service client in NetBeans 5.5 (version for NetBeans 4.1 is here)

Table of Contents


UnitConvertor sources are avalaible here.

Creating web project

  • Go to File - New Project - Web - Web Application
  • Specify UnitConvertor as Project Name
  • Specify project's directory
  • Select Sun App server as target server
  • Choose J2EE 1.4 as J2EE version and
  • Click Finish

Creating web service client

  • Go to File - New File - Web services - Web Service Client - Next
  • Specify http://www.webservicex.net/length.asmx?WSDL as Running Web Service WSDL URL
  • Optional: if you're behind proxy check its settings via Set Proxy ... button
  • Specify package for the client, eg. org.netbeans.end2end.unitconvertor.client
  • Select J2EE Container-generated static stub as client type
  • Click Finish

Creating Message Logger

  • Rigth-click web module node in project tab
  • Invoke New - New File/Folder... - Web Services - Message Handler
  • Type MessageLogger name, specify package, eg. org.netbeans.end2end.unitconvertor and finish New File wizard
  • Open handler's code in editor
  • Replace handleRequest(MessageContext context) and handleResponse(MessageContext context) implementation with the following line:
    	return logMessage(context);
  • Implement logMessage method:
        private boolean logMessage(MessageContext context) {
            SOAPMessageContext smc = (SOAPMessageContext) context;
            SOAPMessage msg = smc.getMessage();
            try {
                msg.writeTo(System.out);
            } catch (IOException ioe) {
                System.out.println(ioe.getMessage());
            } catch (SOAPException se) {
                System.out.println(se.getMessage());
            }
            return true;
        }           
  • Press Shift+Alt+F - this will fix missing imports of java.lang.IOException and javax.xml.soap.SOAPException.
  • Add handler to the client through Configure Handlers... from context menu of LengthUnit client node in project view

Convertor form

  • Open index.jsp code in editor
  • Create form for user input somewhere between the <body> tags
        <form action="convert" method="GET">
          Convert <input type="text" maxlength="10" name="size" value="0.0"/>
          <select name="from">
            <option value="Angstroms">Angstroms</option>
            <option value="Nanometers">Nanometers</option>
            <option value="Microinch">Microinch</option>
            <option value="Microns">Microns</option>
            <option value="Mils">Mils</option>
            <option value="Millimeters">Millimeters</option>
            <option value="Centimeters">Centimeters</option>
            <option value="Inches">Inches</option>
            <option value="Links">Links</option>
            <option value="Spans">Spans</option>
            <option value="Feet">Feet</option>
            <option value="Cubits">Cubits</option>
            <option value="Varas">Varas</option>
            <option value="Yards">Yards</option>
            <option value="Meters">Meters</option>
            <option value="Fathoms">Fathoms</option>
            <option value="Rods">Rods</option>
            <option value="Chains">Chains</option>
            <option value="Furlongs">Furlongs</option>
            <option value="Cablelengths">Cablelengths</option>
            <option value="Kilometers">Kilometers</option>
            <option value="Miles">Miles</option>
            <option value="Nauticalmile">Nauticalmile</option>
            <option value="League">League</option>
            <option value="Nauticalleague">Nauticalleague</option>
          </select>
          to
          <select name="to">
            <option value="Angstroms">Angstroms</option>
            <option value="Nanometers">Nanometers</option>
            <option value="Microinch">Microinch</option>
            <option value="Microns">Microns</option>
            <option value="Mils">Mils</option>
            <option value="Millimeters">Millimeters</option>
            <option value="Centimeters">Centimeters</option>
            <option value="Inches">Inches</option>
            <option value="Links">Links</option>
            <option value="Spans">Spans</option>
            <option value="Feet">Feet</option>
            <option value="Cubits">Cubits</option>
            <option value="Varas">Varas</option>
            <option value="Yards">Yards</option>
            <option value="Meters">Meters</option>
            <option value="Fathoms">Fathoms</option>
            <option value="Rods">Rods</option>
            <option value="Chains">Chains</option>
            <option value="Furlongs">Furlongs</option>
            <option value="Cablelengths">Cablelengths</option>
            <option value="Kilometers">Kilometers</option>
            <option value="Miles">Miles</option>
            <option value="Nauticalmile">Nauticalmile</option>
            <option value="League">League</option>
            <option value="Nauticalleague">Nauticalleague</option>
          </select>
          <button type="submit">Convert!</button>
        </form>
  • Created form should look like:

Servlet

  • Rigth-click web module node in project tab
  • Invoke New - Servlet
  • Type ConvertorServlet name, specify package, eg. org.netbeans.end2end.unitconvertor and press Next
  • Change URL Mapping(s) to /convert and finish New File wizard
  • Open servlet's code in editor
  • Right-click inside the processRequest method of the servlet class and invoke Web Service Client Resources - Call Web Service Operation
  • Select changeLengthUnit and click OK button - now IDE generates helper methods (getLengthUnit() and getLengthUnitSoap()) and also a statement which calls the web service operation:
            try { // This code block invokes the changeLengthUnit operation on web service
                getLengthUnitSoap().changeLengthUnit(/* TODO enter operation arguments */);
            } catch(java.rmi.RemoteException ex) {
                // TODO handle remote exception
            } catch(Exception ex) {
                // TODO handle custom exceptions here
            }
                
  • Add user input handling code into processRequest method, so the method implementation is:
        protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
            String size = request.getParameter("size");
            String from = request.getParameter("from");
            String to = request.getParameter("to");
            double d = 0.0;
            try {
                d = Double.parseDouble(size);
            } catch (NumberFormatException nfe) {
                d = Double.MIN_VALUE;
            }
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Convertor</title>");
            out.println("</head>");
            out.println("<body>");
            if (d != Double.MIN_VALUE) {
                try {
                    out.println(size);
                    out.println(" ");
                    out.println(from);
                    out.println(" is ");
                    out.println(getLengthUnitSoap().changeLengthUnit(d,
                            Lengths.fromString(from), Lengths.fromString(to)));
                    out.println(" ");
                    out.println(to);
                } catch(java.rmi.RemoteException ex) {
                    // TODO handle remote exception
                    ex.printStackTrace(out);
                }
            } else {
                out.println(size + " is not valid number!");
            }
            out.println("</body>");
            out.println("</html>");
            out.flush();
            out.close();
        }
    		
  • Press Shift+Alt+F - this will fix missing import of org.netbeans.end2end.unitconvertor.client.Lengths.

Running application

  • Now we've done our application and we can run it. Here's sample output:
  • And in server output we can see SOAP messages which are comming from/to our client:

Project Features

About this Project

QA was started in November 2009, is owned by Jiří Kovalský, and has 97 members.
By use of this website, you agree to the NetBeans Policies and Terms of Use (revision 20160708.bf2ac18). © 2014, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo
 
 
Close
loading
Please Confirm
Close