Benny's Blog
Navigation: Home » Archives for Apache
11. April 2012

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>
22. März 2012

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
3. März 2012

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();
29. Juni 2011

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.

…weiterlesen

23. Juni 2011

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.

…weiterlesen

6. Februar 2011

Wer sehen möchte, auf welche Webseiten des eigenen Apache-Webservers die Besucher gerade zugreifen, der kann sich das mit apachetop anzeigen lassen.

Installation & Anwendung

1
2
3
sudo su
apt-get install apachetop -y
apachetop -f /var/log/apache2/other_vhosts_access.log

Erklärung

  1. Root-Rechte holen
  2. apachetop installieren
  3. Webseiten-Aufrufe anzeigen

Hinweis:
Die Domains müssen natürlich ihre Logausgabe in “other_vhosts_access.log” schreiben.