Home » Archive

Articles tagged with: java bean

Java Codes »

[6 Mar 2009 | No Comment | 426 views]

This code example shows how to create a simple web service. We use the annotation @WebService to declare the class as a such.
The annotation @WebMethod is provided at method level to declare it as an operation for the web service.
The operation getTime of the JavadbWebService simply returns the current time.
package com.javabout.ws.example;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.jws.WebMethod;
import javax.jws.WebService;
/**
 *
 * @author www.javabout.com
 */
@WebService()
public class JavaboutWebService {
    
    @WebMethod
    public String getTime() {
        
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat(“HH:mm”);
        return (sdf.format(calendar.getTime()));
        
    }
}
The result of a call to the operation getTime produces something like this:

14:15

This is the SOAP request and response to …

Java Codes »

[6 Mar 2009 | No Comment | 485 views]

This example shows how to create a web service client using JAX-WS reference implementation and its tool ‘wsimport’.
JAX-WS RI can be downloaded from Sun (http://java.sun.com).
To create the web service client we need an existing web service, so we will use the one created in the example ‘Create a simple Web Service’:

Click here to go to the ‘Create a simple Web Service’ example

So we assume the web service is deployed on our local computer and it listens to port 8080.
Go to the bin directory in the jax-ws ri installation directory and …

Java Codes »

[6 Mar 2009 | No Comment | 2,041 views]

Sometimes you need to insert information in the soap header when calling a web service. Perhaps the service needs authentication information that needs to be set.
This example shows how to set the security information for a Web Service that is deployed on a Weblogic server using JAX-WS and SAAJ.
First we need to create the actual handler which implements the SOAPHandler interface.
Next we need to create the class that implements the HandlerResolver interface. This class decides what handlers should be called and in what specific order. The handler above is added …

Java Codes »

[6 Mar 2009 | No Comment | 418 views]

This example shows how to display all available time zone IDs. These IDs are essential when you want to use classes like Date and Calendar and display the time in another time zone than default.To retrieve the time zones we call the static method getAvailableIDs of the TimeZone class. It returns an array of strings with the ID of each time zone.The array returned is quite extensive and to make it easier to find what we’re looking for in the output, we call the sort method of the Arrays class …

Java Codes »

[6 Mar 2009 | No Comment | 275 views]

To display time in a specific time zone we can use the Calendar class. After it has been created we set the time zone on the instance using the method setTimeZone. It is really all that is to it, after that we can just query the instance for hours and minutes as in the code example below where time in three major cities around the world is displayed.
package com.javadb.examples;import java.util.Calendar;import java.util.TimeZone;/** * * @a*#117;tho*#114; w*#119;*#119;*#46;j*#97;*#118;adb.*#99;om */public class Main {    public void setTimeZones() {                Calendar calNewYork = Calendar.getInstance();        Calendar calParis = Calendar.getInstance();        Calendar calTokyo = Calendar.getInstance();                calNewYork.setTimeZone(TimeZone.getTimeZone(“America/New_York”));        calParis.setTimeZone(TimeZone.getTimeZone(“Europe/Paris”));        calTokyo.setTimeZone(TimeZone.getTimeZone(“Asia/Tokyo”));                System.out.println(“Time in New York: …

Java Codes »

[6 Mar 2009 | No Comment | 325 views]

This example shows how to load a ResourceBundle from the classpath and then enumerate through it.Since ResourceBundle is an abstract class and therefore cannot be instantiated we call its static method getBundle() which returns the ResourceBundle instance.The method has a few overloads but we use the one that takes a basename (in this case ‘Phrases’) and a locale. This means that somewhere on the classpath there has to be a file named ‘Phrases_en_US.properties’.In this example the file only has two rows (see further below). An Enumeration with the keys is …

Java Codes »

[6 Mar 2009 | No Comment | 238 views]

This example shows how to get the currency symbols for different countries. The code below displays the currency symbols for three countries, US, UK and France.
package com.javadb.examples;import java.util.Currency;import java.util.Locale;/** * * *#64;author *#119;*#119;*#119;*#46;java*#100;b.*#99;*#111;*#109;  */public class Main {        public void displayCurrencySymbols() {                Currency currency = Currency.getInstance(Locale.US);         System.out.println(“United States: “ currency.getSymbol());         currency = Currency.getInstance(Locale.UK);        System.out.println(“United Kingdom: “ currency.getSymbol());         currency = Currency.getInstance(Locale.FRANCE);        System.out.println(“France: “ currency.getSymbol());            }     public static void main(String[] args) {        new Main().displayCurrencySymbols();    }  }

The output from the code example:

United States: USDUnited Kingdom: GBPFrance: €

Java Codes »

[6 Mar 2009 | No Comment | 250 views]

This class prints current date and time by creating a Date object and using instances of SimpleDateFormat to format the Date object.See the SimpleDateFormat api reference for more format options.
import java.text.SimpleDateFormat;import java.util.Date;public class GetDateTime {    public GetDateTime() {        SimpleDateFormat sdfDate = new SimpleDateFormat(“dd/MM/yyyy”);    SimpleDateFormat sdfTime = new SimpleDateFormat(“HH:mm:ss”);        Date now = new Date();        String strDate = sdfDate.format(now);    String strTime = sdfTime.format(now);        System.out.println(“Date: “ strDate);    System.out.println(“Time: “ strTime);      }  public static void main(String[] args) {    GetDateTime getDateTime = new GetDateTime();  }} 

Java Codes »

[6 Mar 2009 | No Comment | 395 views]

>This code example shows how to increment and decrement a date using an instance of the Calendar class.The first method, incrementDate, increments the Calendar which is passed to the method by 5 days.The second method, decrementDate, decrements the same Calendar instance with one month.
>import java.util.Calendar;/** * * @*#97;*#117;t*#104;*#111;*#114; *#119;*#119;w*#46;*#106;*#97;v*#97;*#100;*#98;.*#99;*#111;m */public class Main {        public void incrementDate(Calendar cal) {                int daysToIncrement = 5;         cal.add(Calendar.DATE, daysToIncrement);        System.out.println(“Date after increment: “ cal.getTime());    }        public void decrementDate(Calendar cal) {                int monthsToDecrement = -1;        cal.add(Calendar.MONTH, monthsToDecrement);        System.out.println(“Date after decrement: “ cal.getTime());     }    /**     * @param args the command line arguments     */    public static void main(String[] args) {                Calendar …

Java Codes »

[6 Mar 2009 | No Comment | 294 views]

>This code example shows how to output today’s date. It uses the Calendar class. Since it is an abstract class and therefore cannot be instantiated we created it using the getInstance() method. Then we create an instance of the SimpleDateFormat class which takes the format we want to display the date in as argument to the constructor.Finally the method ‘format()’ is called on the SimpleDateFormat object. The format() method takes a java.util.Date object as argument and we can get our hands on one by calling the getTime() method of the …