mkyong

mkyong

Software Engineer • Writer • Dad

20+ years hands-on, writing about Java, Spring, and AI. Every code example here is tested.

1,960 posts

Posts by mkyong

Since JDK 1.6, Java developers are able to access network card detail via NetworkInterface class. In this example, we show you how to get the localhost MAC address in Java. App.java – Get MAC Address via NetworkInterface.getByInetAddress() package com.mkyong; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; public class App{ public static void main(String[] args){ […]

Read more How to get MAC address in Java

In Java, you can use InetAddress.getLocalHost() to get the Ip Address of the current Server running the Java app. package com.mkyong; import java.net.InetAddress; import java.net.UnknownHostException; public class Test { public static void main(String[] args) { InetAddress ip; try { ip = InetAddress.getLocalHost(); System.out.println("Current IP address : " + ip.getHostAddress()); } catch (UnknownHostException e) { e.e.printStackTrace(); […]

Read more How to get Server IP address in Java

In this article, we show you how to implement container authentication with JAX-WS, under Tomcat 6.0. In this way, the authentication is declarative rather than programmatic like this – application authentication in JAX-WS. And Tomcat implement the container authentication via security realm. At the end of this article, the deployed web service will authenticate user […]

Read more Container Authentication with JAX-WS – (Tomcat version)

One of the common way to handle authentication in JAX-WS is client provides “username” and “password”, attached it in SOAP request header and send to server, server parse the SOAP document and retrieve the provided “username” and “password” from request header and do validation from database, or whatever method prefer. In this article, we show […]

Read more Application Authentication with JAX-WS

In Java web service development environment, developer are always generate a test certificate using keytool. While doing client testing, often times, the web service test client will hits following error messages : java.security.cert.CertificateException: No name matching localhost found SunCertPathBuilderException: unable to find valid certification path to requested target Here’s a source code, that i copied […]

Read more How to bypass certificate checking in a Java web service client

1. Problem Set up a localhost Tomcat to support SSL and deployed this web service for testing. While connecting to the deployed web service over SSL connection via this URL : https://localhost:8443/HelloWorld/hello?wsdl, it hits Terminal javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target Caused by: sun.security.validator.ValidatorException: PKIX […]

Read more SunCertPathBuilderException: unable to find valid certification path to requested target

Problem Configured Tomcat to support SSL and deployed this simple hello world web service. And use following client connect to the deployed web service over SSL connection : package com.mkyong.client; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.Service; import com.mkyong.ws.HelloWorld; public class HelloWorldClient{ public static void main(String[] args) throws Exception { URL url = new URL("https://localhost:8443/HelloWorld/hello?wsdl"); QName […]

Read more java.security.cert.CertificateException: No name matching localhost found

Problem Configured Tomcat’s SSL, while starting Tomcat server, it hits following exception : 14 Disember 2010 4:18:31 PM org.apache.tomcat.util.net.jsse.JSSESocketFactory getStore SEVERE: Failed to load keystore type JKS with path c:\keystore due to Keystore was tampered with, or password was incorrect java.io.IOException: Keystore was tampered with, or password was incorrect at sun.security.provider.JavaKeyStore.engineLoad(JavaKeyStore.java:771) at sun.security.provider.JavaKeyStore$JKS.engineLoad(JavaKeyStore.java:38) at java.security.KeyStore.load(KeyStore.java:1185) […]

Read more Tomcat : java.io.IOException: Keystore was tampered with, or password was incorrect

A guide to show you how to configure Tomcat 6.0 to support SSL or https connection. 1. Generate Keystore First, uses “keytool” command to create a self-signed certificate. During the keystore creation process, you need to assign a password and fill in the certificate’s detail. $Tomcat\bin>keytool -genkey -alias mkyong -keyalg RSA -keystore c:\mkyongkeystore Enter keystore […]

Read more How to configure Tomcat to support SSL or https

Problem JSF 2.0 web application, a managed bean uses @Resource to inject the “jdbc/mkyongdb” datasource into a ds property. @ManagedBean(name="customer") @SessionScoped public class CustomerBean implements Serializable{ //resource injection @Resource(name="jdbc/mkyongdb") private DataSource ds; When deployed to Tomcat 6, it hits following error messages for the MySQL datasource configuration. com.sun.faces.mgbean.ManagedBeanCreationException: An error occurred performing resource injection on […]

Read more javax.naming.NameNotFoundException: Name jdbc is not bound in this Context

Here’s a guide to show you how to configure MySQL datasource in Tomcat 6. 1. Get MySQL JDBC Driver Get JDBC driver here – http://www.mysql.com/products/connector/ , for example, mysql-connector-java-5.1.9.jar, and copy it to $TOMCAT\lib folder. 2. Create META-INF/context.xml Add a file META-INF/context.xml into the root of your web application folder, which defines database connection detail […]

Read more How to configure MySQL DataSource in Tomcat 6

Here’s a simple Java HTTPS client to demonstrate the use of HttpsURLConnection class to send a HTTP GET request yo get the https URL content and certificate detail. P.S You may interest at this example – automate login a website with HttpsURLConnection. HttpsClient.java package com.mkyong.client; import java.net.MalformedURLException; import java.net.URL; import java.security.cert.Certificate; import java.io.*; import javax.net.ssl.HttpsURLConnection; […]

Read more Java HttpsURLConnection example

Here’s a Apache Ant template file , best use to start a project from scratch. File : build.xml <project name="ProjectName" default="dist" basedir="."> <description> Project Description </description> <!– set global properties for this build –> <property name="projectName" value="ProjectName"/> <property name="src" location="src"/> <property name="build" location="build"/> <property name="dist" location="dist"/> <property name="webcontent" location="WebContent"/> <target name="init"> <!– Create the time […]

Read more Ant Template file to build a Java Project

Here’s a guide to show you how to deploy JAX-WS web services on Tomcat servlet container. See following summary steps of a web service deployment. Create a web service (of course). Create a sun-jaxws.xml, defines web service implementation class. Create a standard web.xml, defines WSServletContextListener, WSServlet and structure of a web project. Build tool to […]

Read more Deploy JAX-WS web services on Tomcat

A complete JAX-WS SOAP-based example to show how to use Message Transmission Optimization Mechanism (MTOM) and XML-Binary Optimized Packaging (XOP) technique to send a binary attachment (image) from server to client and vice verse. Note There are ton of articles about what’s MTOM and the benefits of using it (see reference sites below), but it’s […]

Read more JAX-WS attachment with MTOM

Problem In JAX-WS development, when following service endpoint is deploying, File : HelloWorld.java package com.mkyong.ws; //Service Endpoint Interface @WebService public interface HelloWorld{ @WebMethod String getHelloWorldAsString(); } File : HelloWorldImpl.java //Service Implementation package com.mkyong.ws; @WebService(endpointInterface = "com.mkyong.ws.HelloWorld") public class HelloWorldImpl implements HelloWorld{ @Override public String getHelloWorldAsString() { //… } } It hits following error message immediately? […]

Read more Wrapper class package.jaxws.methodName is not found. Have you run APT to generate them?

Problem Deploying a JAX-WS web service on Tomcat, hits following error message : java.lang.ClassNotFoundException: org.glassfish.external.amx.AMXGlassfish Solution The JAX-WS dependency library “management-api.jar” is missing. Go here http://jax-ws.java.net/. Download JAX-WS RI distribution. Unzip it and copy “management-api.jar” to Tomcat library folder “{$TOMCAT}/lib“. Restart Tomcat.

Read more java.lang.ClassNotFoundException : org.glassfish.external.amx.AMXGlassfish

Problem Deploying a JAX-WS web service on Tomcat, hits following error message : java.lang.ClassNotFoundException: org.glassfish.gmbal.ManagedObjectManager Solution The JAX-WS dependency library “gmbal-api-only.jar” is missing. Go here http://jax-ws.java.net/. Download JAX-WS RI distribution. Unzip it and copy “gmbal-api-only.jar” to Tomcat library folder “{$TOMCAT}/lib“. Restart Tomcat.

Read more java.lang.ClassNotFoundException : org.glassfish.gmbal.ManagedObjectManager

Problem Deploying a JAX-WS web service on Tomcat, hits following error message : java.lang.ClassNotFoundException: org.jvnet.staxex.XMLStreamReaderEx Solution The JAX-WS dependency library “stax-ex.jar” is missing. Go here http://jax-ws.java.net/. Download JAX-WS RI distribution. Unzip it and copy “stax-ex.jar” to Tomcat library folder “{$TOMCAT}/lib“. Restart Tomcat.

Read more java.lang.ClassNotFoundException : org.jvnet.staxex.XMLStreamReaderEx

Problem Deploying a JAX-WS web service on Tomcat, hits following error message : java.lang.ClassNotFoundException: javax.xml.ws.soap.AddressingFeature$Responses Solution The JAX-WS dependency library “jaxws-api.jar” is missing. Go here http://jax-ws.java.net/. Download JAX-WS RI distribution. Unzip it and copy “jaxws-api.jar” to Tomcat library folder “{$TOMCAT}/lib“. Restart Tomcat.

Read more java.lang.ClassNotFoundException : javax.xml.ws.soap.AddressingFeature$Responses

Problem Deploying a JAX-WS web service on Tomcat, hits following error message : java.lang.ClassNotFoundException: com/sun/xml/ws/policy/PolicyException Solution The JAX-WS dependency library “policy.jar” is missing. Go here http://jax-ws.java.net/. Download JAX-WS RI distribution. Unzip it and copy “policy.jar” to Tomcat library folder “{$TOMCAT}/lib“. Restart Tomcat.

Read more java.lang.ClassNotFoundException : com/sun/xml/ws/policy/PolicyException

Problem Deploying a JAX-WS web service on Tomcat, hits following error message : java.lang.ClassNotFoundException: com/sun/xml/bind/v2/model/annotation/AnnotationReader Solution The JAX-WS dependency library “jaxb-impl.jar” is missing. Go here http://jax-ws.java.net/. Download JAX-WS RI distribution. Unzip it and copy “jaxb-impl.jar” to Tomcat library folder “{$TOMCAT}/lib“. Restart Tomcat.

Read more java.lang.ClassNotFoundException : com/sun/xml/bind/v2/model/annotation/AnnotationReader

Problem Deploying a JAX-WS web service on Tomcat, hits following error message : java.lang.ClassNotFoundException: com.sun.xml.ws.transport.http.servlet.WSServletContextListener Solution The JAX-WS dependency library “jaxws-rt.jar” is missing. Go here http://jax-ws.java.net/. Download JAX-WS RI distribution. Unzip it and copy “jaxws-rt.jar” to Tomcat library folder “{$TOMCAT}/lib“. Restart Tomcat. Reference WSServletContextListener JavaDoc

Read more java.lang.ClassNotFoundException: com.sun.xml.ws.transport.http.servlet.WSServletContextListener

Developing a Java web service development with JAX-WS, and publish an end point… public static void main(String[] args) { Endpoint.publish("http://localhost:8080/ws/hello", new WallStreetImpl()); } 1. Problem It hits the following error message. Exception in thread "main" com.sun.xml.internal.ws.server.ServerRtException: Server Runtime Error: java.net.BindException: Address already in use: bind … Caused by: java.net.BindException: Address already in use: bind at […]

Read more JAX-WS – java.net.BindException: Address already in use: bind

Problem Deploying a JAX-WS web service on Tomcat, hits following error message : java.lang.ClassNotFoundException: com/sun/xml/stream/buffer/XMLStreamBuffer Solution The JAX-WS dependency library “streambuffer.jar” is missing. Go here http://jax-ws.java.net/. Download JAX-WS RI distribution. Unzip it and copy “streambuffer.jar” to Tomcat library folder “{$TOMCAT}/lib“. Restart Tomcat.

Read more java.lang.ClassNotFoundException : com/sun/xml/stream/buffer/XMLStreamBuffer

In JSF 2.0, you can attach a javax.faces.event.PreRenderViewEvent system event to perform custom task before a view root (JSF page) is display. Let see a complete PreRenderViewEvent example below : 1. Managed Bean Create a normal bean, contains a method signature “public void method-name(ComponentSystemEvent event)“, later you will ask listener to call this method. In […]

Read more JSF 2 PreRenderViewEvent example

Since JSF 2.0, you can register javax.faces.event.PostConstructApplicationEvent and javax.faces.event.PreDestroyApplicationEvent system event to manipulate the JSF application life cycle. 1. PostConstructApplicationEvent – Perform a custom post-configuration after application has started. 2. PreDestroyApplicationEvent – Perform a custom cleanup task before application is about to be shut down. Note In JSF, you can’t depends on the standard ServletContextListeners […]

Read more JSF 2 PostConstructApplicationEvent and PreDestroyApplicationEvent example

As i know,there are 4 ways to pass a parameter value from JSF page to backing bean : Method expression (JSF 2.0) f:param f:attribute f:setPropertyActionListener Let see example one by one : 1. Method expression Since JSF 2.0, you are allow to pass parameter value in the method expression like this #{bean.method(param)}. JSF page… <h:commandButton […]

Read more 4 ways to pass parameter from JSF page to backing bean