<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>DestaqueBlog &#187; Tutorials</title>
	<atom:link href="http://weblog.destaquenet.com/category/en/tutorials/feed/" rel="self" type="application/rss+xml" />
	<link>http://weblog.destaquenet.com</link>
	<description>Blog da equipe Destaquenet.</description>
	<lastBuildDate>Tue, 23 Nov 2010 17:06:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Emacs and Slime: Your Lisp Lab</title>
		<link>http://weblog.destaquenet.com/2010/11/18/emacs-and-slime-your-lisp-lab/</link>
		<comments>http://weblog.destaquenet.com/2010/11/18/emacs-and-slime-your-lisp-lab/#comments</comments>
		<pubDate>Thu, 18 Nov 2010 12:42:56 +0000</pubDate>
		<dc:creator>Daniel Martins</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[clisp]]></category>
		<category><![CDATA[clojure]]></category>
		<category><![CDATA[emacs]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[sbcl]]></category>
		<category><![CDATA[slime]]></category>
		<category><![CDATA[swank]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://weblog.destaquenet.com/?p=1377</guid>
		<description><![CDATA[This post goes to who&#8217;s studying Clojure (or other Lisp implementations) and is having trouble configuring Emacs. Be prepared for a huge amount of ugly workarounds. ELPA The first thing I do on a fresh Emacs installation is configure the &#8230; <a href="http://weblog.destaquenet.com/2010/11/18/emacs-and-slime-your-lisp-lab/">Continue lendo <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>This post goes to who&#8217;s studying <a href="http://clojure.org">Clojure</a> (or other <a href="http://en.wikipedia.org/wiki/Lisp_(programming_language)">Lisp</a> implementations) and is having trouble configuring <a href="http://www.gnu.org/software/emacs">Emacs</a>. Be prepared for a huge amount of ugly workarounds.</p>
<h3>ELPA</h3>
<p>The first thing I do on a fresh Emacs installation is configure the <a href="http://tromey.com/elpa">ELPA</a>, or <em>Emacs Lisp Package Archive</em>. If you don&#8217;t know what ELPA is, think of it as an <code>apt-get</code> for Emacs (with some quirks though, as discussed below).</p>
<p>You can find the installation instructions <a href="http://tromey.com/elpa/install.htm">here</a>. If everything works, now you should be able to run the command <code>M-x package-list-packages &lt;ENTER&gt;</code>. Read <a href="http://sean.wenzel.net/docs/emacs/quick_reference/">this</a> if you don&#8217;t understand how commands and shortcuts work on Emacs.</p>
<p>The list of packages available for installation via ELPA should be displayed in a secondary buffer after a few seconds.</p>
<p><span id="more-1377"></span></p>
<h3>Clojure-Mode</h3>
<p>Place the cursor over the line that describes the <code>swank-clojure</code> package, and press <code>I</code> to check it for installation. Do the same thing with <a href="http://www.emacswiki.org/ParEdit"><code>paredit</code></a>. If other packages were checked by accident, move the cursor to the correspondent lines and press <code>U</code> to undo the selection. When you&#8217;re done, press <code>X</code> to proceed with the installation.</p>
<p>Then, run the command <code>M-x slime &lt;ENTER&gt;</code>, and Emacs will ask if you want it to download the Clojure JAR files for you. Unfortunately, answering yes won&#8217;t do anything. <img src='http://weblog.destaquenet.com/wp-includes/images/smilies/icon_sad.gif' alt=':-(' class='wp-smiley' /> </p>
<p>To fix this, download the <a href="http://clojure.org/downloads">Clojure JAR files</a> manually and put them on <code>~/.clojure/</code>. Then, download the most recent <a href="http://clojars.org/repo/swank-clojure/swank-clojure/">swank-clojure JAR</a> file and put it on <code>~/.swank-clojure/</code>.</p>
<p>There you go. Restart Emacs and run the command <code>M-x slime &lt;ENTER&gt;</code> to start a Clojure REPL inside Emacs!</p>
<div id="attachment_1363" class="wp-caption aligncenter" style="width: 310px"><a href="http://weblog.destaquenet.com/wp-content/uploads/2010/11/clj-repl.png"><img src="http://weblog.destaquenet.com/wp-content/uploads/2010/11/clj-repl-300x168.png" alt="REPL Clojure" title="clj-repl" width="300" height="168" class="size-medium wp-image-1363" /></a><p class="wp-caption-text">Clojure REPL in Emacs</p></div>
<p>Remember that Slime provides many useful features like auto-completion, docstring lookup, etc. Check the <a href="http://common-lisp.net/project/slime/">Slime website</a> for more information.</p>
<h3>Other Lisps</h3>
<p>Run the following command line to install two of the most popular open source implementations of the ANSI Common Lisp standard, <a href="http://www.gnu.org/software/clisp">CLISP</a> and <a href="http://www.sbcl.org/">SBCL</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> clisp sbcl</pre></div></div>

<p>Finally, open the file <code>~/.emacs</code> and paste the following code at the end:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">;; paredit + lisp modes</span>
<span style="color: #66cc66;">&#40;</span>defvar *lisp-modes* '<span style="color: #66cc66;">&#40;</span>clojure-mode-hook lisp-mode-hook emacs-lisp-mode-hook
                       lisp-interaction-mode-hook slime-repl-mode-hook<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">;; config used in all lisp modes</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> lisp-config <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>paredit-mode t<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>show-paren-mode t<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">mapc</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>mode<span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>add-hook mode 'lisp-config<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> *lisp-modes*<span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">;; other lisp implementations</span>
<span style="color: #66cc66;">&#40;</span>eval-after-load <span style="color: #ff0000;">&quot;slime&quot;</span>
  '<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">progn</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setq</span> slime-lisp-implementations
                '<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>sbcl <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;/usr/bin/sbcl&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                  <span style="color: #66cc66;">&#40;</span>clisp <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;/usr/bin/clisp&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<h3>Swank</h3>
<p>The latest versions of Slime come with Swank servers for many popular Lisp implementations, although the package available via ELPA looks like a outdated stripped-down version of Slime. Don&#8217;t ask me why.</p>
<p>Let&#8217;s fix this. Make sure you have a CVS client installed and then run the following command line to get the latest Slime:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">cvs</span> <span style="color: #660033;">-d</span> :pserver:anonymous:anonymous<span style="color: #000000; font-weight: bold;">@</span>common-lisp.net:<span style="color: #000000; font-weight: bold;">/</span>project<span style="color: #000000; font-weight: bold;">/</span>slime<span style="color: #000000; font-weight: bold;">/</span>cvsroot <span style="color: #c20cb9; font-weight: bold;">co</span> slime</pre></div></div>

<p>Now, go to the newly created directory <code>slime</code> and copy all files from this directory over the files installed via ELPA in <code>~/.emacs.d/elpa/slime-&lt;VERSION&gt;</code>. Also remove the file <code>slime.elc</code> from the latter directory as this is a compiled version of an old file.</p>
<p>Restart Emacs. The command <code>M-x slime &lt;ENTER&gt;</code> still starts a Clojure REPL. To start a SBCL REPL, for example, run the command <code>M-- M-x slime &lt;ENTER&gt;</code>, type <code>sbcl</code> and press <code>&lt;ENTER&gt;</code>.</p>
<div id="attachment_1364" class="wp-caption aligncenter" style="width: 310px"><a href="http://weblog.destaquenet.com/wp-content/uploads/2010/11/sbcl-repl.png"><img src="http://weblog.destaquenet.com/wp-content/uploads/2010/11/sbcl-repl-300x168.png" alt="REPL SBCL" title="sbcl-repl" width="300" height="168" class="size-medium wp-image-1364" /></a><p class="wp-caption-text">SBCL REPL in Emacs</p></div>
<h4><em>&#8220;Jeez, I never saw that many workarounds in a single blog post&#8230;&#8221;</em></h4>
<p>Everything would be a lot easier if the packages distributed via ELPA were updated more often, so I expect things to get better eventually. I&#8217;ll update this post when that happens. (Fingers crossed)</p>
]]></content:encoded>
			<wfw:commentRss>http://weblog.destaquenet.com/2010/11/18/emacs-and-slime-your-lisp-lab/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Create your own save-your-ass multi-boot USB stick</title>
		<link>http://weblog.destaquenet.com/2010/08/02/create-your-own-save-your-ass-multi-boot-usb-stick/</link>
		<comments>http://weblog.destaquenet.com/2010/08/02/create-your-own-save-your-ass-multi-boot-usb-stick/#comments</comments>
		<pubDate>Mon, 02 Aug 2010 11:15:46 +0000</pubDate>
		<dc:creator>Daniel Martins</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[boot]]></category>
		<category><![CDATA[clonezilla]]></category>
		<category><![CDATA[drive]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[gparted]]></category>
		<category><![CDATA[grub]]></category>
		<category><![CDATA[images]]></category>
		<category><![CDATA[kernel]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[memtest]]></category>
		<category><![CDATA[mint]]></category>
		<category><![CDATA[multi-boot]]></category>
		<category><![CDATA[rescue]]></category>
		<category><![CDATA[stick]]></category>
		<category><![CDATA[tools]]></category>
		<category><![CDATA[ubuntu]]></category>
		<category><![CDATA[usb]]></category>

		<guid isPermaLink="false">http://weblog.destaquenet.com/?p=1043</guid>
		<description><![CDATA[I don&#8217;t have to convince you that carry around a couple of Linux distros can mean the difference between life and death. Well, of course I&#8217;m being a little bit melodramatic here, but it&#8217;s really nice to have the ability &#8230; <a href="http://weblog.destaquenet.com/2010/08/02/create-your-own-save-your-ass-multi-boot-usb-stick/">Continue lendo <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I don&#8217;t have to convince you that carry around a couple of Linux distros can mean the difference between life and death. Well, of course I&#8217;m being a little bit melodramatic here, but it&#8217;s really nice to have the ability to quickly run a memory test on a machine that insists to misbehave, or even backup important stuff when you are on the go.</p>
<p>For me, some tools and distros are particularly useful:</p>
<ul>
<li><a href="http://clonezilla.org/">Clonezilla</a>. Very nice disk/partition cloning tool. It takes some time for beginners to get used to it, but it&#8217;s worth the effort. This tool saved my life more than once.</li>
<li><a href="http://gparted.sourceforge.net/">GParted</a>. Great tool for disk partitioning.</li>
<li><a href="http://linuxmint.com/">Linux Mint</a>. Very easy to use Ubuntu-based Linux distro. I particularly like the Live-DVD edition, which includes useful software like <a href="http://www.videolan.org/vlc/">VLC</a>, <a href="http://www.openoffice.org/">OpenOffice</a> and <a href="http://java.com/">Java</a>.</li>
<li><a href="http://ubuntu.com/">Ubuntu</a>. One of the most popular Linux distros nowadays.</li>
<li><a href="http://www.memtest.org/">Memtest86+</a>. Memory testing tool bundled in most Linux distros. Indispensable.</li>
</ul>
<p>Now, instead of burning one CD/DVD for each one of these tools, we&#8217;re going to use one multi-boot USB stick.</p>
<p><span id="more-1043"></span></p>
<h3>Preparing the flash drive</h3>
<p>The first step is to install a bootloader on the USB stick, so we can choose which distro/tool to load when booting from it. Lucky for me that my Linux distro of choice, Ubuntu, already comes with Grub2 pre-installed. This particular version of <a href="http://www.gnu.org/software/grub/">Grub</a> is required since it allows us to mount and boot .ISO images.</p>
<p>Once you have Grub2 installed, plug in your USB stick and make sure that its main partition is bootable and is formated with FAT32 (<code>vfat</code>).</p>
<p>Then, run this command to install Grub2 on the stick:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> grub-install <span style="color: #660033;">--no-floppy</span> <span style="color: #660033;">--root-directory</span>=<span style="color: #000000; font-weight: bold;">/</span>media<span style="color: #000000; font-weight: bold;">/</span>USB_STICK <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>sdX</pre></div></div>

<p>Remember to make <code>root-directory</code> point to the path where the flash drive is mounted, and change <code>/dev/sdX</code> to the actual device. Please double-check the parameters to avoid messing with the wrong disk!</p>
<h3>Configuring Grub2</h3>
<p>It&#8217;s time to download the .ISO images for one or more of the aforementioned distros and put them all inside the directory <code>/boot/iso/</code> inside flash drive. Then, create the file <code>/boot/grub/grub.cfg</code> with the following content:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">menuentry <span style="color: #ff0000;">&quot;Linux Mint 9 x86&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">set</span> <span style="color: #007800;">isofile</span>=<span style="color: #ff0000;">&quot;/boot/iso/linuxmint-9-gnome-dvd-i386.iso&quot;</span>
  loopback loop <span style="color: #007800;">$isofile</span>
  linux <span style="color: #7a0874; font-weight: bold;">&#40;</span>loop<span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span>casper<span style="color: #000000; font-weight: bold;">/</span>vmlinuz <span style="color: #007800;">boot</span>=casper iso-scan<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">filename</span>=<span style="color: #007800;">$isofile</span> noeject noprompt <span style="color: #660033;">--</span>
  initrd <span style="color: #7a0874; font-weight: bold;">&#40;</span>loop<span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span>casper<span style="color: #000000; font-weight: bold;">/</span>initrd.lz
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
menuentry <span style="color: #ff0000;">&quot;Ubuntu 10.04 Desktop x86&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">set</span> <span style="color: #007800;">isofile</span>=<span style="color: #ff0000;">&quot;/boot/iso/ubuntu-10.04-desktop-i386.iso&quot;</span>
  loopback loop <span style="color: #007800;">$isofile</span>
  linux <span style="color: #7a0874; font-weight: bold;">&#40;</span>loop<span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span>casper<span style="color: #000000; font-weight: bold;">/</span>vmlinuz <span style="color: #007800;">boot</span>=casper iso-scan<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">filename</span>=<span style="color: #007800;">$isofile</span> noeject noprompt <span style="color: #660033;">--</span>
  initrd <span style="color: #7a0874; font-weight: bold;">&#40;</span>loop<span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span>casper<span style="color: #000000; font-weight: bold;">/</span>initrd.lz
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
menuentry <span style="color: #ff0000;">&quot;Clonezilla Live 1.2.5-17 x86&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">set</span> <span style="color: #007800;">isofile</span>=<span style="color: #ff0000;">&quot;/boot/iso/clonezilla-live-1.2.5-17-i686.iso&quot;</span>
  loopback loop <span style="color: #007800;">$isofile</span> 
  linux <span style="color: #7a0874; font-weight: bold;">&#40;</span>loop<span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span>live<span style="color: #000000; font-weight: bold;">/</span>vmlinuz <span style="color: #007800;">boot</span>=live <span style="color: #007800;">union</span>=aufs nolocales noprompt <span style="color: #007800;">ip</span>=frommedia <span style="color: #007800;">toram</span>=filesystem.squashfs <span style="color: #007800;">findiso</span>=<span style="color: #007800;">$isofile</span> 
  initrd <span style="color: #7a0874; font-weight: bold;">&#40;</span>loop<span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span>live<span style="color: #000000; font-weight: bold;">/</span>initrd.img
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
menuentry <span style="color: #ff0000;">&quot;GParted Live 0.5.2-9 x86&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">set</span> <span style="color: #007800;">isofile</span>=<span style="color: #ff0000;">&quot;/boot/iso/gparted-live-0.5.2-9.iso&quot;</span>
  loopback loop <span style="color: #007800;">$isofile</span> 
  linux <span style="color: #7a0874; font-weight: bold;">&#40;</span>loop<span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span>live<span style="color: #000000; font-weight: bold;">/</span>vmlinuz <span style="color: #007800;">boot</span>=live <span style="color: #007800;">union</span>=aufs nolocales noprompt <span style="color: #007800;">ip</span>=frommedia <span style="color: #007800;">toram</span>=filesystem.squashfs <span style="color: #007800;">findiso</span>=<span style="color: #007800;">$isofile</span> 
  initrd <span style="color: #7a0874; font-weight: bold;">&#40;</span>loop<span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span>live<span style="color: #000000; font-weight: bold;">/</span>initrd.img
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
menuentry <span style="color: #ff0000;">&quot;Memory test (memtest86+)&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
  linux16 <span style="color: #000000; font-weight: bold;">/</span>boot<span style="color: #000000; font-weight: bold;">/</span>iso<span style="color: #000000; font-weight: bold;">/</span>memtest86+.bin
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<p>Don&#8217;t forget to change each <code>isofile</code> variable to match the downloaded images.</p>
<h3>Caveat</h3>
<p>This stuff is new, and there are only a few distros that can be booted that way. If you want to try this with other distros than the ones listed here, you&#8217;d probably need to Google for the right Kernel options to use.</p>
]]></content:encoded>
			<wfw:commentRss>http://weblog.destaquenet.com/2010/08/02/create-your-own-save-your-ass-multi-boot-usb-stick/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Django For Mobile Devices</title>
		<link>http://weblog.destaquenet.com/2010/07/12/django-for-mobile-devices/</link>
		<comments>http://weblog.destaquenet.com/2010/07/12/django-for-mobile-devices/#comments</comments>
		<pubDate>Mon, 12 Jul 2010 11:00:00 +0000</pubDate>
		<dc:creator>Daniel Martins</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[configuration]]></category>
		<category><![CDATA[deploy]]></category>
		<category><![CDATA[device]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[wsgi]]></category>

		<guid isPermaLink="false">http://weblog.destaquenet.com/?p=932</guid>
		<description><![CDATA[We are living what some people call the &#8220;mobile explosion&#8221;, a time where an increasing number of portable devices &#8212; like tablets and smartphones &#8212; are becoming a significant part of the Web. This is the reason why having your &#8230; <a href="http://weblog.destaquenet.com/2010/07/12/django-for-mobile-devices/">Continue lendo <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>We are living what some people call the &#8220;mobile explosion&#8221;, a time where an increasing number of portable devices &#8212; like tablets and smartphones &#8212; are becoming a significant part of the Web. This is the reason why having your website tailored for these devices is becoming increasingly important.</p>
<p>Fortunately, <a href="http://djangoproject.com/">Django</a> is one of the few web frameworks that makes this an easy problem to solve.</p>
<p><span id="more-932"></span></p>
<h3>How we solved it</h3>
<p>A simple way is to create a separate settings module for the mobile version. Doing this, we&#8217;re able to run two different webapps while reusing the existing codebase. Also, each version can have its own template directory, URL root configuration, etc.</p>
<p>Let&#8217;s do this. In the project root directory, create a file <code>m_settings.py</code> with the following content:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># File: myproject/m_settings.py</span>
&nbsp;
<span style="color: #483d8b;">&quot;&quot;&quot;Django settings for the mobile-specific website.
&quot;&quot;&quot;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Load the default settings</span>
<span style="color: #ff7700;font-weight:bold;">from</span> settings <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Domain &quot;m.mydomain.com&quot;</span>
SITE_ID = <span style="color: #ff4500;">2</span>
PREPEND_WWW = <span style="color: #008000;">False</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Used elsewhere to determine which version is running</span>
DEFAULT_VERSION = <span style="color: #008000;">False</span>
MOBILE_VERSION  = <span style="color: #008000;">True</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># URLs for the mobile version</span>
ROOT_URLCONF  = <span style="color: #483d8b;">'myproject.m_urls'</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Templates for the mobile version</span>
TEMPLATE_DIRS = <span style="color: black;">&#40;</span>
    <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">normpath</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">dirname</span><span style="color: black;">&#40;</span>__file__<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">'/template/mobile'</span><span style="color: black;">&#41;</span>,
<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Other settings...</span></pre></div></div>

<p>These two webapps share the same codebase, but that doesn&#8217;t mean they have to use the same URL root configuration. In fact, websites suitable for mobile devices are usually simpler, with fewer URL mappings.</p>
<p>To define separate URL root configuration for the mobile version, create a file <code>m_urls.py</code> with the following content:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># File: myproject/m_urls.py</span>
&nbsp;
<span style="color: #483d8b;">&quot;&quot;&quot;Mobile-specific URL definition.
&quot;&quot;&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">conf</span> <span style="color: #ff7700;font-weight:bold;">import</span> settings
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">conf</span>.<span style="color: black;">urls</span>.<span style="color: black;">defaults</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
<span style="color: #808080; font-style: italic;"># from django.contrib import admin</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># admin.autodiscover()</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Error page handlers</span>
handler404 = <span style="color: #483d8b;">'website.views.error_404_handler'</span>
handler500 = <span style="color: #483d8b;">'website.views.error_500_handler'</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Root URL patterns</span>
urlpatterns = patterns<span style="color: black;">&#40;</span><span style="color: #483d8b;">''</span>,
    <span style="color: #808080; font-style: italic;"># I18N</span>
    <span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'^i18n/'</span>, include<span style="color: black;">&#40;</span><span style="color: #483d8b;">'django.conf.urls.i18n'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>,
&nbsp;
    <span style="color: #808080; font-style: italic;"># Internal apps</span>
    <span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'^contact/'</span>, include<span style="color: black;">&#40;</span><span style="color: #483d8b;">'contact.urls'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>,
&nbsp;
    <span style="color: #808080; font-style: italic;"># (r'^admin/doc/', include('django.contrib.admindocs.urls')),</span>
    <span style="color: #808080; font-style: italic;"># (r'^admin/', include(admin.site.urls)),</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># Other settings...</span>
<span style="color: black;">&#41;</span></pre></div></div>

<p>Note that, in this particular example, the <a href="http://docs.djangoproject.com/en/dev/ref/contrib/admin/">Admin app</a> isn&#8217;t accessible from mobile version. This just shows that we can structure each webapp differently without too much trouble.</p>
<h3>Developing</h3>
<p>You already know how to run the development server for the default version. Most people don&#8217;t know, however, that it&#8217;s possible to choose a different port number and settings module, which allows us to launch one development server for each webapp version:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Terminal window 1: default version</span>
$ python manage.py runserver <span style="color: #000000;">8000</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Terminal window 2: mobile version</span>
$ python manage.py runserver <span style="color: #660033;">--settings</span>=m_settings <span style="color: #000000;">8001</span></pre></div></div>

<p>Just develop your stuff and see the results in both versions right away. Easy peasy.</p>
<h3>Testing</h3>
<p>You probably noticed that we have two settings that can be used to identify what webapp version is being run, <code>DEFAULT_VERSION</code> and <code>MOBILE_VERSION</code>. </p>
<p>So it&#8217;s just a matter of checking those settings when you need to do different things in each webapp version. This is particularly useful when you use an app in both versions and want to create separate tests for each version:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># File: myproject/myapp/tests/__init__.py</span>
&nbsp;
<span style="color: #483d8b;">&quot;&quot;&quot;Unit tests module for MyApp.
&quot;&quot;&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">conf</span> <span style="color: #ff7700;font-weight:bold;">import</span> settings
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">getattr</span><span style="color: black;">&#40;</span>settings, <span style="color: #483d8b;">'DEFAULT_VERSION'</span>, <span style="color: #008000;">False</span><span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;"># Tests the default version</span>
    <span style="color: #ff7700;font-weight:bold;">from</span> default_models_tests <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
    <span style="color: #ff7700;font-weight:bold;">from</span> default_navigation_tests <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">getattr</span><span style="color: black;">&#40;</span>settings, <span style="color: #483d8b;">'MOBILE_VERSION'</span>, <span style="color: #008000;">False</span><span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;"># Tests the mobile version</span>
    <span style="color: #ff7700;font-weight:bold;">from</span> mobile_models_tests <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
    <span style="color: #ff7700;font-weight:bold;">from</span> mobile_navigation_tests <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span></pre></div></div>

<p>We can run the tests for each version in a similar way we did before with the <code>runserver</code> command:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Default version</span>
$ python manage.py <span style="color: #7a0874; font-weight: bold;">test</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>appname...<span style="color: #7a0874; font-weight: bold;">&#93;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Mobile version</span>
$ python manage.py <span style="color: #7a0874; font-weight: bold;">test</span> <span style="color: #660033;">--settings</span>=m_settings <span style="color: #7a0874; font-weight: bold;">&#91;</span>appname...<span style="color: #7a0874; font-weight: bold;">&#93;</span></pre></div></div>

<h3>Deploying</h3>
<p>Despite what you may think, this is the easy part.</p>
<p>Since <a href="http://docs.djangoproject.com/en/dev/howto/deployment/modwsgi/">deploying Django</a> with <a href="http://apache.org/">Apache</a> and <a href="http://code.google.com/p/modwsgi/">mod_wsgi</a> is the recommended way to get Django into production, it&#8217;s likely that you already have a WSGI file like this:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>, <span style="color: #dc143c;">sys</span>
&nbsp;
APPS_PATH   = <span style="color: #483d8b;">'/home/user/wsgi_apps'</span>
MYPROJECT_PATH = <span style="color: #483d8b;">'%s/myproject'</span> <span style="color: #66cc66;">%</span> APPS_PATH
&nbsp;
<span style="color: #808080; font-style: italic;"># Third-party eggs</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">site</span>
<span style="color: #dc143c;">site</span>.<span style="color: black;">addsitedir</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'/home/user/.python/lib'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #dc143c;">sys</span>.<span style="color: black;">path</span> += <span style="color: black;">&#91;</span>APPS_PATH, MYPROJECT_PATH<span style="color: black;">&#93;</span>
<span style="color: #dc143c;">os</span>.<span style="color: black;">environ</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'DJANGO_SETTINGS_MODULE'</span><span style="color: black;">&#93;</span> = <span style="color: #483d8b;">'myproject.settings'</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> django.<span style="color: black;">core</span>.<span style="color: black;">handlers</span>.<span style="color: black;">wsgi</span>
application = django.<span style="color: black;">core</span>.<span style="color: black;">handlers</span>.<span style="color: black;">wsgi</span>.<span style="color: black;">WSGIHandler</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>All we need to do is configure another WSGI-enabled subdomain on our server, e.g. m.mywebsite.com, that refers to another WSGI file like this one:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>, <span style="color: #dc143c;">sys</span>
&nbsp;
APPS_PATH   = <span style="color: #483d8b;">'/home/user/wsgi_apps'</span>
MYPROJECT_PATH = <span style="color: #483d8b;">'%s/myproject'</span> <span style="color: #66cc66;">%</span> APPS_PATH
&nbsp;
<span style="color: #808080; font-style: italic;"># Third-party eggs</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">site</span>
<span style="color: #dc143c;">site</span>.<span style="color: black;">addsitedir</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'/home/user/.python/lib'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #dc143c;">sys</span>.<span style="color: black;">path</span> += <span style="color: black;">&#91;</span>APPS_PATH, MYPROJECT_PATH<span style="color: black;">&#93;</span>
<span style="color: #dc143c;">os</span>.<span style="color: black;">environ</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'DJANGO_SETTINGS_MODULE'</span><span style="color: black;">&#93;</span> = <span style="color: #483d8b;">'myproject.m_settings'</span> <span style="color: #808080; font-style: italic;"># This line</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> django.<span style="color: black;">core</span>.<span style="color: black;">handlers</span>.<span style="color: black;">wsgi</span>
application = django.<span style="color: black;">core</span>.<span style="color: black;">handlers</span>.<span style="color: black;">wsgi</span>.<span style="color: black;">WSGIHandler</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>The only difference between these two WSGI files is that each one points to a different settings module. And no, you don&#8217;t need to have a separate copy for each version. We are using the same codebase for both versions, remember? <img src='http://weblog.destaquenet.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<h3>Minimizing the traffic</h3>
<p>Amazingly, not everyone has access ultra-fast internet connections on their devices, so it&#8217;s very important to keep your sites as lightweight as possible.</p>
<p>There are several open source projects that try to address this problem. One of them is <a href="http://code.google.com/p/django-compress/">django-compress</a>, a Django app that provides an automated system for compressing CSS and JavaScript files.</p>
<p>In fact, doesn&#8217;t matter if your websites target mobile devices or not; you should <em>always</em> try to minimize the amount of traffic.</p>
<h3>What&#8217;s left?</h3>
<p>There&#8217;s one small problem though: when someone visit the regular website from a mobile device, or vice-versa, they are not redirected to the appropriate version. But this is a topic for another article!</p>
]]></content:encoded>
			<wfw:commentRss>http://weblog.destaquenet.com/2010/07/12/django-for-mobile-devices/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Setting Up A Local Mail Server With Exim And Ubuntu</title>
		<link>http://weblog.destaquenet.com/2010/06/25/setting-up-a-local-mail-server-with-exim-and-ubuntu/</link>
		<comments>http://weblog.destaquenet.com/2010/06/25/setting-up-a-local-mail-server-with-exim-and-ubuntu/#comments</comments>
		<pubDate>Fri, 25 Jun 2010 11:00:08 +0000</pubDate>
		<dc:creator>Daniel Martins</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[configuration]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[environment]]></category>
		<category><![CDATA[exim]]></category>
		<category><![CDATA[gmail]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[mail]]></category>
		<category><![CDATA[sendmail]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[smtp]]></category>
		<category><![CDATA[telnet]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://weblog.destaquenet.com/?p=936</guid>
		<description><![CDATA[Exim is an open source MTA that can be used as a drop-in replacement for Sendmail on Unix-like operating systems. I have no idea what&#8217;s the best mail server out there, but if you are looking for a relay-only server &#8230; <a href="http://weblog.destaquenet.com/2010/06/25/setting-up-a-local-mail-server-with-exim-and-ubuntu/">Continue lendo <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.exim.org/">Exim</a> is an open source <acronym title="Mail Transfer Agent">MTA</acronym> that can be used as a drop-in replacement for <a href="http://www.sendmail.org/">Sendmail</a> on Unix-like operating systems. I have no idea what&#8217;s the best mail server out there, but if you are looking for a relay-only server to install on your development setup, Exim is probably the easiest to get started with.</p>
<p>In this article I&#8217;ll show you how to configure Exim to send messages through <a href="http://gmail.com/">Gmail</a>.</p>
<p><span id="more-936"></span></p>
<h3>Installing Exim</h3>
<p>First we need to install Exim and its dependencies, which is a simple task in modern Linux distros like <a href="http://www.ubuntu.com/">Ubuntu</a>/<a href="http://www.debian.org/">Debian</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> exim4</pre></div></div>

<h3>Configuring Exim as a relay server</h3>
<p>There&#8217;s a wizard that helps us with the basic setup; run the following command line to open it and fill each screen as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> dpkg-reconfigure exim4-config</pre></div></div>

<ol>
<li><strong>General type of mail configuration:</strong> mail sent by smarthost; received via SMTP or fetchmail</li>
<li><strong>System mail name:</strong> yourdomain.com</li>
<li><strong>IP-addresses to listen on for incoming connections:</strong> 127.0.0.1 ; ::1</li>
<li><strong>Other destinations for which mail is accepted:</strong> Leave it blank</li>
<li><strong>Machines to relay mail for:</strong> Leave it blank</li>
<li><strong>Host name for the outgoing smarthost:</strong> smtp.gmail.com:587</li>
<li><strong>Hide local mail name in outgoing mail:</strong> Yes</strong></li>
<li><strong>Visible domain name for local users:</strong> yourdomain.com</li>
<li><strong>Keep number of DNS-queries minimal:</strong> No</li>
<li><strong>Delivery method for local mail:</strong> mbox format in /var/mail/</li>
<li><strong>Split configuration into small files:</strong> Yes</li>
<li><strong>Root and postmaster mail recipient:</strong> Leave it blank</li>
</ol>
<p>Now we have to configure the user credentials in the file <code>/etc/exim4/passwd.client</code> by adding the following line:</p>
<pre>
# Replace [username] and [password] by your account's credentials
*.google.com:[username]:[password]
</pre>
<p>Let&#8217;s finish the configuration by running the following command line:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> update-exim4.conf</pre></div></div>

<h3>Testing the setup</h3>
<p>Here&#8217;s an old-s<strong>c</strong>h<strong>ool</strong> way to test if our setup is working &#8212; with <code>telnet</code> command:</p>
<pre>
$ telnet localhost 25
    Trying ::1...
    Connected to localhost.
    Escape character is '^]'.
    220 localhost ESMTP Exim 4.71 Wed, 23 Jun 2010 22:28:56 -0300
HELO localhost
    250 localhost Hello localhost [::1]
MAIL FROM: username@yourdomain.com
    250 OK
RCPT TO: destination@email.com
    250 Accepted
DATA
    354 Enter message, ending with "." on a line by itself
SUBJECT: Message subject
Testing Exim via telnet. w00t!
.
    250 OK id=1ORbG5-0005uh-EF
QUIT
    221 localhost closing connection
    Connection closed by foreign host.
</pre>
<p>Open the log files at <code>/var/log/exim4</code> to see if the message relayed correctly. This is how a successful attempt looks like:</p>
<pre>
2010-06-23 22:29:47 1ORbG5-0005uh-EF <= local@yourdomain.com H=localhost [::1] P=smtp S=345
2010-06-23 22:29:50 1ORbG5-0005uh-EF => destination@email.com R=smarthost T=remote_smtp_smarthost H=gmail-smtp-msa.l.google.com [74.125.45.109] ..."
2010-06-23 22:29:50 1ORbG5-0005uh-EF Completed
</pre>
<p>If the log looks good and you didn&#8217;t get the message, it&#8217;s a good idea to check the spam box of the destination email before you change any configuration.</p>
]]></content:encoded>
			<wfw:commentRss>http://weblog.destaquenet.com/2010/06/25/setting-up-a-local-mail-server-with-exim-and-ubuntu/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Templates in 10 minutes with JSP and tag files</title>
		<link>http://weblog.destaquenet.com/2009/12/17/templates-in-10-minutes-with-jsp-and-tag-files/</link>
		<comments>http://weblog.destaquenet.com/2009/12/17/templates-in-10-minutes-with-jsp-and-tag-files/#comments</comments>
		<pubDate>Thu, 17 Dec 2009 12:30:06 +0000</pubDate>
		<dc:creator>Daniel Martins</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jsp]]></category>
		<category><![CDATA[layout]]></category>
		<category><![CDATA[servlet]]></category>
		<category><![CDATA[tag]]></category>
		<category><![CDATA[tagfiles]]></category>
		<category><![CDATA[taglib]]></category>
		<category><![CDATA[template]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://weblog.destaquenet.com/?p=822</guid>
		<description><![CDATA[Have you ever seen JSPs full of Aramaic-like scriptlets, crazy includes, duplicated code and no taglibs beyond that provided by the web framework in use? Are you staring at one right now? This is a fairly common thing, at least &#8230; <a href="http://weblog.destaquenet.com/2009/12/17/templates-in-10-minutes-with-jsp-and-tag-files/">Continue lendo <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Have you ever seen JSPs full of Aramaic-like scriptlets, crazy includes, duplicated code and no taglibs beyond that provided by the web framework in use? Are you staring at one right now?</p>
<p>This is a fairly common thing, at least for the companies I worked for so far, and that&#8217;s what scares me the most. Hopefully, you can easily make things better if you want to.</p>
<p>In this post, I&#8217;ll show you a few techniques that can be used to write clean JSPs without resorting to any kind of templating system whatsoever.</p>
<p><span id="more-822"></span></p>
<h3>An example</h3>
<p>The first step is to define the base template for the application pages. Consider the following XHTML example and its graphic representation:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;</span>
<span style="color: #00bbdd;">    &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/1999/xhtml&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;meta</span> <span style="color: #000066;">http-equiv</span>=<span style="color: #ff0000;">&quot;content-type&quot;</span> <span style="color: #000066;">content</span>=<span style="color: #ff0000;">&quot;text/html; charset=UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;meta</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;keywords&quot;</span> <span style="color: #000066;">content</span>=<span style="color: #ff0000;">&quot;keyword1, keyword2, keyword3&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;meta</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;description&quot;</span> <span style="color: #000066;">content</span>=<span style="color: #ff0000;">&quot;Page description&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Page title<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
        <span style="color: #808080; font-style: italic;">&lt;!-- Essential scripts --&gt;</span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;link</span> <span style="color: #000066;">rel</span>=<span style="color: #ff0000;">&quot;stylesheet&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/css&quot;</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;css/style.css&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;script</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;js/query.js&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
        <span style="color: #808080; font-style: italic;">&lt;!-- Extra header --&gt;</span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;script</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;js/util.js&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #808080; font-style: italic;">&lt;!-- Main --&gt;</span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;wrapper&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;header&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;content&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;footer&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> <span style="color: #808080; font-style: italic;">&lt;!-- Main --&gt;</span>
&nbsp;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;script</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            // Other scripts (e.g. Google Analytics)
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<div id="attachment_803" class="wp-caption aligncenter" style="width: 310px"><a href="http://weblog.destaquenet.com/wp-content/uploads/2009/12/layout.gif"><img src="http://weblog.destaquenet.com/wp-content/uploads/2009/12/layout-300x156.gif" alt="Template graphic representation" title="layout" width="300" height="156" class="size-medium wp-image-803" /></a><p class="wp-caption-text">Template graphic representation</p></div>
<p>What usually happens, even with a simple template such as this, is a high level of code duplication among the different pages due to the heavy use of <a href="http://en.wikipedia.org/wiki/Copy_and_paste_programming">Copy and Paste Programming</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;</span>
<span style="color: #00bbdd;">    &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/1999/xhtml&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;meta</span> <span style="color: #000066;">http-equiv</span>=<span style="color: #ff0000;">&quot;content-type&quot;</span> <span style="color: #000066;">content</span>=<span style="color: #ff0000;">&quot;text/html; charset=UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;meta</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;keywords&quot;</span>    <span style="color: #000066;">content</span>=<span style="color: #ff0000;">&quot;keyword1, keyword2, keyword3&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;meta</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;description&quot;</span> <span style="color: #000066;">content</span>=<span style="color: #ff0000;">&quot;Page description&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Page title<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
        <span style="color: #808080; font-style: italic;">&lt;!-- Essential scripts --&gt;</span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jsp:include</span> <span style="color: #000066;">page</span>=<span style="color: #ff0000;">&quot;scripts.jsp&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
        <span style="color: #808080; font-style: italic;">&lt;!-- Extra header --&gt;</span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;script</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;js/util.js&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #808080; font-style: italic;">&lt;!-- Main --&gt;</span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;wrapper&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jsp:include</span> <span style="color: #000066;">file</span>=<span style="color: #ff0000;">&quot;header.jsp&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;content&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Page content here<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jsp:include</span> <span style="color: #000066;">page</span>=<span style="color: #ff0000;">&quot;footer.jsp&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> <span style="color: #808080; font-style: italic;">&lt;!-- Main --&gt;</span>
&nbsp;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;script</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            // Other scripts
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>See? Why break the template into small pieces if you are going keep duplicating code anyway? The best thing to do is to translate that template into something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;">&lt;%@ taglib <span style="color: #000066;">tagdir</span>=<span style="color: #ff0000;">&quot;/WEB-INF/tags/layout&quot;</span> <span style="color: #000066;">prefix</span>=<span style="color: #ff0000;">&quot;layout&quot;</span> %<span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;layout:page</span> <span style="color: #000066;">title</span>=<span style="color: #ff0000;">&quot;Title&quot;</span> <span style="color: #000066;">description</span>=<span style="color: #ff0000;">&quot;Page description&quot;</span> <span style="color: #000066;">keywords</span>=<span style="color: #ff0000;">&quot;keyword1, keyword2, keyword3&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jsp:attribute</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;extraHeader&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;script</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;js/util.js&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/jsp:attribute<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jsp:attribute</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;extraBottom&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        // Other scripts (e.g. Google Analytics)
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/jsp:attribute<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jsp:body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        Page content here
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/jsp:body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/layout:page<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<h3>Content reuse with tag files</h3>
<p>There are three types of JSP tags, each with its own strengths and weaknesses: <a href="http://java.sun.com/javaee/5/docs/tutorial/doc/bnaow.html#bnaoy">classic tag handlers</a>, <a href="http://java.sun.com/javaee/5/docs/tutorial/doc/bnann.html">simple tag handlers</a> and <a href="http://java.sun.com/javaee/5/docs/tutorial/doc/bnama.html">tag files</a>. Today I&#8217;ll cover the basics of tag files, but every Java developer should learn when and how to use the other two.</p>
<p>The first thing we need to do is create the <code>WEB-INF/tags/layout</code> directory, which will keep the tag files we are about to create. Now, create two files inside this directory, one for the header and the other for the footer:</p>
<p><strong>header.tag</strong></p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;">&lt;%@ tag <span style="color: #000066;">body-content</span>=<span style="color: #ff0000;">&quot;empty&quot;</span> <span style="color: #000066;">description</span>=<span style="color: #ff0000;">&quot;Header tag file&quot;</span> %<span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;header&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #808080; font-style: italic;">&lt;!-- Header section here --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p><strong>footer.tag</strong></p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;">&lt;%@ tag <span style="color: #000066;">body-content</span>=<span style="color: #ff0000;">&quot;empty&quot;</span> <span style="color: #000066;">description</span>=<span style="color: #ff0000;">&quot;Footer tag file&quot;</span> %<span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;footer&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #808080; font-style: italic;">&lt;!-- Footer section here --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>As we can see, tag files are just like JSPs, but with some special properties.</p>
<p>The last tag file, <strong>page.tag</strong>, defines the overall page structure:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;">&lt;%@ tag <span style="color: #000066;">description</span>=<span style="color: #ff0000;">&quot;Page layout&quot;</span> %<span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
<span style="color: #009900;">&lt;%@ attribute <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;title&quot;</span>       <span style="color: #000066;">required</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000066;">description</span>=<span style="color: #ff0000;">&quot;Page title&quot;</span> %<span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;">&lt;%@ attribute <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;keywords&quot;</span>    <span style="color: #000066;">required</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000066;">description</span>=<span style="color: #ff0000;">&quot;Page keywords to improve SEO&quot;</span> %<span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;">&lt;%@ attribute <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;description&quot;</span> <span style="color: #000066;">required</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000066;">description</span>=<span style="color: #ff0000;">&quot;Page description&quot;</span> %<span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
<span style="color: #009900;">&lt;%@ attribute <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;extraHeader&quot;</span> <span style="color: #000066;">fragment</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000066;">description</span>=<span style="color: #ff0000;">&quot;Extra code to put before &lt;/head&gt;</span></span>&quot; %&gt;
<span style="color: #009900;">&lt;%@ attribute <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;extraBottom&quot;</span> <span style="color: #000066;">fragment</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000066;">description</span>=<span style="color: #ff0000;">&quot;Extra code to put before &lt;/body&gt;</span></span>&quot; %&gt;
&nbsp;
<span style="color: #009900;">&lt;%@ taglib <span style="color: #000066;">tagdir</span>=<span style="color: #ff0000;">&quot;/WEB-INF/tags/layout&quot;</span> <span style="color: #000066;">prefix</span>=<span style="color: #ff0000;">&quot;layout&quot;</span> %<span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
<span style="color: #00bbdd;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;</span>
<span style="color: #00bbdd;">    &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/1999/xhtml&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;meta</span> <span style="color: #000066;">http-equiv</span>=<span style="color: #ff0000;">&quot;content-type&quot;</span> <span style="color: #000066;">content</span>=<span style="color: #ff0000;">&quot;text/html; charset=UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
        <span style="color: #808080; font-style: italic;">&lt;!-- Displaying the attributes using EL --&gt;</span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;meta</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;keywords&quot;</span>    <span style="color: #000066;">content</span>=<span style="color: #ff0000;">&quot;${keywords}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;meta</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;description&quot;</span> <span style="color: #000066;">content</span>=<span style="color: #ff0000;">&quot;${description}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${title}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
        <span style="color: #808080; font-style: italic;">&lt;!-- Essential scripts --&gt;</span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;link</span> <span style="color: #000066;">rel</span>=<span style="color: #ff0000;">&quot;stylesheet&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/css&quot;</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;css/style.css&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;script</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;js/query.js&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
        <span style="color: #808080; font-style: italic;">&lt;!-- Process the given input fragment --&gt;</span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jsp:invoke</span> <span style="color: #000066;">fragment</span>=<span style="color: #ff0000;">&quot;extraHeader&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #808080; font-style: italic;">&lt;!-- Main --&gt;</span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;wrapper&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #808080; font-style: italic;">&lt;!-- Renders the page header --&gt;</span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;layout:header</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
            <span style="color: #808080; font-style: italic;">&lt;!-- Renders the tag body inside a DIV --&gt;</span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;content&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;jsp:doBody</span><span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
            <span style="color: #808080; font-style: italic;">&lt;!-- Renders the page footer --&gt;</span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;layout:footer</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> <span style="color: #808080; font-style: italic;">&lt;!-- Main --&gt;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">&lt;!-- Process the given input fragment --&gt;</span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jsp:invoke</span> <span style="color: #000066;">fragment</span>=<span style="color: #ff0000;">&quot;extraBottom&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>This last tag file is more complex than the other two, because you can pass to it both values and <a href="http://java.sun.com/javaee/5/docs/tutorial/doc/bnaln.html#bnalq">fragments</a>. Besides, this tag accepts a body, which gets executed with <code>&lt;jsp:doBody/&gt;</code>.</p>
<p>Of course, the example shown here is fairly simple, so we didn&#8217;t have to use more advanced features like <a href="http://java.sun.com/javaee/5/docs/tutorial/doc/bnaln.html#bnaly">variables</a> and <a href="http://java.sun.com/javaee/5/docs/tutorial/doc/bnaln.html#bnaly">dynamic attributes</a>. Be that as it may, it&#8217;s a good idea to know what the technology has to offer. You&#8217;ll never know when a feature will be useful until you need it.</p>
<h3>Usage</h3>
<p>Now, let&#8217;s put those tag files to use:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;">&lt;%@ taglib <span style="color: #000066;">tagdir</span>=<span style="color: #ff0000;">&quot;/WEB-INF/tags/layout&quot;</span> <span style="color: #000066;">prefix</span>=<span style="color: #ff0000;">&quot;layout&quot;</span> %<span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;">&lt;%@ taglib <span style="color: #000066;">uri</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/jsp/jstl/core&quot;</span> <span style="color: #000066;">prefix</span>=<span style="color: #ff0000;">&quot;c&quot;</span> %<span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;layout:page</span> <span style="color: #000066;">title</span>=<span style="color: #ff0000;">&quot;Index page&quot;</span> <span style="color: #000066;">description</span>=<span style="color: #ff0000;">&quot;This is the index page of my amazing app&quot;</span> <span style="color: #000066;">keywords</span>=<span style="color: #ff0000;">&quot;amazing, app&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jsp:attribute</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;extraHeader&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;script</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;js/util.js&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;script</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;js/some_jquery_plugin.js&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/jsp:attribute<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jsp:attribute</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;extraBottom&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        $(function() {
            // Call some JQuery function here, specific for this page
        });
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/jsp:attribute<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jsp:body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        Welcome to my amazing app!<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;br</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        1+1 = ${1+1}
&nbsp;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ul<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;c:forEach</span> <span style="color: #000066;">items</span>=<span style="color: #ff0000;">&quot;${objects}&quot;</span> <span style="color: #000066;">var</span>=<span style="color: #ff0000;">&quot;obj&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${obj.attr}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/c:forEach<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ul<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/jsp:body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/layout:page<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Although the application as a whole follow the same visual standard, each page may need additional resources (JavaScript, CSS, etc.). This is hardly a problem because our template allows us to include extra content if necessary.</p>
<p>The other advantage is that tag files allow the use of custom tags and <a href="http://java.sun.com/javaee/5/docs/tutorial/doc/bnahq.html">unified EL</a>, but <strong>deny the use of scriptlets</strong>, which is a good thing if you want to get rid of messy JSPs.</p>
<h3>Recommended reading</h3>
<ul>
<li><a href="http://java.sun.com/javaee/5/docs/tutorial/doc/">The Java EE 5 Tutorial</a>;</li>
<li><a href="http://www.amazon.com/Head-First-Servlets-JSP-Certified/dp/0596516681/ref=sr_1_1?ie=UTF8&#038;s=books&#038;qid=1260993372&#038;sr=8-1">Head First Servlets &#038; JSP</a>;</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://weblog.destaquenet.com/2009/12/17/templates-in-10-minutes-with-jsp-and-tag-files/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

