This website uses cookies for visitor traffic analysis. By using the website, you agree with storing the cookies on your computer.More information

JavaFX + Beagleboard in Action (NightHacking)

See JavaFX on a Beagleboard in Action:

NightHacking/ tour

A nice application.

Joomla/RSForms post request to Java Servlet

Maybe this information is useful for other Joomla/RSForms users or for developers who want send a post request from php to anywhere :)

I had the following configuration in my dev environment:

Joomla together with RSForms (free version)

I love to use this combination because it is easy and just works. With RSForms I created some forms like User registration. This tool allows you to send emails to the registered user and it stores the registration automatically in the database. It's really useful.

In my case, the user registration was not real-time because I got an email and had to create a user account manually. This is web 0.1.

Well, I decided to push the whole process to web 2012. My plan was to create a simple Java servlet that creates my user accounts. This servlet should be called from the RSForms component, with a simple POST request. I don't like SOAP overhead, so it was no option for me. Of course, my servlet is a service :)

BTW, I wouldn't change the RSForms component because it should work as it was. The component has a nice feature that allows you to configure custom scripts for loading and processing.

I started with the configuration and added following process script:

$url = 'https://tomcatvm/services/User';
 
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

$data = array();
$data[] = 'language=en';

foreach ($_POST['form'] as $post => $value)
{
    if (is_array($value))
    {
        foreach ($value as $post2 => $value2)
        {
            $data[] = $post.'[]='.urlencode($value2);
        }
    }
    else
    {
        $data[] = $post.'='.urlencode($value);
    }
}

curl_setopt($ch, CURLOPT_POSTFIELDS, implode('&', $data));
 
$data = curl_exec($ch);

if (curl_errno($ch))
{
    JUtility::sendMail($form->emailfrom,$form->emailfromname,
    'support@sibvisions.com','Service error',
    'Service call failed! ('.curl_error($ch).')',$form->emailmode,null,null,null);
}

curl_close($ch);

The code is written in php, but it is not rocket science. It simply sends a post request to the configured URL. The important thing is that all form parameters were used as parameters for the request, because the service needs the entered values!

That's all for Joomla/RSForms. During development I thought that the process script will be executed only once per form but that was not true. It was executed after loading and everytime when the user submits the form. If the forms had validation errors, the process script was executed more than once (with and without valid inputs).

I changed the php script a little bit and moved the process execution after the validation. It depends on your RSForms version, but in my case the original script (/plugins/content/mosforme.php) looks like:

...
eval($form->script_process);

if(!empty($processform)){
...

and the new script:

if(!empty($processform)){
...
    if(!empty($_SESSION['formmsg'])){
    ...
    // store it in the db
    ...
    eval($form->script_process);
    ....
}

But it is also possible to use the original script without modification.

The other side was the servlet... It's to simple to show you the whole source, only a snippet:

@Override
public void doPost(HttpServletRequest pRequest, HttpServletResponse pResponse) throws ServletException
{
    String sParamFirstName;
    String sParamLastName;
    String sParamEmail;

    try
    {
        sParamFirstName = getParameter(pRequest, "firstname");
        sParamLastName  = getParameter(pRequest, "lastname");
        sParamEmail     = getParameter(pRequest, "email");
        sParamLanguage  = getParameter(pRequest, "language");
    ...