Articles tagged with: www.javabout.com
Java Codes »
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 »
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 »
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 »
Since Java 6 there is a new way to check if a String is empty. The most common way to determine if a String is empty is to call the length() method of the String class and check if it returns 0. There is now a method added to the class called isEmpty() which returns a boolean value.The output from the code below is:truetruefalsefalse
/** * * @author javabout.com */public class Main { /** * Example method for checking if a String is empty */ public void checkEmptyString() { String a1 = “”; String b1 = “not an empty string”; System.out.println(“Checking …
Java Codes »
When a Java program has gone through all code it exits automatically by returning zero if all has gone well.In some situations you might want to exit the program with a line of code, and this is done by calling the static exit method of the System class.The exit method takes an int as argument so if you want to exit the program normally you write:
System.exit(0);
Should you want to exit because of for example some error has occured you normally specify a negative number as the argument.You could of course …
Java Codes »
By listing the properties of the system you run the program on you can find out a lot about it. Your program maybe should act in a certain way depending on what operating system it’s currently on.This code example shows how to retrieve the properties and how to enumerate through and print them out to the console.
import java.util.Enumeration;import java.util.Properties;/** * * @author javabout.com */public class Main { /** * Lists the system properties */ public void listSystemProperties() { Properties prop = System.getProperties(); Enumeration enumeration = prop.keys(); while (enumeration.hasMoreElements()) { String key = (String) enumeration.nextElement(); System.out.println(key ” = “ …
Java Codes »
There are severals ways to accomplish this. The example below shows two of them.The second example is only possible if you run Java version 1.5 or later.
import java.text.DecimalFormat;public class NumberUtil { public void roundNumber(double numberToRound) { DecimalFormat df = new DecimalFormat(“0.000″) ; System.out.println( “Rounded number = “ df.format( numberToRound ) ) ; //This only works with Java runtime 1.5 or later System.out.println(String.format(“Rounded number = %.3f”, numberToRound)); } public static void main(String[] args) { NumberUtil util = new NumberUtil(); util.roundNumber(1.23856); }}
The output of either one of the above rounding methods is:“Rounded number = 1,239″
Java Codes »
Since strings are immutable, you may want to use the StringBuilder class if you’re going to alter the String in the code. The StringBuilder class can be seen as a mutable String object which allocates more memory (or deallocates if told so by using the trimToSize() method) when it’s content is altered.It should be said early that the StringBuilder class is not thread-safe which means that it doesn’t automatically handle synchronization of access to the object by different threads as opposed to the StringBuffer class.If this object needs to be …
Java Codes »
This code snippet shows how to find out the minimum and maximum values of the byte datatype.To get the minimum value you simply call the static variable MIN_VALUE of the Byte class, and MAX_VALUE for the maximum value.
/* * Main.java * * @author www.javabout.com */public class Main { /* * This method displays the minimum and maximum values of the byte datatype. */ public void displayMinAndMaxValuesOfByte() { System.out.println(Byte.MIN_VALUE); System.out.println(Byte.MAX_VALUE); } /** * @param args the command line arguments */ public static void main(String[] args) { new Main().displayMinAndMaxValuesOfByte(); }}
The min and max values of a byte are:-128127
Java Codes »
The class ‘Float’ is a wrapper class for the datatype float, and it has two static variables that contains the minimum and maximum values of the float datatype: MIN_VALUE and MAX_VALUE.You can print out the min and max values of the float datatype by specifying these variables as arguments to System.out.
/* * Main.java * * @author www.javabout.com */public class Main { /* * This method displays the minimum and maximum values of the datatype float. */ public void displayMinAndMaxValuesOfFloat() { System.out.println(Float.MIN_VALUE); System.out.println(Float.MAX_VALUE); } /** * @param args the command line arguments */ public static void main(String[] args) { new Main().displayMinAndMaxValuesOfFloat(); }}
The output from the executed code will …
