Add Apache MyFaces Tomahawk dependencies with Maven

Repository:

<repository>
  <id>apache-repo</id>
  <name>apache-repo</name>
  <url>http://myfaces.zones.apache.org/dist/maven-repository</url>
</repository>
 
<repository>
  <id>ibiblio</id>
  <name>ibiblio</name>
  <url>http://www.ibiblio.org/maven2</url>
</repository>

Dependencies:

<dependency>
  <groupId>org.apache.myfaces.core</groupId>
  <artifactId>myfaces-impl</artifactId>
  <version>2.2.0</version>
  <scope>compile</scope>
  <type>jar</type>
</dependency>
 
<dependency>
  <groupId>org.apache.myfaces.core</groupId>
  <artifactId>myfaces-api</artifactId>
  <version>2.2.0</version>
  <scope>compile</scope>
  <type>jar</type>
</dependency>
 
<dependency>
  <groupId>org.apache.myfaces.tomahawk</groupId>
  <artifactId>tomahawk</artifactId>
  <version>1.1.14</version>
  <scope>compile</scope>
  <type>jar</type>
</dependency>

How to use Server Side Includes

Server Side Includes (SSI) is a server-side scripting language which runs on Apache (with mod_ssi), some Java Application Servers (see Server Side Include in GlassFish) and also on Microsoft Internet Information Services (IIS). Here are a few examples of how to use SSI:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Server Side Includes</title>
    <style type="text/css">
      * { margin: 0; padding: 0; border: 0; }
    </style>
  </head>
  <body>  
    <!-- http://httpd.apache.org/docs/2.0/howto/ssi.html -->
 
    <!--#set var="name" value="Benny Neugebauer" --> 
    <!--#set var="text" value="My name is ${name}." --> 
    <!--#set var="accountBalance" value="\$72.72" --> 
 
    <p>
      <!--#echo var="name" --><br/>
      <!--#echo var="text" --><br/>
      My account balance is: <!--#echo var="accountBalance" -->
      <br/><br/>
      <!--#if expr="${name} == 'Benny Neugebauer'" -->
      <b>Hey Benny!</b>
      <!--#else -->
      <em>Who are you??</em>
      <!--#endif --> 
      <br/>
      <!--#if expr="{$name} = /[a-zA-Z]/" -->
      Your name contains just letters. Yay!
      <!--#endif -->
      <br/><br/>
    </p>
 
    <p>
      <!-- Print some CGI variables... -->
      <!--#echo var="HTTP_USER_AGENT" --><br/>
      <!--#echo var="SERVER_NAME" --><br/>
      <!--#echo var="REMOTE_ADDR" --><br/>
    </p>
  </body>
</html>

Get Filename without Extension in Java

You can get the name of a file without it’s extension in Java by using the FilenameUtils of Apache Commons IO:

1
2
3
4
5
6
7
8
9
10
11
12
import java.io.File;
import org.apache.commons.io.FilenameUtils;
 
public class NewMain
{
  public static void main(String[] args)
  {
    File file = new File("C:/This/is/a/test.txt");
    String fileNameWithOutExtension = FilenameUtils.removeExtension(file.getName());
    System.out.println(fileNameWithOutExtension); // prints 'test'
  }
}

Another approach without using external libraries is:

1
2
3
4
5
6
File file = new File("C:/This/is/a/test.txt");
String fileNameWithOutExtension = file.getName();
int index = fileNameWithOutExtension.lastIndexOf('.');
if (index != -1)
  fileNameWithOutExtension = fileNameWithOutExtension.substring(0, index);
System.out.println(fileNameWithOutExtension); // prints 'test

How to count occurrences of a character in a String with Java?

Code sample:

public class CharacterCounter
{
  public static int countOccurrences(String find, String string)
  {
    int count = 0;
    int indexOf = 0;
 
    while (indexOf > -1)
    {
      indexOf = string.indexOf(find, indexOf + 1);
      if (indexOf > -1)
        count++;
    }
 
    return count;
  }
}

Method call:

int occurrences = CharacterCounter.countOccurrences("l", "Hello World.");
System.out.println(occurrences); // 3

You can also count the occurrences of characters in a string by using the Apache commons lang library with the following one-liner:

int count = StringUtils.countMatches("a.b.c.d", ".");

If you are using the Sping Framework then you can do:

int occurance = StringUtils.countOccurrencesOf("a.b.c.d", ".");

If you want to be really smart (and don’t want to use a loop), then you can also use this one here:

int count = string.length() - string.replace(find, "").length();

Beispiel für Apache Ant Build Script

In letzter Zeit habe ich viel mit Apache Ant Build-Skripten zu tun. Bei richtiger Konfigurationen bringt Apache Ant einen erheblichen Vorteil bei der Erstellung von Java-Projekten, unabhängig von Betriebssystem und IDE. Um Anfängern den Einstieg in Ant zu erleichtern, habe ich eine Vorlage für eine build.xml mitsamt build.properties erstellt, die beliebig erweitert werden kann.

In NetBeans kann man mit einem Java Free-Form Project das eigene Build-Skript benutzen.
Beispiel für Apache Ant Build Script weiterlesen

HTTP POST Beispiel mit Apache HttpClient in Java

Mit Java und der Apache HttpClient-Bibliothek Version 4.1.1 habe ich einen Web-Client geschrieben, der eine „email“-Adresse als HTTP POST Parameter an eine PHP-Webseite übergibt, welche daraufhin die empfangene Email-Adresse in einer Textdatei speichert. Der Code dient nur als Beispiel.
HTTP POST Beispiel mit Apache HttpClient in Java weiterlesen