October 2013 Posts

Changing JAX-WS Webservice Client’s Target Endpoint Url

Let say you have a wsdl that you get from an url, but you want to fire the webservice generated from it to a different url, see my code below for example,

<service name="TestingService">
	<port name="TestingServicePort" binding="tns:TestingServicePortBinding">
		<soap:address location="http://localhost:8084/WSDLTest/TestingService" />
	</port>
</service>

as you can see, my wsdl is pointing at “http://localhost:8084/WSDLTest/TestingService”. But what if i want to fire my webservice client into another url, without changing the original wsdl. For example, my new url would be http://192.168.0.101/WSDLTest/TestingService.

Well, it’s actualy quite easy. All you need to do is only casting the service interface into interface BindingProvider, and add a new url property. This is what my code would look like,


             TestingService_Service tss = new TestingService_Service();
             TestingService testingService = tss.getTestingServicePort();
             ((BindingProvider) testingService).getRequestContext()
                .put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "http://192.168.0.101/WSDLTest/TestingService");

If you fire your service again, it will point to a new url (http://192.168.0.101/WSDLTest/TestingService) instead of the old one (http://localhost:8084/WSDLTest/TestingService).

Google+

How to Create A Simple Captcha Using Java Servlet

On this article im going to create a simple image captcha that will show a 5-random alphanumeric letters, im using SimpleCaptha library to help me creating images and validating user’s input.

First is a simple java servlet, it will build random images and store its value on httpsession.

package com.edw.captcha;

import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import nl.captcha.Captcha;
import nl.captcha.backgrounds.GradiatedBackgroundProducer;
import nl.captcha.servlet.CaptchaServletUtil;
import nl.captcha.text.renderer.DefaultWordRenderer;

public class EdwCaptcha extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        List<Color> colors = new ArrayList<Color> ();
        colors.add(Color.black);
        colors.add(Color.red);
        
        List<Font> fonts = new ArrayList<Font>();
        fonts.add(new Font("Arial", Font.ITALIC, 40));
        
        Captcha captcha = new Captcha.Builder(120, 50)
                .addText(new DefaultWordRenderer(colors, fonts))
                .addBackground(new GradiatedBackgroundProducer(Color.white, Color.white))
                .gimp()
                .addBorder()
                .build();

        // display the image produced
        CaptchaServletUtil.writeImage(response, captcha.getImage());

        // save the captcha value on session
        request.getSession().setAttribute("simpleCaptcha", captcha); 
    }
}

And now im registering my servlet on my web.xml,

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    
    <servlet>
        <servlet-name>Captcha</servlet-name>
        <servlet-class>com.edw.captcha.EdwCaptcha</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Captcha</servlet-name>
        <url-pattern>/Captcha.jpg</url-pattern>
    </servlet-mapping>    
</web-app>

Here is the result image generated from my servlet,

You can validate the submitted captcha values by comparing it with the values that you store in httpsession.

Google+