<?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>Programming tips for newbies - CodeRookie &#187; Tutorial</title>
	<atom:link href="http://www.coderookie.com/category/tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.coderookie.com</link>
	<description>Programming tips from a rookie developer to other rookie developers</description>
	<lastBuildDate>Wed, 17 Nov 2010 07:46:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Converting a set of mts files to a single avi file with fade in/out in Linux</title>
		<link>http://www.coderookie.com/2010/tutorial/converting-a-set-of-mts-files-to-a-single-avi-file-with-fade-inout-in-linux/</link>
		<comments>http://www.coderookie.com/2010/tutorial/converting-a-set-of-mts-files-to-a-single-avi-file-with-fade-inout-in-linux/#comments</comments>
		<pubDate>Tue, 16 Nov 2010 20:10:55 +0000</pubDate>
		<dc:creator>Kris</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[linux]]></category>

		<guid isPermaLink="false">http://www.coderookie.com/?p=32</guid>
		<description><![CDATA[For a while I used a commercial windows software to convert a bunch of MTS files, but as a Linux user (Windows is for games mostly :) ), I finally managed to find a way to do it using ffmpeg, a patch and mencoder.

I bought Panasonic SD-9 2 years ago and since then I was [...]]]></description>
			<content:encoded><![CDATA[<p>For a while I used a commercial windows software to convert a bunch of MTS files, but as a Linux user (Windows is for games mostly :) ), I finally managed to find a way to do it using ffmpeg, a patch and mencoder.<br />
<span id="more-32"></span></p>
<p>I bought Panasonic SD-9 2 years ago and since then I was filming all my vacations, parties, etc. Usually when I film I end up with at least a 50 MTS files that are mostly unrelated, so I wanted to have some nice fade out and fade ins between the joined slices. Those fades are the only thing that was some kind hard to do in linux, but half a year ago I found a nice patch to ffmpeg that does exactly this. So here we go.</p>
<p>Requirements:<br />
1. ffmpeg source code (if you use the latest trunk then you need libx264 version >= 0.99):<br />
<code>svn co svn://svn.ffmpeg.org/ffmpeg/trunk</code><br />
2. patch file (<a href="http://www.mail-archive.com/ffmpeg-soc@mplayerhq.hu/msg08155.html">mail list post</a>, or <a href="http://bmintern.homeunix.com/~brandon/vf_fade.patch">patch authors site</a> or the file hosted on <a href="http://www.coderookie.com/ffmpeg_fade2.patch">my blog</a>)<br />
3. mencoder (it is used to count the total frames of the movie)<br />
4. avimerge (to put all the converted files back together)</p>
<p>Steps:<br />
1. Go to the directory for ffmpeg<br />
2. Apply patch (patch -p 0 < patch_file)<br />
Application of patch may fail, as ffmpeg is changing, but all you really need is <a href="http://www.coderookie.com/vf_fade.c">vf_fade.c</a> file placed in libavfilter directory, and add line:<br />
<code>OBJS-$(CONFIG_FADE_FILTER)                   += vf_fade.o</code><br />
to libavfilter/Makefile, as well a line:<br />
<code>REGISTER_FILTER (FADE,        fade,        vf);</code><br />
to libavfilter/allfilters.c</p>
<p>3. Build ffmpeg: ./configure with appropriate options, mine are:<br />
<code>--prefix=$HOME/ffmpeg --enable-libx264 --enable-libxvid --enable-libvorbis --enable-libtheora --enable-libmp3lame --enable-libfaac --enable-avfilter --enable-nonfree --enable-postproc --enable-runtime-cpudetect --enable-hardcoded-tables  --enable-hwaccel=h263_vaapi --enable-hwaccel=h264_vaapi --enable-hwaccel=mpeg2_vaapi --enable-hwaccel=mpeg4_vaapi --enable-hwaccel=vc1_vaapi --enable-hwaccel=wmv3_vaapi --enable-filter=fade --enable-encoder=libx264 --enable-encoder=libxvid --enable-encoder=libmp3lame --enable-encoder=libfaac --enable-encoder=wmv2 --enable-encoder=wmv1 --enable-encoder=vorbis --enable-encoder=mpeg4 --enable-encoder=ac3 --enable-decoder=h264_vdpau  --enable-gpl</code><br />
And make, followed by make install.</p>
<p>Now you should have working ffmpeg with fade in/outs.</p>
<p>Here&#8217;s a <a href="http://www.coderookie.com/make_avi.sh">make_avi.sh</a> script that I&#8217;ve written for this:<br />
<code><br />
#!/bin/sh<br />
BIT_RATE=2700k<br />
SIZE=720x576<br />
fade_dur=15<br />
FFMPEG=ffmpeg<br />
for FILE_IN in $*; do<br />
    echo -n "$FILE_IN...."<br />
    #last_frame=`$FRAME_COUNTER $FILE_IN`<br />
    last_frame=` mencoder -nosound -ovc frameno -vc null -o /dev/null $FILE_IN 2>/dev/null | egrep " [0-9]+ frames\$" | gawk '{print \$12}'`<br />
    echo $last_frame<br />
    filters="-vfilters fade=in:0:$fade_dur,fade=out:$[last_frame/2 - $fade_dur-5]:$fade_dur"<br />
    ${FFMPEG} -i $FILE_IN  -deinterlace -vcodec libxvid -aspect 4:3 -r 25 -s $SIZE -b $BIT_RATE $filters -acodec libmp3lame -ac 2 -ab 128k -y -pass 1 -passlogfile temp.log -f avi /dev/null &#038;&#038; \<br />
    ${FFMPEG} -i $FILE_IN -deinterlace -vcodec libxvid -aspect 4:3 -r 25 -s $SIZE -b $BIT_RATE $filters -acodec libmp3lame -ac 2 -ab 128k -y -pass 2 -passlogfile temp.log -f avi ${FILE_IN/MTS/avi}<br />
done<br />
in_file_list="$*"<br />
out_file_list=${in_file_list//MTS/avi}<br />
avimerge -o merge.avi -i $out_file_list<br />
</code></p>
<p>Just save it, change FFMPEG variable to your location of ffmpeg and run it passing a list of MTS files, it will create a merge.avi file in the current directory.<br />
You can also tweak the &#8220;fade_dur&#8221; to make the fade longer/shorter.</p>
<p>I know it&#8217;s not much of a how to, but maybe someone will find it useful.</p>

<div class="sociable">
<span class="sociable_tagline">
<strong>Share and Enjoy:</strong>
	<span>These icons link to social bookmarking sites where readers can share and discover new web pages.</span>
</span>
<ul>
	<li><a href="http://www.dzone.com/links/add.html?url=http%3A%2F%2Fwww.coderookie.com%2F2010%2Ftutorial%2Fconverting-a-set-of-mts-files-to-a-single-avi-file-with-fade-inout-in-linux%2F&amp;title=Converting+a+set+of+mts+files+to+a+single+avi+file+with+fade+in%2Fout+in+Linux" title="DZone" onfocus="sociable_description_link(this, 'description')"><img src="http://www.coderookie.com/wp-content/plugins/sociable/images/dzone.png" title="DZone" alt="DZone" class="sociable-hovers" /></a></li>
	<li><a href="http://del.icio.us/post?url=http%3A%2F%2Fwww.coderookie.com%2F2010%2Ftutorial%2Fconverting-a-set-of-mts-files-to-a-single-avi-file-with-fade-inout-in-linux%2F&amp;title=Converting+a+set+of+mts+files+to+a+single+avi+file+with+fade+in%2Fout+in+Linux" title="del.icio.us"><img src="http://www.coderookie.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a></li>
	<li><a href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.coderookie.com%2F2010%2Ftutorial%2Fconverting-a-set-of-mts-files-to-a-single-avi-file-with-fade-inout-in-linux%2F&amp;title=Converting+a+set+of+mts+files+to+a+single+avi+file+with+fade+in%2Fout+in+Linux" title="Digg" onfocus="sociable_description_link(this, 'bodytext')"><img src="http://www.coderookie.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a></li>
	<li><a href="http://technorati.com/faves?add=http%3A%2F%2Fwww.coderookie.com%2F2010%2Ftutorial%2Fconverting-a-set-of-mts-files-to-a-single-avi-file-with-fade-inout-in-linux%2F" title="Technorati"><img src="http://www.coderookie.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a></li>
</ul>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.coderookie.com/2010/tutorial/converting-a-set-of-mts-files-to-a-single-avi-file-with-fade-inout-in-linux/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Really Easy Introduction to JSP and Servlets and . Part I</title>
		<link>http://www.coderookie.com/2006/java/a-really-easy-introduction-to-servlets-and-jsp-part-i/</link>
		<comments>http://www.coderookie.com/2006/java/a-really-easy-introduction-to-servlets-and-jsp-part-i/#comments</comments>
		<pubDate>Tue, 05 Sep 2006 07:58:55 +0000</pubDate>
		<dc:creator>Kris</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[j2ee]]></category>

		<guid isPermaLink="false">http://www.coderookie.com/2006/java/a-really-easy-introduction-to-servlets-and-jsp-part-i/</guid>
		<description><![CDATA[Have you ever wondered "What the hell is this J2EE thingy?", "How can someone use Java to develop web applications?" or even "Web pages in Java? Isn't this for applets only?".
If so, than we have something in common, for years I have been asking myself those questions, while not having enough time to dig into [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.coderookie.com/wp-content/uploads/2006/09/question.jpg" alt="question mark" align="left" />Have you ever wondered "What the hell is this J2EE thingy?", "How can someone use Java to develop web applications?" or even "Web pages in Java? Isn't this for applets only?".</p>
<p>If so, than we have something in common, for years I have been asking myself those questions, while not having enough time to dig into the subject and learn more about Java. This language had always seemed to me to slow and having an ugly GUI (remember all that applets floating around the web in the late 90-ties ?).</p>
<p>As I have finally found some time to study a part of J2EE consisting of Servlets and JSP (using the excellent <a href="http://www.amazon.com/gp/product/0596005407?ie=UTF8&amp;tag=coderookie-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0596005407">Head First Servlets and JSP: Passing the Sun Certified Web Component Developer Exam (SCWCD)</a><img src="http://www.assoc-amazon.com/e/ir?t=coderookie-20&amp;l=as2&#038;o=1&amp;a=0596005407" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /> by Kathy Sierra et al.), I will present you with what I have learned. I really hope that you will benefit from the information presented below. If you have bought some kind of Tomcat hosting or some other J2EE web hosting than you can skip the first point, and go straight to the second one.<br />
<script type="text/javascript"><!--
google_ad_client = "pub-3846130357517643";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
google_ad_channel ="9138240644";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_text = "000000";
google_color_url = "FFFFFF";
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
<span id="more-11"></span></p>
<p><img src="http://www.coderookie.com/wp-content/uploads/2006/09/tomcat.gif" alt="Tomcat" class="alignright" /><strong>1. Getting a web container - Tomcat</strong></p>
<p>First of all, you should know that most J2EE technologies (OK, maybe not most, but few crucial ones: EJB, servlets &#038; JSP) cannot be run like normal Java applications just by invoking java executable in the command line.</p>
<p>Servlets, JSP and EJB need a... lets call it a framework and this kind of framework implementation specifically for servlets/JSP (called a Web Container) is Apache Tomcat, and it will be the basis for this tutorial. If you would like to use some other container than have a look at e.g. Jetty.</p>
<p>Now, lets start with the tutorial.</p>
<p><a href="http://www.gpsnavigatorsreview.com/garmin/garmin-nuvi-660-43-inch-widescreen-bluetooth-portable-gps-navigator/" ><strong>Garmin nüvi 660</strong> wide screen GPS review</a></p>
<p>First you need to have JDK installed, which can be downloaded from http://java.sun.com/javase/downloads/index.jsp (choose JDK 5.0 Update x), and then install the JDK (if there are any problems than let me know I'll try to clarify this).</p>
<p>Secondly we will download tomcat from <a href="http://tomcat.apache.org/download-55.cgi">here</a> and for this tutorial I use Tomcat 5.5.17. For the lazy ones, here is a <a href="http://www.apache.org/dist/tomcat/tomcat-5/v5.5.17/bin/apache-tomcat-5.5.17.zip">direct link to 5.5.17 version</a>. </p>
<p>Before running Tomcat, there should be a JAVA_HOME variable defined in environment, how to do it:</p>
<p><strong>in Windows</strong>: Go to Control Panel, double click System, select Advanced tab, and click "Environment Variables" button. Check if there is no JAVA_HOME defined in user and system variables pointing to JDK directory, if there is then double check that it points correctly to a JDK (not JRE), if there is no such variable then click New button in system, give a JAVA_HOME name, and paste in the directory where your JDK is sitting (on my system it is "C:\Program Files\Java\jdk1.5.0_06").</p>
<p><strong>in Unix</strong>: Find where you have JDK, usually "where java" or "whereis java" should give you the path to the java executable (not a symlink!), on my system it is "/opt/sun-jdk-1.5.0.07". Depending on the shell you have, setting variable will be "export JAVA_HOME=..." or "setenv JAVA_HOME ..." (replace the dots with the path to the JDK). BTW, please feel free to ask be questions if you have any problem.</p>
<p>Unzip the archive in a directory you want and start the file startup.bat. Unix users should first do "chmod u+x *.sh", and then run startup.sh. If everything works fine then you should see something like this:</p>
<div class="syntax_hilite">
<div id="code-11">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Using CATALINA_BASE:&nbsp; &nbsp;/home/krzyk/apache-tomcat-<span style="color:#800000;color:#800000;">5</span>.<span style="color:#800000;color:#800000;">5</span>.<span style="color:#800000;color:#800000;">17</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Using CATALINA_HOME:&nbsp; &nbsp;/home/krzyk/apache-tomcat-<span style="color:#800000;color:#800000;">5</span>.<span style="color:#800000;color:#800000;">5</span>.<span style="color:#800000;color:#800000;">17</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Using CATALINA_TMPDIR: /home/krzyk/apache-tomcat-<span style="color:#800000;color:#800000;">5</span>.<span style="color:#800000;color:#800000;">5</span>.<span style="color:#800000;color:#800000;">17</span>/temp</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Using JRE_HOME:&nbsp; &nbsp; &nbsp; &nbsp;/usr/lib/jvm/sun-jdk-<span style="color:#800000;color:#800000;">1</span>.<span style="color:#800000;color:#800000;">5</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Now go to the address http://localhost:8080/ and see if it really works, you should see a page about Apache Tomcat.</p>
<p>If it is correct then....</p>
<p><img src="http://www.coderookie.com/wp-content/uploads/2006/09/566435_fourth_of_july.jpg" alt="fireworks" class="center"/></p>
<p>Congratulations! You have just made the first step into the J2EE world ;). </p>
<p><strong>2. Writing the first servlet</strong></p>
<p>Now it's time to get our hands dirty and write some code, but first we need to make some essential directories.</p>
<p>Create a directory where your project will be kept (I'll name it FirstApplication), under that directory<br />
create following dirs:</p>
<div class="syntax_hilite">
<div id="code-12">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">WEB-INF</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">WEB-INF/src</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">WEB-INF/classes </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>If you use some java IDE (e.g. Eclipse or NetBeans) for developing code, then mark the "src" directory as the one containing the source, and the "classes" as the one with binaries. For the rest of us lets make directory for com.coderookie, that is under "src" mkdir "com" and "com/coderookie".</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-3846130357517643";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
google_ad_channel ="9138240644";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_text = "000000";
google_color_url = "FFFFFF";
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<p>Go into the WEB-INF/src/com/coderookie dir, and create a file called FirstServlet.java with the following code:</p>
<div class="syntax_hilite">
<div id="java-13">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">package com.<span style="color: #006600;">coderookie</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #a1a100;">import javax.servlet.http.HttpServlet;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #a1a100;">import javax.servlet.http.HttpServletRequest;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #a1a100;">import javax.servlet.http.HttpServletResponse;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #a1a100;">import java.io.PrintWriter;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #a1a100;">import java.io.IOException;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FirstServlet <span style="color: #000000; font-weight: bold;">extends</span> HttpServlet <span style="color: #808080; font-style: italic;">//#1</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> doGet<span style="color: #66cc66;">&#40;</span>HttpServletRequest request,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; HttpServletResponse response<span style="color: #66cc66;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?q=allinurl%3AIOException+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">IOException</span></a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">//#2</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=allinurl%3APrintWriter+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">PrintWriter</span></a> out = response.<span style="color: #006600;">getWriter</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">//#3</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; out.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"Hello world!"</span><span style="color: #66cc66;">&#41;</span>;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">//#4</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>And now for explanations:<br />
#1 - servlet is really just a class that extends HttpServlet and implements certain methods</p>
<p>#2 - here is one of those mentioned methods doGet is responsible for handling the GET method (pretty obvious) of HTTP protocol (see <a href="http://en.wikipedia.org/wiki/HTTP#Request_methods">this wiki page for more information on HTTP methods</a>)</p>
<p>#3 - HttpServletResponse has a method that allows us to get a PrintWriter for the web page output</p>
<p>#4 - using PrintWriter methods we can write anything (using a web page) to the user that entered our servlet</p>
<p>Besides writing the code, we need to write a web descriptor for our application. Descriptor is used to inform web container about the applications ingredients. You must put there such information as: what are the names your servlets, how URLs should map to a certain servlet and much more (I'll cover more in other posts).</p>
<p>Create a web.xml file in WEB-INF directory:</p>
<div class="syntax_hilite">
<div id="xml-14">
<div class="xml">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;web</span>-app <span style="color: #000066;">xmis</span>=<span style="color: #ff0000;">"http://java.sun.com/xml/ns/j2ee"</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; xmlns:<span style="color: #000066;">xsi</span>=<span style="color: #ff0000;">"http://www.w3.org/2001/XMLSchema-instance"</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; xsi:<span style="color: #000066;">schemaLocation</span>=<span style="color: #ff0000;">"http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000066;">version</span>=<span style="color: #ff0000;">"2.4"</span><span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;servlet<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;servlet</span>-name<span style="font-weight: bold; color: black;">&gt;</span></span>SimpleServlet<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/servlet</span>-name<span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;servlet</span>-class<span style="font-weight: bold; color: black;">&gt;</span></span>com.coderookie.FirstServlet<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/servlet</span>-class<span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/servlet<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;servlet</span>-mapping<span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;servlet</span>-name<span style="font-weight: bold; color: black;">&gt;</span></span>SimpleServlet<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/servlet</span>-name<span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;url</span>-pattern<span style="font-weight: bold; color: black;">&gt;</span></span>/first<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/url</span>-pattern<span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/servlet</span>-mapping<span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/web</span>-app<span style="font-weight: bold; color: black;">&gt;</span></span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Don't worry about the long <web-app...> tag, you will copy it between different applications, you do not have to memorize it.</p>
<p>Go to the WEB-INF directory and run following command (you MUST replace TOMCAT with the exact location of your tomcat installation e.g. "/home/krzyk/apache-tomcat-5.5.17" or "d:\tomcat")</p>
<p>For <strong>Windows</strong> users (note "<strong>\</strong>" and "<strong>;</strong>"):</p>
<div class="syntax_hilite">
<div id="code-15">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">javac -cp TOMCAT\common\lib\servlet-api.<span style="">jar</span>;classes;.</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;-d classes src\com\coderookie\FirstServlet.<span style="">java</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>For <strong>Unix</strong> ones (here we have "<strong>/</strong>" and "<strong>:</strong>"):</p>
<div class="syntax_hilite">
<div id="code-16">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">javac -cp TOMCAT/common/lib/servlet-api.<span style="">jar</span>:classes:.</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;-d classes src/com/coderookie/FirstServlet.<span style="">java</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>It is time to test your servlet, copy your projects directory to TOMCAT/webapps (again change TOMCAT to the directory where you have unzipped Tomcat). Change directory to TOMCAT/bin and do shutdown.bat (or shutdown.sh) and then startup.bat (or startup.sh).</p>
<p>If there is no error then type in a browser http://localhost:8080/PROJECT_NAME/first (where PROJECT_NAME is a name of your projects directory in webapps directory). Notice that "/first" is the url-pattern from web.xml file.</p>
<p>You should see a "Hello world!" displayed on this page, if it is than we have another success!</p>
<p><a href="http://www.flickr.com/photos/67566398@N00/183579318/"><img src="http://www.coderookie.com/wp-content/uploads/2006/09/183579318_2cfc891466_m.jpg" alt="combine" align="left" /></a><br />
<strong>3. Combining servlet and JSP</strong></p>
<p>What if you would like to print a HTML page using the servlet above ? </p>
<p>Using println for the whole page would be a horror, just have a look at the example presented below:</p>
<ol>
&nbsp;
</ol>
<div class="syntax_hilite">
<div id="java-17">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">out.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"&lt;HTML&gt;"</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">out.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"&lt;HEAD&gt;"</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">out.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"&lt;TITLE&gt;"</span> + <span style="color: #ff0000;">"My first page"</span> + <span style="color: #ff0000;">"&lt;/TITLE&gt;"</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">out.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"&lt;/HEAD&gt;"</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">out.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"&lt;BODY&gt;"</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">out.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"&lt;/BODY&gt;"</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">out.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"&lt;/HTML&gt;"</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>To much out.println, don't you think ? But have no fear, JSP to the rescue.</p>
<p>Create a file named hi.jsp (or whatever you like) and place is either in WEB-INF or in the directory of the project (the one that is higher then WEB-INF):</p>
<div class="syntax_hilite">
<div id="xml-18">
<div class="xml">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;html<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;head<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;title<span style="font-weight: bold; color: black;">&gt;</span></span></span>${title}<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/title<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/head<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;body<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; Hi World !</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/body<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/html<span style="font-weight: bold; color: black;">&gt;</span></span></span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>This page contains an EL (Expression Language) structure ${title}, which can be set from the servlet, we will use this page as a view for our servlet.</p>
<p>Change the code of FirstServlet to this one:</p>
<div class="syntax_hilite">
<div id="java-19">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">package com.<span style="color: #006600;">coderookie</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #a1a100;">import javax.servlet.http.HttpServlet;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #a1a100;">import javax.servlet.http.HttpServletRequest;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #a1a100;">import javax.servlet.http.HttpServletResponse;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #a1a100;">import javax.servlet.RequestDispatcher;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #a1a100;">import javax.servlet.ServletContext;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #a1a100;">import javax.servlet.ServletException;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #a1a100;">import java.io.IOException;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FirstServlet <span style="color: #000000; font-weight: bold;">extends</span> HttpServlet</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> doGet<span style="color: #66cc66;">&#40;</span>HttpServletRequest request,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; HttpServletResponse response<span style="color: #66cc66;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">throws</span> ServletException, <a href="http://www.google.com/search?q=allinurl%3AIOException+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">IOException</span></a></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; request.<span style="color: #006600;">setAttribute</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"title"</span>, <span style="color: #ff0000;">"My first super page"</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// #1</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; ServletContext app = getServletContext<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; RequestDispatcher dispatcher =</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; app.<span style="color: #006600;">getRequestDispatcher</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"/hi.jsp"</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">//#2</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; dispatcher.<span style="color: #006600;">forward</span><span style="color: #66cc66;">&#40;</span>request,response<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>
Explanations:<br />
#1 - for now, just assume, that everything dynamic that you wan to display on the JSP page, should be previously set by the setAttribute of the HttpServletRequest, first parameter is the name which you will use to display the second argument in JSP (here "title", so in JSP it will be ${title} and will be replaced by Tomcat with "My first super page")</p>
<p>#2 - we must get a ServletContext (this object keeps global configuration for the application), and from it, the RequestDispatcher for the JSP page, in the parameter you need to provide the path to the JSP, relative to the application folder (that is, one step higher than WEB-INF), OK this might be confusing, so look at the ASCII drawing below. And finally, we forward the control to the JSP page by calling forward method of the RequestDispatcher.</p>
<div class="syntax_hilite">
<div id="code-20">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">PROJECT_NAME</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">|</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">|- hi.<span style="">jsp</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">|</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;-- WEB-INF</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; |</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; |</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp;- classes</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; |</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp;- src</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; |</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp;- <span style="color:#006600; font-weight:bold;">&#40;</span>hi.<span style="">jsp</span> - you can put it even here<span style="color:#006600; font-weight:bold;">&#41;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Now make your favorite drink ready, and go to the http://localhost:8080/PROJECT_NAME/first</p>
<p>Probably you should see a page with title "My first super page", and "Hi World!" text on it. If it works, then once again, you rule (and I haven't made any mistakes ;).</p>
<p><img src="http://www.coderookie.com/wp-content/uploads/2006/09/234620006_892d12a8b4_m.jpg" alt="234620006_892d12a8b4_m.jpg" alt="Have fun" class="alignright" />This is it for now, stay tuned for the following parts, introducing more advanced topics, you can now review material from this tutorial, play around with the JSP and servlet, <strong>have some fun</strong>. If you develop an interesting application using the very basic information above, please send me an info, a link to the application or the source code. The next part of this tutorial will be about Model-View-Controler design pattern and how to apply it to servlets and JSP.</p>
<p><script>reddit_url='http://www.coderookie.com/2006/java/a-really-easy-introduction-to-servlets-and-jsp-part-i/'</script><br />
<script language="javascript" src="http://reddit.com/button.js?t=3"></script> &nbsp;&nbsp; <script>
digg_url = 'http://digg.com/programming/A_Really_Easy_Introduction_to_JSP_and_Servlets_J2EE';
</script><script src="http://digg.com/api/diggthis.js"></script> </p>
<p><a href="http://del.icio.us/post?url=http%3A%2F%2Fwww.coderookie.com%2F2006%2Fjava%2Fa-really-easy-introduction-to-servlets-and-jsp-part-i%2F&#038;title=A+Really+Easy+Introduction+to+Servlets+and+JSP.+Part">Add to del.icio.us <img src="http://www.coderookie.com/wp-content/themes/unsleepable/images/delicious.png" alt="delicious" /></a></p>

<div class="sociable">
<span class="sociable_tagline">
<strong>Share and Enjoy:</strong>
	<span>These icons link to social bookmarking sites where readers can share and discover new web pages.</span>
</span>
<ul>
	<li><a href="http://www.dzone.com/links/add.html?url=http%3A%2F%2Fwww.coderookie.com%2F2006%2Fjava%2Fa-really-easy-introduction-to-servlets-and-jsp-part-i%2F&amp;title=A+Really+Easy+Introduction+to+JSP+and+Servlets+and+.+Part+I" title="DZone" onfocus="sociable_description_link(this, 'description')"><img src="http://www.coderookie.com/wp-content/plugins/sociable/images/dzone.png" title="DZone" alt="DZone" class="sociable-hovers" /></a></li>
	<li><a href="http://del.icio.us/post?url=http%3A%2F%2Fwww.coderookie.com%2F2006%2Fjava%2Fa-really-easy-introduction-to-servlets-and-jsp-part-i%2F&amp;title=A+Really+Easy+Introduction+to+JSP+and+Servlets+and+.+Part+I" title="del.icio.us"><img src="http://www.coderookie.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a></li>
	<li><a href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.coderookie.com%2F2006%2Fjava%2Fa-really-easy-introduction-to-servlets-and-jsp-part-i%2F&amp;title=A+Really+Easy+Introduction+to+JSP+and+Servlets+and+.+Part+I" title="Digg" onfocus="sociable_description_link(this, 'bodytext')"><img src="http://www.coderookie.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a></li>
	<li><a href="http://technorati.com/faves?add=http%3A%2F%2Fwww.coderookie.com%2F2006%2Fjava%2Fa-really-easy-introduction-to-servlets-and-jsp-part-i%2F" title="Technorati"><img src="http://www.coderookie.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a></li>
</ul>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.coderookie.com/2006/java/a-really-easy-introduction-to-servlets-and-jsp-part-i/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>The Pseudocode Programming Process</title>
		<link>http://www.coderookie.com/2006/tutorial/the-pseudocode-programming-process/</link>
		<comments>http://www.coderookie.com/2006/tutorial/the-pseudocode-programming-process/#comments</comments>
		<pubDate>Thu, 17 Aug 2006 19:19:39 +0000</pubDate>
		<dc:creator>Kris</dc:creator>
				<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://www.coderookie.com/2006/08/17/the-pseudocode-programming-process/</guid>
		<description><![CDATA[  Pseudocode interesting technique that I found in Code Complete, Second Edition by Steve McConnell. 
PPP is a way of developing routines by writing pseudocode for them  in the first place (not a new topic) and after that, making the pseudocode a comment in the routine (functions, procedure, method or whatever your language [...]]]></description>
			<content:encoded><![CDATA[<p><img class="post_img" src="http://www.coderookie.com/wp-content/uploads/2006/08/code.jpg" alt="Code on a monitor" align="left" alt=""/>  Pseudocode interesting technique that I found in <a href="http://www.amazon.com/gp/product/0735619670?ie=UTF8&amp;tag=bookreviewbyk-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0735619670">Code Complete, Second Edition</a><img src="http://www.assoc-amazon.com/e/ir?t=coderookie-20&#038;l=as2&#038;o=1&#038;a=0735619670" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /> by Steve McConnell. </p>
<p>PPP is a way of developing routines by writing pseudocode for them  in the first place (not a new topic) and after that, <strong>making the pseudocode a comment</strong> in the routine (functions, procedure, method or whatever your language calls them). After comments are in the empty routine, the <string>last</strong> thing to do is write the code below appropriate comment lines. </p>
<p>I know it isn't the clearest explanation, here look below at the example that I prepared.<br />
<span id="more-5"></span><br />
We'll work on a routine responsible for query searching in a search engine (based on my very simple &#038; ugly search engine.)</p>
<p><a href="http://www.gpsnavigatorsreview.com/magellan/magellan-maestro-3100-35-inch-portable-gps-navigator/" title="Magellan 3100 GPS review">Magellan Maestro 3100 a best value GPS for vehicles, review</a></p>
<p>First, let's write the header comment for the routine:</p>
<div class="syntax_hilite">
<div id="php-24">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#b0b0ff; font-style:italic;">// Searches for query in a set of pages, every word</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#b0b0ff; font-style:italic;">// is searched independently, and then results of all</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#b0b0ff; font-style:italic;">// search operations are concatenated. </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Pretty simple, but should state what the routine will do. Next I will use a simple English statements as pseudocode and write the body of the routine:</p>
<div class="syntax_hilite">
<div id="php-25">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#b0b0ff; font-style:italic;">// Searches for query in a set of pages, every word</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#b0b0ff; font-style:italic;">// is searched independently, and then results of all</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#b0b0ff; font-style:italic;">// search operations are concatenated.</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#b0b0ff; font-style:italic;">// split query into separate words</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#b0b0ff; font-style:italic;">// for each word in the query</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#b0b0ff; font-style:italic;">// get pages containing the word</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#b0b0ff; font-style:italic;">// from the database</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#b0b0ff; font-style:italic;">// for each page</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#b0b0ff; font-style:italic;">// if this page is in the score table</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#b0b0ff; font-style:italic;">// add current word occurrences on the</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#b0b0ff; font-style:italic;">// page to the score table for given page</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#b0b0ff; font-style:italic;">// if this page is not in the score table</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#b0b0ff; font-style:italic;">// add the page to the score table</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#b0b0ff; font-style:italic;">// and store current word occurrences for</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#b0b0ff; font-style:italic;">// this page in the score table</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#b0b0ff; font-style:italic;">// create container for pages to return</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#b0b0ff; font-style:italic;">// for each page in the score table</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#b0b0ff; font-style:italic;">// retrieve page info from database and store it</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#b0b0ff; font-style:italic;">// in the container</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#b0b0ff; font-style:italic;">// return the container </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>It is not the most complicated routine in the word but will do.</p>
<p>Here's the code added for this routine:</p>
<div class="syntax_hilite">
<div id="php-26">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#b0b0ff; font-style:italic;">// Searches for query in a set of pages, every word</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#b0b0ff; font-style:italic;">// is searched independently, and then results of all</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#b0b0ff; font-style:italic;">// search operations are concatenated.</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">function</span> search<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$query</span>, <span style="color:#0000FF;">$from</span>, <span style="color:#0000FF;">$amount</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#b0b0ff; font-style:italic;">// split query into separate words</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#0000FF;">$text</span> = <a href="http://www.php.net/preg_replace"><span style="color:#000066;">preg_replace</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"/[^a-zA-Z ]/"</span>, <span style="color:#FF0000;">""</span>, <span style="color:#0000FF;">$query</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#0000FF;">$words</span> = <a href="http://www.php.net/preg_split"><span style="color:#000066;">preg_split</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"/ +/"</span>, <span style="color:#0000FF;">$text</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#b0b0ff; font-style:italic;">// for each word in the query</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#616100;">foreach</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$words</span> <span style="color:#616100;">as</span> <span style="color:#0000FF;">$word</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp;<span style="color:#b0b0ff; font-style:italic;">// get pages containing the word from</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp;<span style="color:#b0b0ff; font-style:italic;">// the database</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp;<span style="color:#0000FF;">$pages</span> = <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">dataStore</span>-&gt;<span style="color:#006600;">getPagesWithWord</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$word</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp;<span style="color:#b0b0ff; font-style:italic;">// for each page</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp;<span style="color:#616100;">foreach</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$pages</span> <span style="color:#616100;">as</span> <span style="color:#0000FF;">$id</span> =&gt; <span style="color:#0000FF;">$page</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp;<span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#b0b0ff; font-style:italic;">// if this page is in the score table</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><a href="http://www.php.net/isset"><span style="color:#000066;">isset</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">pages</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF;">$id</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#b0b0ff; font-style:italic;">// add current word occurances on the page</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#b0b0ff; font-style:italic;">// to the score table for given page</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">pages</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF;">$id</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">"score"</span><span style="color:#006600; font-weight:bold;">&#93;</span> +=</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$page</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">"occurances"</span><span style="color:#006600; font-weight:bold;">&#93;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#b0b0ff; font-style:italic;">// if this page is not in the score table</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#616100;">else</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#b0b0ff; font-style:italic;">// add the page to the score table</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#b0b0ff; font-style:italic;">// and store current word occurrences for</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#b0b0ff; font-style:italic;">// this page in the score table</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">pages</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF;">$id</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#0000FF;">$page</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp;<span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#b0b0ff; font-style:italic;">// create container for pages to return</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#0000FF;">$webPages</span> = <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#b0b0ff; font-style:italic;">// for each page in the score table</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#616100;">foreach</span> <span style="color:#006600; font-weight:bold;">&#40;</span><a href="http://www.php.net/array_keys"><span style="color:#000066;">array_keys</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">pages</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#616100;">as</span> <span style="color:#0000FF;">$pageId</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#b0b0ff; font-style:italic;">// retrieve page info from database and store in</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#b0b0ff; font-style:italic;">// the container</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000FF;">$webPages</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">dataStore</span>-&gt;<span style="color:#006600;">getPage</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$pageId</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#b0b0ff; font-style:italic;">// return the container</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#616100;">return</span> <span style="color:#0000FF;">$webPages</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>The code above is in PHP but the same could be done for any other language, that's the beauty of pseudocode :) . You could give the pseudocode to some other programmer for a review and he doesn't have to know the lang in which it is written.</p>
<p>Some useful notes (once again taken from <a href="http://www.amazon.com/gp/product/0735619670?ie=UTF8&amp;tag=bookreviewbyk-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0735619670">Code Complete, Second Edition</a><img src="http://www.assoc-amazon.com/e/ir?t=coderookie-20&#038;l=as2&#038;o=1&#038;a=0735619670" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />) on how to write the pseudocode:</p>
<ol>
<li>use English-like words to describe the code</li>
<li>do not use syntactic elements of the programming language</li>
<li>the pseudocode should be written at the level of intent ("whats" not "hows")</li>
<li>it should be enough low level to enable developer to write code based on it</li>
</ol>
<p>That's it for this post, give me a hint if you liked the code, or better, try to write some more examples of the pseudocode.</p>
<p><script>reddit_url='http://www.coderookie.com/2006/tutorial/the-pseudocode-programming-process/'</script><br />
<script language="javascript" src="http://reddit.com/button.js?t=3"></script> &nbsp;&nbsp; <script></p>
<p><!--adsense--></p>

<div class="sociable">
<span class="sociable_tagline">
<strong>Share and Enjoy:</strong>
	<span>These icons link to social bookmarking sites where readers can share and discover new web pages.</span>
</span>
<ul>
	<li><a href="http://www.dzone.com/links/add.html?url=http%3A%2F%2Fwww.coderookie.com%2F2006%2Ftutorial%2Fthe-pseudocode-programming-process%2F&amp;title=The+Pseudocode+Programming+Process" title="DZone" onfocus="sociable_description_link(this, 'description')"><img src="http://www.coderookie.com/wp-content/plugins/sociable/images/dzone.png" title="DZone" alt="DZone" class="sociable-hovers" /></a></li>
	<li><a href="http://del.icio.us/post?url=http%3A%2F%2Fwww.coderookie.com%2F2006%2Ftutorial%2Fthe-pseudocode-programming-process%2F&amp;title=The+Pseudocode+Programming+Process" title="del.icio.us"><img src="http://www.coderookie.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a></li>
	<li><a href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.coderookie.com%2F2006%2Ftutorial%2Fthe-pseudocode-programming-process%2F&amp;title=The+Pseudocode+Programming+Process" title="Digg" onfocus="sociable_description_link(this, 'bodytext')"><img src="http://www.coderookie.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a></li>
	<li><a href="http://technorati.com/faves?add=http%3A%2F%2Fwww.coderookie.com%2F2006%2Ftutorial%2Fthe-pseudocode-programming-process%2F" title="Technorati"><img src="http://www.coderookie.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a></li>
</ul>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.coderookie.com/2006/tutorial/the-pseudocode-programming-process/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

