#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Simple Java Web Service - help


    This is the task:

    Create a translation service.

    Customer service to activate the service method as follows:

    getWord ("car", "russian", "polish")


    The first parameter is the required word, the second is the original language, and the third target language.

    The method should return a string with the appropriate word or words separated by commas if there are synonyms.

    Data source, the service should use XML documents (the system may have only a few words, in order to test the functionality).


    I started by creating an XML file and establishing a connection between Java and XML:

    import java.io.IOException;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;
    import org.xml.sax.SAXException;
    import org.xml.sax.helpers.DefaultHandler;


    public class Main {


    public static void main(String[] args) throws
    ParserConfigurationException, SAXException, IOException{
    SAXParserFactory spf = SAXParserFactory.newInstance();
    SAXParser sp = spf.newSAXParser();
    sp.parse ("Servis.xml", new MySaxHandler());
    }
    }

    I have no idea what to do next. Please help.
  2. #2
  3. Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    May 2004
    Location
    Superior, CO, USA
    Posts
    2,418
    Rep Power
    1713
    I'm not sure what you're trying to do - a SOAP web service or what? Is this just a command line program or a web based program? Let us know some more details and we'll try to help.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0
    Originally Posted by stdunbar
    I'm not sure what you're trying to do - a SOAP web service or what? Is this just a command line program or a web based program? Let us know some more details and we'll try to help.
    I need to create a Java web service . I can use SOAP or JAX-WS. I am using NetBeans and GlassFish server but this field is new for me so I do not even know where to start.
  6. #4
  7. Meow Black Belt
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2005
    Location
    Beaverton OR
    Posts
    932
    Rep Power
    526
    Originally Posted by @passat
    I need to create a Java web service . I can use SOAP or JAX-WS. I am using NetBeans and GlassFish server but this field is new for me so I do not even know where to start.
    Refer to Java EE tutorial for getting started with creating a web service.
  8. #5
  9. Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    May 2004
    Location
    Superior, CO, USA
    Posts
    2,418
    Rep Power
    1713
    I'd take a look at this link to get started.

    A tiny bit of code to get you going:

    java Code:
    import javax.jws.WebMethod;
    import javax.jws.WebService;
     
    @WebService( serviceName = "TranslationService" )
    public class Translator {
     
        @WebMethod
        public String  getWord( String word, String originalLanguage, String targetLanguage ) {
            // lookup word in original language, return in target language
     
            return word + originalLanguage;
        }
    }


    All this does is define the "TranslationService". And in my case it simply adds the string of the originalLanguage to the end - just something to show that it's different.

    If you deploy this to glassfish you'll be able to see that the web service exists by going to something like http://localhost:8080/yourwebapp/TranslationService?WSDL. In this case "yourwebapp" is the name of your web application.

    The client side is a bit more involved. Basically you'll want to use the Java command "wsimport". This is in the bin directory of your JDK. In a directory, and assuming that your service is deployed to http://localhost:8080/yourwebapp/TranslationService?wsdl you would run something like:

    Code:
    mkdir src
    
    wsimport -keep -s src -p com.yourpackagename.soap http://localhost:8080/yourwebapp/TranslationService?wsdl
    This will generate several files in src/com/yourpackagename/soap. You do not need to understand each of these files. But your two most important ones will be src/com/yourpackagename/soap/TranslationService.java and src/com/yourpackagename/soap/Translator.java (assuming that you use my code above). From there you can access the "service" and "port" (the port will contain a mirror of the method you want.

    That's alot of stuff. Give it a shot and let us know where it breaks.

IMN logo majestic logo threadwatch logo seochat tools logo