Archive for the 'Coding' Category

Download sources of dependencies with maven

Maven is an amazing piece of software, despite all the criticism it has endured it is fascinating to me. However there is one short line that has made life so much easier for me:

mvn -Declipse.downloadSources=true

This tells maven to download all associated sources of jar files in the pom.xml. Amazing, how much easier it is to set up a project with proper debugging sources etc. There is even a cool eclipse integration available over at codehaus.org.

How to migrate from drupal to wordpress (and clean up all the nasty leftovers)

So, after three years of Drupal, I finally got rid of it. By the last version of Drupal (it was version 5.1) there was just too much stuff in there I didn’t need and lots of features missing that make up a first class blog management system or BMS (*omg* I so created a new word!!! – oh wait, someone came up with that already, whatever). I wanted to change to Wordpress. Let me show you how I did it…

Continue reading ‘How to migrate from drupal to wordpress (and clean up all the nasty leftovers)’

Eclipse WTP and Tomcat Tutorial

If you are not interested in all the technical details of Servlet development in eclipse and tomcat, and you just want a quick introduction on how to install an Eclipse based Web development environment with all the bells and whistles and create a quick Servlet, then this screencast is for you.

Actually i just wanted to play around with Wink, which is totally awesome. Maybe I will create a more sophisticated version of this tutorial later. Sorry for the messy audio and some content glitches, but I’m sure it will be helpful to some people anyways.

Arcor Webbill Reader v0.2

Okay, okay ich habs am Wochenende nicht geschaft so viel hinzubekommen wie ich mir vorgenommen hatte. Dafür gibts heute eine neue Release des Arcor Webbill Readers.

Da ich über Andrejs Qualen mit dem Eclipse Visual editor gelesen habe, dachte ich mir, ich muss das gute Stück auch mal ausprobieren. Heraus kam dabei die GUI Version vom Webbill Reader. Er liest wie sein Vorgänger die CSV Dateien des Arcor Webbill Frontends ein und präsentiert die Kosten getrennt nach MSN. Jedoch wird als zusätzliche Info nun auch angegeben in welches Netz man am meisten telefoniert hat. Ganz sinnvoll, wenn man seine Telefontarife feintunen möchte :-)

Starten kann man ihn mittels der Kommandozeile java -jar webbill.jar [csvfile] der Sourcecode liegt natürlich bei und unterliegt der GPL.

Download

P.S.: Den Visual Editor fand ich zwar auch viel zu langsam und den code nicht besonders schön, aber gemessen an den Visual Editors die ich sonst so kenne, fand ich ihn gar nicht so übel.

Arcor Webbill tool

Da ich im letzten Monat der Telekom entsagte und zu Arcor wechselte, komme ich nun endlich auch mal in den Genuss eines fortschrittlichen Rechnungssystems. Die Rede ist von Arcors Webbill. Diese lässt einen nämlich nicht nur schon während des laufenden Monats einen Blick auf die kommende Rechnung werfen, sondern bietet auch noch eine Menge Zusatzfeatures. Beispielsweise den Download der Rechnungsdaten per CSV. Da an meinem Anschluss drei Personen hängen, die jeweils eine eigene Nummer benutzen, wollte ich nun die CSV Daten in Excel sortieren um rauszufinden, was wer vertelefoniert hat. Eine mühsame Aufgabe, grade wenn man kein Excel Fan ist. Nun gut, also Eclipse hochgefahren und in 20 Minuten ein kleines Programm geschrieben:

WebBillReader.java

Das Programm erfordert ein JDK >= 1.5 zum kompilieren (zum ausführen des kompilierten Programmes müsste eigentlich ein 1.4er JDK reichen).
Es wird mittels

java WebBillReader Rechnung.csv

aufgerufen und erzeugt Ausgaben der Form:

Number 0815 1234567 has a bill of: 25.7624u20AC
Number 0815 7456321 has a bill of: 15.6254u20AC
Number 0815 1982734 has a bill of: 0.0388u20AC
Total: 41.4266u20AC

Liest also die MSN-Anschlüsse und deren Verbrauch aus und stellt ihn inklusive der Mehrwertsteuer dar.
Viel Spaß damit.

Den Sourcecode gibt es hier
Das ausführbare Programm hier

Tomcat Tutorial: HelloWorld for Complete Fools [English]

The following tutorial was translated into English from the German original on this site, which is also available here. Please excuse me for my poor english skills, I am not a native speaker.

Update 26.02.2007: Note, that this Article, though technically still relevant and valid, is not up to date with the progress of the eclipse platform. I created a screencast to reflect the changes that took place and to show you development with the Eclipse WTP. Click here for the article.

I know quite a number of java developers who, despite their good knowledge of the java platform, somehow failed to successfully deploy a new Servlet to a Apache Tomcat installation. I stress, that they did not fail because of their inferior skills, but because of the fact, that there is no comprehensive
resource out there that explains the Tomcat deployment procedure:

  • in a nutshell
  • with pictures
  • without going into details that aren’t of direct interest

So, without much further ado, I will try to explain how to install tomcat, create a deployment structure using Eclipse and writing a simple HelloWorld Servlet.

In theory, deploying a Servlet is a really straightforward task – that is, if you know somebody who is willing to introduce you into the deployment mysticisms of the Apache Tomcat development. Unfortunately, I knew nobody who was really adroit with that. So I spent a really long time running into every single ambush Tomcat has to offer for blue eyed newbies. Hence I can rightly say, that Tomcat’s configuration is a pain in the rear if you don’t have an expert person who guides you through your very first steps. Even if you know your Google search words cold, you won’t find a single document that makes your task seem trivial.

So I figured, it would be best for newbies and the like, if you could just follow an example with pictures in it. In the following tutorial, I will try to explain the deployment procedure from the very beginning in step-by-step fashion to build a standard “Hello World” Servlet. To accomplish this, I will use the Eclipse IDE and Eclipse specific plugins, but I hope that one can understand the general proceeding that is necessary for other IDEs as well. I will assume that you are familiar with the installation of eclipse and eclipse plugins.

The following software will be neccessary to follow the tutorial:

The Server

Especially for Windows users it’s pretty easy to install Tomcat, because there it has a straightforward installer script that rids you of most of the configuration work. Everything you have to do is set the Administrator password (and remember it!) and the installation directory (and remember it!).

The Plugin

The Sysdeo Tomcat Plugin is our best friend. After you have installed it, you will notice these buttons and menues in your IDE.

The left button starts Tomcat, the middle button stops it, the right button restarts Tomcat. Right now, they won’t work if you press them. To enable them, you first need to configure a few things. Go to the menu “Window-> Preferences” there, you will see this dialogue box.

Enlarge

Now you need to go to the menu item “Tomcat” and select your Tomcat version. At the time of writing that would be the 4.1 5.x version. Now we adjust the field “Tomcat Home” to point to the install directory that we selected before at install time. The Sysdeo plugin now knows where to look for the server.xml file and automatically assumes the standard path to look for the file. Eclipse is now able to manage this configuration file, i.e. add a new <context> for a new application.


The Tomcat servlet container, features multiple user roles, such as the “Administrator” that we have already added to the user configuration upon installation. All users and passwords are found (in plain text, so take care to handle this safely) in the conf/tomcat-users.xml file. We now need to add another user called “Manager” that was not added to the file at installation time. Therefor we scroll down the menu point “Tomcat” and click the item “Tomcat Manager App”. Now we can add a username and a password for the manager. Subsequently we click on “Add user to tomcat-users.xml” and leave the configuration menu.


Enlarge
Now we are ready to test start our Tomcat server. Click on the start button in the Tomcat menu and watch the console output. If Tomcat boots up without any stacktraces open your browser, and try to open the following address http://localhost:8080/ . If you see an image
that is similar, to the one below everything is working okay. If it doesn’t you might want to Google on what might have caused the stacktrace.

Hello World

For now, we stop out Tomcat server and take a look at our very own, first servlet. First, we need to open a new project in the navigator window.


In the project window we select “Tomcat Project”…

…click on the Next button and call our new project “Hello World”


After a click on the next Button we now can adjust under which URI (Uniform Resource Identifier) we want our new application to respond to our requests. To illustrate what part of the URL this is, just take a look at the fat part in the following address: http://localhost:8080/HelloWorld/hello.
The default adjustments on the other controls should be fine by now, so we don’t bother to adjust them. They should look similar to following image.


Enlarge

If we now “Finish” the Wizard, we can see, that Eclipse has created a whole bunch of new directories. It should look similar to this:


Now we can create a new class named HelloServlet in the directory WEB-INF/src. Now we can copy and paste following code into this class.

import java.io.*;

import javax.servlet.http.*;
import javax.servlet.*;

public class HelloServlet extends HttpServlet {
  public void doGet (HttpServletRequest req,
                                         HttpServletResponse res)
        throws ServletException, IOException
  {
        PrintWriter out = res.getWriter();
        out.println("Hello, Brave new World!");
        out.close();
  }
}

Our View should now look like this:



Enlarge
Unfortunately we are not yet finished. We still need to create the web.xml descriptor, which contains certain elements of configuration specific to our application and the server behaviour. So we create the file web.xml in the directory WEB-INF (Note: not in the directory WEB-INF/src !!!). For our simple application, the following parameters should be fine.

<!DOCTYPE web-app PUBLIC
  '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN'
  'http://java.sun.com/dtd/web-app_2_3.dtd'>
<web-app>
  <servlet>
    <servlet-name>hello</servlet-name>
    <servlet-class>HelloServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>hello</servlet-name>
    <url-pattern>/hello</url-pattern>
  </servlet-mapping>
</web-app>

What follows now is a little explanation of what we just copied and pasted into web.xml . The Doctype is an xml specific item that tells the xml parser where to look
for the dtd consistency rules for our xml document. The dtd ensures that no wrong parameter combinations are entered. The main tag &lt;web-app&gt; contains all preferences for our servlet. The &lt;servlet&gt; tag basically contains the name (&lt;servlet-name&gt;) that will be used throughout the xml document to reference our servlet and its linked class (&lt;servlet-class&gt;) . The tag &lt;servlet-mapping&gt; is responsible for telling the server, to what document name in the URL our application should respond to. Note, that the correct order of these tags has to be retained to form a valid web.xml descriptor. If we followed all steps correctly, we should now be able to fire up Tomcat again. Our workspace should now look like that:

Enlarge

So, if we now enter the right combination of URI and resource name into the address bar of our browser (in our case this would actually be http://localhost:8080/HelloWorld/hello) we should be able to see the output from our very first servlet. Congratulations, now you are in the game ;-)

Enlarge

Oh, yeah – if you want to enable your Tomcat engine to serve JSPs too, you just have to add the following lines to the web.xml file and put the jsp files in the top level directory of your eclipse project.

    <servlet>
        <servlet-name>jspAssign</servlet-name>
        <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
        <init-param>
            <param-name>logVerbosityLevel</param-name>
            <param-value>WARNING</param-value>
        </init-param>
        <init-param>
            <param-name>fork</param-name>
            <param-value>false</param-value>
        </init-param>
        <load-on-startup>3</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>jspAssign</servlet-name>
        <url-pattern>/*.jsp</url-pattern>
    </servlet-mapping>

For further reading, I recommend following Sites:

Tomcat 4.1 Documentation
Advanced JSP and Servlet Tutorial

Tomcat Tutorial: HelloWorld for Complete Fools ;-)


This Tutorial is also available in English here

Update 26.02.2007: Obwohl die Grundlagen dieses Artikels immer noch gelten und richtig sind, gibt es mittlerweile einige Entwicklungen an der Eclipse Platform die es sich lohnt anzuschauen. Insbesondere die Eclipse Web Tools Platform. Ich habe zu diesem Zweck einen Screencast erstellt der eine Einführung in das Entwickeln mit WTP gibt. Ihr findet ihn in diesem Artikel.

Ich kenne einige erfahrene Java Entwickler, die bereits daran gescheitert sind ein eigenständiges Servlet Projekt auf Apache Tomcat Basis einzurichten. Entwickler, die komplexeste Neuronale Netze wie im Schlaf entwickeln und die EJB Spezifikation rückwärts beten können, haben hier schon ihre Probleme gehabt. Man kann also mit Recht sagen, daß es nicht selbsterklärend ist ein Servlet auf dem Tomcat Container zur Mitarbeit zu überreden. .WAR Archive deployen an sich kann jeder, aber ganz anders sieht das aus wenn ein
brandneues Projekt in einen eigenen Context gesetzt werden soll und die Verzeichnisstruktur entsprechend der Spezifikation erstellt werden muss et cetera, et cetera, et cetera. Als Neuling muss man sich also erst Recht nicht schämen, wenn man Anfangsproblemchen mit Servlets hat.

Um diese Anfangsschwierigkeit zu mildern habe ich dieses Tutorial geschrieben.

Dabei ist es eigentlich höllisch einfach so ein Servlet zum laufen zu bringen – vorausgesetzt man kann bei jemandem Nachfragen, der bereits die hohe Kunst der Tomcat Administration beherrscht. Ich kannte niemanden, der das konnte und habe ewig lange Zeit damit verbracht jeden Fehler den ein Anfänger begehen kann zu begehen. Daher weiß ich wovon Ich spreche wenn ich sage, die Konfiguration von Tomcat ist ohne Unterstützung einer kundigen Person, selbst unter Zuhilfenahme von Google alles andere als trivial.

In diesem Tutorial werde ich versuchen so detailliert wie möglich zu erklären wie man step-by-step vorgeht um ein simples “Hello World” Servlet unter Tomcat 4 zum laufen zu bekommen. Für das Tutorial verwende ich Eclipse und Eclipse Spezifische Plugins. Ich hoffe dennoch, daß man sich aus den Darstellungen die generelle Vorgehensweise abschauen kann um das gleiche auch für andere Entwicklungsumgebungen zu bewerkstelligen. Es wird hierbei vorausgesetzt, daß die Installation von Eclipse und der Umgang
mit Plugins geläufig sind.

Folgende Software wird also zunächst benötigt:

Der Server

Tomcat zu installieren ist besonders für Windows Benutzer recht einfach, da ein guter Installer alle Konfigurationsarbeit abnimmt. Alles was man noch zu tun hat ist, sich das Admin Passwort und Verzeichnis zu merken in das Tomcat installiert wurde.

Das Plugin

Das Sysdeo Tomcat Plugin ist unser Freund und Helfer. Nachdem es installiert wurde erscheinen prompt folgende Buttons und Menüs in der Bedienleiste von Eclipse.

Die drei Kater bedeuten in der Reihenfolge von Links nach Rechts Start, Stop und Neu starten. Noch funktionieren sie nicht, denn wir müssen zunächst noch einige Einstellungen vornehmen. Hierzu gehen wir in das Menü “Window-> Preferences” dort öffnet sich nun folgender Dialog

Vergrössern

Im Menüpunkt Tomcat tragen wir nun ein, welche Version von Tomcat wir benutzen. Zum heutigen Zeitpunkt ist das die 4.1er oder die 5er Version. In das Feld “Tomcat
Home” geben wir das Verzeichnis ein in das Tomcat installiert wurde. Der Pfad zur Konfigurationsdatei server.xml wird nach der Eingabe des
“Tomcat Home” automatisch ergänzt. Eclipse wird fortan für uns diese Konfigurationsdatei verwalten, in der zum Beispiel der &lt;context&gt; einer Applikation gesetzt wird.

Ein Tomcat Server besitzt verschiedene Benutzerrollen. Einen besonderen Benutzer, den “Administrator”, haben wir bereits bei der Installation angelegt. Seine Benutzername und sein Passwort finden sich in der Datei tomcat-users.xml. Ein weiterer Benutzer ist der Benutzer “Manager”, er ist nach der normalen Installation noch nicht angelegt und muss manuell hinzugefügt werden. Diese Arbeit nimmt uns Eclipse allerdings ab, wenn wir nun die Untermenüs von Punkt Tomcat aufklappen und dort den Punkt “Tomcat Manager App” aufrufen. Hier kann nun ein Benutzername und ein Passwort für den Manager festgelegt werden. Anschließend auf den Button “Add user to tomcat-users.xml” clicken und dir Konfiguration verlassen.


Vergrössern

Jetzt können wir zu Probehalber Tomcat starten. Hierzu clicken wir auf den oben genannten Knopf auf dem der Tomcat Kater abgebildet ist. Wenn Tomcat ohne jegliche Stacktraces hochfährt und wir uns jetzt mit dem Browser auf http://localhost:8080/ verbinden können und unten stehende Startseite sehen ist alles in Ordnung.

Hello World

Jetzt beenden wir Tomcat allerdings erstmal wieder. Und widmen uns unserem ersten Servlet. Hierzu erzeugen wir ein neues Projekt im Navigator Fenster

Im Projektfenster wählen wir jetzt Tomcat Projekt aus…

…clicken auf “Next” und benennen unser Projekt.”HelloWorld”

Nach einem click auf “Next” können wir nun angeben unter welcher
URI (Uniform Resource Identifier) wir unsere Anwendung im Browser künftig ansprechen wollen. Dies entspricht dem im Folgenden, dick gedruckten Teil in
unserer Adresse: http://localhost:8080/HelloWorld/hello Wir belassen alles so wie es ist. Daß bedeutet wir lassen Eclipse die
XML Dateien von Tomcat verwalten und legen für unsere Applikation fest, daß sie nicht in einem Unterverzeichnis sondern im Hauptverzeichnis abgerufen werden soll. Es sollte also so aussehen wie auf folgender Darstellung.

Vergrössern

Wenn wir nun den Wizard mit “Finish” beenden, so sehen wir das Eclipse uns gleich eine ganze Sammlung an Verzeichnissen angelegt hat. Es sollte wie
folgt aussehen.

Im Verzeichnis WEB-INF/src legen wir nun eine neue Klasse an, die wir HelloServlet nennen. Der Code, für diese Klasse sieht folgendermaßen aus.

import java.io.*;

import javax.servlet.http.*;
import javax.servlet.*;

public class HelloServlet extends HttpServlet {
  public void doGet (HttpServletRequest req,
                                         HttpServletResponse res)
        throws ServletException, IOException
  {
        PrintWriter out = res.getWriter();
        out.println(”Hello, Brave new World!”);
        out.close();
  }
}

Unsere Ansicht sieht also nun so aus.

Vergrössern

Leider ist es damit allerdings noch nicht getan. Zunächst müssen wir noch den web.xml Deskriptor anlegen der u.a. angibt unter welchem Namen wir das Servlet über den Browser ansprechen können. Die Datei
web.xml muss im Verzeichnis WEB-INF angelegt werden ( NICHT IM VERZEICHNIS WEB-INF/src !!!). Für
unsere Anwendung sollte sie folgende Einträge enthalten.

<!DOCTYPE web-app PUBLIC
  '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN'
  'http://java.sun.com/dtd/web-app_2_3.dtd'>
<web-app>
  <servlet>
    <servlet-name>hello</servlet-name>
    <servlet-class>HelloServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>hello</servlet-name>
    <url-pattern>/hello</url-pattern>
  </servlet-mapping>
</web-app>

Hierzu ist zunächst eine kleine Erklärung notwendig. Der Doctype tag gibt an wo sich die Überprüfungsregeln zu unserem xml Dokument finden. So kann man sicher gehen, daß sich keine falschen Parameterkonstellationen einschleichen. Der Haupt Tag <web-app> enthält alle sonstigen Einstellungen. Im Tag Servlet wird vor allem der Name (<servlet-name>) mit dem das Servlet innerhalb des XML Dokuments fortan benannt wird, mit der Klasse (<servlet-class>) verknüpft. Im Tag <servlet-mapping> geht es darum dem Server mitzuteilen welchen Dokumentnamen unser Servlet bekommen soll, wenn man es schließlich über die Browserzeile aufruft. Wichtig ist hierbei, daß man die Reihenfolge in der die Tags geschrieben sind beachtet. Tomcat achtet penibel genau, daß die Reihenfolge eingehalten wird. So muß &lt;servlet&gt; immer vor dem &lt;servlet-mapping&gt; definiert worden sein, und &lt;servlet-name&gt; muss in &lt;servlet-mapping&gt; vor &lt;url-pattern&gt; stehen. Wenn wir jetzt alle Schritte richtig augeführt haben, können wir endlich Tomcat starten. Unsere Umgebung sollte nun folgendermaßen aussehen.

Vergrössern

Wenn wir jetzt die Kombination aus der URI und dem <servlet-mapping> in der Addresszeile des Browsers eingeben, also in unserem Fall die Adressehttp://localhost:8080/HelloWorld/hello sollten wir nun endlich die Ausgabe unseres Servlets sehen können. Hurrah! ;-)


Vergrössern

Wer unter Tomcat auch JSPs verwenden will, der kann einfach folgende Zeilen zur web.xml datei hinzufügen und anschließend die JSP Dateien im Hauptverzeichnis des eclipse projektes ablegen.

    <servlet>
        <servlet-name>jspAssign</servlet-name>
        <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
        <init-param>
            <param-name>logVerbosityLevel</param-name>
            <param-value>WARNING</param-value>
        </init-param>
        <init-param>
            <param-name>fork</param-name>
            <param-value>false</param-value>
        </init-param>
        <load-on-startup>3</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>jspAssign</servlet-name>
        <url-pattern>/*.jsp</url-pattern>
    </servlet-mapping>

Die weitere Entwicklung von Servlets unter Eclipse ist übrigens sehr angenehm. Die meisten einfachen Codeänderungen, werden beim speichern in den Servlet Container übernommen, so daß man nicht jedes Mal den Server neu starten oder ein neues Deployment anfangen muss. Viel Spaß mit der weiteren Entwicklung.




Bad Behavior has blocked 1528 access attempts in the last 7 days.

FireStats iconPowered by FireStats