<?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; Java</title>
	<atom:link href="http://www.coderookie.com/category/java/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, 10 Mar 2010 12:37:57 +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>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>9</slash:comments>
		</item>
		<item>
		<title>Python versus Java</title>
		<link>http://www.coderookie.com/2006/java/python-versus-java/</link>
		<comments>http://www.coderookie.com/2006/java/python-versus-java/#comments</comments>
		<pubDate>Thu, 31 Aug 2006 09:22:01 +0000</pubDate>
		<dc:creator>Kris</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.coderookie.com/2006/java/python-versus-java/</guid>
		<description><![CDATA[Ever wondered why there are so many people that program in Python, although Java (along with .NET) is the main enterprise language? You can find out in this post at bitworking.org.




Joe talks about the things (technical term!) that Python has and Java misses. Here is the list of those abstractions with my comments and ways [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.flickr.com/photos/ecosnake/215576226/"><img src="http://static.flickr.com/62/215576226_c60840e1df_m.jpg" alt="python" align="left" /></a>Ever wondered why there are so many people that program in Python, although Java (along with .NET) is the main enterprise language? You can find out in <a href="http://bitworking.org/news/Python_isnt_Java_without_the_compile">this post</a> at bitworking.org.<!--adsense#posts_small--></p>
<p>Joe talks about the things (technical term!) that Python has and Java misses. Here is the list of those abstractions with my comments and ways of simulating (give me a feedback with your propositions):<br />
<span id="more-10"></span></p>
<p><a href="http://www.gpsnavigatorsreview.com/garmin/garmin-nuvi-360-35-inch-bluetooth-portable-gps-navigator/" title="Garmin nuvi 360 info">Review of <strong>Garmin nüvi 360</strong> GPS with bargain price</a></p>
<p>   <strong>1.  First-class functions</strong><br />
This is known from C++, where you can define function pointer that can be passed around as e.g. a parameter to other function.</p>
<p>   <strong>2. Keyword parameters</strong><br />
OK, that's the one I would like to have in Java, it would add a better strictness to the language, now if you want to simulate this kind of behaviour you would have to create a class and add setters/getters.</p>
<p>   <strong>3. Default parameters</strong><br />
I don't know why did Sun skip this while creating Java, it is a very useful abstraction. The simulations of this can be achieved by many overloaded methods/constructors.</p>
<div class="syntax_hilite">
<div id="java-23">
<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;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> A <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; </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: #000000; font-weight: bold;">private</span> <span style="color: #993333;">int</span> value;</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: #993333;">void</span> calculate<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> a<span style="color: #66cc66;">&#41;</span> <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; value = 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; <span style="color: #66cc66;">&#125;</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: #993333;">void</span> calculate<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <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; calculate<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;color:#800000;">10</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// this is the default</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: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<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>   <strong>4. Tuples</strong><br />
   <strong>5. Parallel assignment</strong><br />
   <strong>6. Efficient multiple return values</strong><br />
Here I can't give any proposition how to simulate this in Java (at some point arrays might be used). A very nice functionality of Python (and some other languages), especially in conjunction with the next one...<!--adsense--><br />
That is, returning a tuple out of a method. Again some may try to simulate it using arrays.</p>
<p>   <strong>7. Continuations</strong><br />
They are a way to save a function execution, and restore it at later time, it's as if you could jump out of a function during it's execution, and than, when by calling the function again, returning to the exact point where the function has been saved.</p>
<p>Here's an example to give you a better understanding of this. It is a function that will return values from the beginning to the end:</p>
<div class="syntax_hilite">
<div id="python-24">
<div class="python">
<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: #ff7700;font-weight:bold;">def</span> fun<span style="color: black;">&#40;</span>begin, end<span style="color: black;">&#41;</span>:</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; i = begin</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: #ff7700;font-weight:bold;">while</span> i &lt;end:</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: #ff7700;font-weight:bold;">yield</span><span style="color: black;">&#40;</span>i<span style="color: black;">&#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; i = i + <span style="color: #ff4500;color:#800000;">1</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;">f=fun<span style="color: black;">&#40;</span><span style="color: #ff4500;color:#800000;">10</span>,<span style="color: #ff4500;color:#800000;">100</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">f.<span style="color: black;">next</span><span style="color: black;">&#40;</span><span style="color: black;">&#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;">f.<span style="color: black;">next</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">f.<span style="color: black;">next</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Executing above code gives:<br />
<code>10<br />
11<br />
12<br />
</code></p>
<p>   <strong>8. User-defined operators</strong><br />
That one was probably a mistake, I don't know a way to add a user-defined operator to Python (give me a hint if I am wrong)</p>
<p>   <strong>9. Closures</strong><br />
In one of the <a href="http://www.coderookie.com/2006/java/closures-in-java/">previous posts</a> I mentioned about proposition to add closures to Java.</p>
<p>   <strong>10. Meta-programming</strong><br />
I haven't used much of this in Python, but it allows e.g. to assigning methods to classes on the fly. A nice functionality but I don't know how could it be used in a corporate environment, with more than 5 developers creating the code.</p>
<p><script>reddit_url='http://www.coderookie.com/2006/java/python-versus-java/'</script><br />
<script language="javascript" src="http://reddit.com/button.js?t=3"></script> &nbsp;&nbsp; <script></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%2Fpython-versus-java%2F&amp;title=Python+versus+Java" 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%2Fpython-versus-java%2F&amp;title=Python+versus+Java" 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%2Fpython-versus-java%2F&amp;title=Python+versus+Java" 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%2Fpython-versus-java%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/python-versus-java/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Closures in Java?</title>
		<link>http://www.coderookie.com/2006/java/closures-in-java/</link>
		<comments>http://www.coderookie.com/2006/java/closures-in-java/#comments</comments>
		<pubDate>Sat, 19 Aug 2006 08:43:45 +0000</pubDate>
		<dc:creator>Kris</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[News]]></category>

		<guid isPermaLink="false">http://www.coderookie.com/2006/08/19/closures-in-java/</guid>
		<description><![CDATA[TomTom ONE XL-S - a wide screen GPS
Just a short notice, found it while browsing the digg for programming stories.
There is a proposition to include closures in Java (probably 7.0), here is a pdf file with the proposition.

I am quite new to this functional programming concept, but to give you some idea what is it [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.gpsnavigatorsreview.com/tomtom/tomtom-one-xl-s-43-inch-widescreen-portable-gps-navigator/" title="A widescreen GPS from TomTom">TomTom ONE XL-S - a wide screen GPS</a></p>
<p><!--adsense#posts_small-->Just a short notice, found it while browsing the digg for programming stories.<br />
There is a proposition to include closures in Java (probably 7.0), here is a pdf file with <a href="http://blogs.sun.com/roller/resources/ahe/closures.pdf">the proposition</a>.</p>
<p><span id="more-6"></span></p>
<p>I am quite new to this functional programming concept, but to give you some idea what is it (example taken from the pdf file):</p>
<div class="syntax_hilite">
<div id="java-27">
<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;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">static</span> <span style="color: #993333;">void</span> main<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> args<span style="color: #66cc66;">&#41;</span> <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;</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: #808080; font-style: italic;">// here is the definition of function type</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: #993333;">int</span> plus2<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> x<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> x+<span style="color: #cc66cc;color:#800000;">2</span>; <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;">&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: #808080; font-style: italic;">// here we have an alias for the same function</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: #993333;">int</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span><span style="color: #66cc66;">&#41;</span> plus2b = plus2;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <a href="http://www.google.com/search?q=allinurl%3ASystem+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span>plus2b<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;color:#800000;">2</span><span style="color: #66cc66;">&#41;</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;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Maybe it doesn't look so great (aliases for functions, phi), but AFAIR there should be also a way to do this:</p>
<div class="syntax_hilite">
<div id="java-28">
<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;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">static</span> <span style="color: #993333;">void</span> main<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> args<span style="color: #66cc66;">&#41;</span> <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; <span style="color: #993333;">int</span> power<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> x, <span style="color: #993333;">int</span> y<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> <a href="http://www.google.com/search?q=allinurl%3AMath+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Math</span></a>.<span style="color: #006600;">pow</span><span style="color: #66cc66;">&#40;</span>y,x<span style="color: #66cc66;">&#41;</span>; <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;">&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: #808080; font-style: italic;">// definition of x^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: #993333;">int</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span><span style="color: #66cc66;">&#41;</span> power2 = power<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;color:#800000;">2</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;">&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; <span style="color: #808080; font-style: italic;">// should print 10^2&nbsp; = 100</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <a href="http://www.google.com/search?q=allinurl%3ASystem+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span>power2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;color:#800000;">10</span><span style="color: #66cc66;">&#41;</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;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p><!--adsense--></p>
<p>But I could not find it in the pdf file, so maybe there won't be such possibility.<br />
I'll try to introduce more "functional" concepts in some of the future posts, so stay tuned.</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%2Fclosures-in-java%2F&amp;title=Closures+in+Java%3F" 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%2Fclosures-in-java%2F&amp;title=Closures+in+Java%3F" 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%2Fclosures-in-java%2F&amp;title=Closures+in+Java%3F" 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%2Fclosures-in-java%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/closures-in-java/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
