<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Java Training Blog</title>
	
	<link>http://java.blogs.webucator.com</link>
	<description>Trainers' thoughts on Java and related technologies</description>
	<lastBuildDate>Fri, 03 Jun 2011 23:42:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/JavaTrainingBlog" /><feedburner:info uri="javatrainingblog" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Java Training: Learning the Basics</title>
		<link>http://feedproxy.google.com/~r/JavaTrainingBlog/~3/OmgmC_8iPZk/</link>
		<comments>http://java.blogs.webucator.com/2011/06/03/java-training-learning-the-basics/#comments</comments>
		<pubDate>Fri, 03 Jun 2011 23:42:33 +0000</pubDate>
		<dc:creator>Bob Clary</dc:creator>
				<category><![CDATA[Java Fundamentals]]></category>

		<guid isPermaLink="false">http://java.blogs.webucator.com/?p=328</guid>
		<description><![CDATA[Welcome to the first post in our Introduction to Java blog series! Join us for a multi-part series to introduce the key concepts derived from our Introduction to Java training class. And, if you have any suggestions for future blog posts, just leave a comment below! Let&#8217;s get kicked off with a discussion on the basics [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjava.blogs.webucator.com%2F2011%2F06%2F03%2Fjava-training-learning-the-basics%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fjava.blogs.webucator.com%2F2011%2F06%2F03%2Fjava-training-learning-the-basics%2F&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Welcome to the first post in our Introduction to Java blog series! Join us for a multi-part series to introduce the key concepts derived from our <a href="http://www.webucator.com/java/course/introduction-java-training-for-new-programmers.cfm">Introduction to Java training class</a>. And, if you have any suggestions for future blog posts, just leave a comment below! Let&#8217;s get kicked off with a discussion on the basics of Java for new programmers.</p>
<p><span id="more-328"></span></p>
<p>Lets get started by looking at some general syntax rules.</p>
<p>Java is <em>case-sensitive</em></p>
<ul>
<li><code>main()</code>,<code> Main()</code>, and <code>MAIN()</code> would all be different methods</li>
</ul>
<p>There are a limited number of reserved words that have a special meaning within Java.</p>
<ul>
<li>You may not use these words for your own variables or methods.</li>
<li>Examples: <code>public</code>, <code>void</code>, <code>static</code>, <code>do</code>, <code>for</code>, <code>while</code>, <code>if</code></li>
</ul>
<p>Most keyboard symbol characters (the set of characters other than alphabetic or numeric) have a special meaning.</p>
<p>Names may contain alphabetic characters, numeric characters, currency characters, and connecting characters such as the underscore ( <code>_</code> ) character.</p>
<ul>
<li>Names may not begin with a numeric character.</li>
<li>Note that the set of legal characters draws from the entire Unicode character set.</li>
<li>Also note that it is probably impossible to write a succinct set of rules about what are valid characters, other than to say a character, that when passed to <code>Character.isJavaIdentifierPart(char ch)</code>, results in a <code>true</code> value.</li>
</ul>
<p>The compiler parses your code by separating it into individual entities:</p>
<ul>
<li>names (of classes, variables, and methods)</li>
<li>command keywords</li>
<li>single or compound symbols (Compound symbols are when an operation is signified by a two-symbol combination.)</li>
<li>these entities are called <em>tokens</em> or <em>symbols</em> in computer science jargon</li>
</ul>
<p>Tokens may be separated by spaces, tabs, carriage returns, or by use of an <em>operator</em> (such as <code>+</code>, <code>-</code>, etc.)</p>
<ul>
<li>Since names may not contain spaces, tabs, or carriage returns, or operator characters, these characters imply a separation of what came before them from what comes after them.</li>
</ul>
<p>Extra <em>whitespace</em> is ignored</p>
<ul>
<li>Once the compiler knows that two items are separate, it ignores any additional separating whitespace characters (spaces, tabs, or carriage returns).</li>
</ul>
<h3>Java Statements</h3>
<p>A statement:</p>
<ul>
<li>is one step of code, which may take more than one line</li>
<li>ends with a semicolon (the <code>;</code> character)</li>
<li>can have multiple statements on one line</li>
</ul>
<p>Program execution is statement by statement, one statement at a time</p>
<ul>
<li>from top to bottom (if more than one statement on a line, left to right)</li>
</ul>
<p>Within a statement, execution of the individual pieces is not necessarily left to right</p>
<ul>
<li>there are concepts called <em>operator precedence </em>and <em>associativity</em> that determine the order of operations within a statement</li>
</ul>
<h3>Blocks of Code</h3>
<p>A block of code:</p>
<ul>
<li>is enclosed in curly braces &#8211; start with <code>{</code> and end with <code>}</code></li>
<li>consists of zero, one, or more statements</li>
<li>behaves like a single statement to the outside world</li>
<li>a complete method is a block</li>
<li>blocks may be nested &#8211; containing one or more blocks inside</li>
<li>generally, blocks belong to whatever comes before them (although it is perfectly OK to create a block that does not, this is almost never done)</li>
</ul>
<h3>Variables</h3>
<p>Variables store data that your code can use.</p>
<p>There are two fundamental categories of variables: <em>primitive data</em> and <em>references.</em></p>
<ul>
<li>With primitive data, the compiler names a memory location and uses to store the actual data -<br />
numeric values such as integers, floating point values, and the code values of single individual text characters are stored as primitives.</li>
<li>With references, the data is accessed indirectly &#8211; the compiler selects a memory location, associates it with the variable name, and stores in it a value that is effectively the memory address of the actual data.<br />
In Java, all objects and arrays are stored using references.</li>
</ul>
<h3>Declaring Variables</h3>
<p>Variables must be <em>declared</em> before they are used.</p>
<p>A declaration informs the compiler that you wish to:</p>
<ul>
<li>create an <em>identifier</em> that will be accepted by the compiler within a section of code (Exactly what that section is depends on how and where the variable is declared; that is the concept of <em>scope</em>, which will be addressed later.)</li>
<li>associate that identifier with a specified type of data, and enforce restrictions related to that in the remainder of your code</li>
<li>create a memory location for the specified type of data</li>
<li>associate the identifier with that memory location</li>
</ul>
<p>Java uses many specific data types; each has different properties in terms of size required and handling by the compiler.</p>
<ul>
<li>The declaration specifies the name and datatype.</li>
<li>Generally the declaration also defines the variable, meaning that it causes the compiler to allocate storage space in memory of the requested type&#8217;s size.</li>
<li>A declaration may also assign in initial value (to <em>initialize </em>the variable).</li>
<li>Multiple variables of the same type can be declared in a comma-separated list.</li>
</ul>
<table border="1" cellpadding="10">
<thead>
<tr>
<th>Code</th>
<th>Effect</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>int a;</code></td>
<td>declares the name <code>a</code> to exist, and allocates a memory location to hold a 32-bit integer</td>
</tr>
<tr>
<td><code>int a = 0;</code></td>
<td>same as above, and also assigns an initial value of 0</td>
</tr>
<tr>
<td><code>int a = 0, b, c = 3;</code></td>
<td>declares three integer variables and initializes two of them</td>
</tr>
</tbody>
</table>
<p>Note that different languages have different rules regarding trying to read data from variables that have not been initialized.</p>
<ul>
<li>Some languages just let the value be whatever happened to be in that memory location already (from some previous operation).</li>
<li>Some languages initialize automatically to 0.</li>
<li>Some languages give a compiler or runtime error.</li>
<li>Java uses both of the last two: local variables within methods must be initialized before attempting to use their value, while variables that are fields within objects are automatically set to zero</li>
</ul>
<p>Now, that was a quick look into some of the basic fundamentals of Java. If you want to learn more about Java, stay tuned for our next blog installment in the series, and check out all of our upcoming <a href="http://www.webucator.com/java/index.cfm">Java training</a> options. <strong>To receive the latest news on our blogs, classes, and tutorials, sign up for our free newsletter</strong>. <a title="Newsletter" href="http://www.webucator.com/webunews/index.cfm">Click here to sign up</a>.</p>
<img src="http://feeds.feedburner.com/~r/JavaTrainingBlog/~4/OmgmC_8iPZk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://java.blogs.webucator.com/2011/06/03/java-training-learning-the-basics/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://java.blogs.webucator.com/2011/06/03/java-training-learning-the-basics/</feedburner:origLink></item>
		<item>
		<title>Java Memory Management</title>
		<link>http://feedproxy.google.com/~r/JavaTrainingBlog/~3/8x_xST1OfxU/</link>
		<comments>http://java.blogs.webucator.com/2010/11/12/java-memory-management/#comments</comments>
		<pubDate>Fri, 12 Nov 2010 20:09:30 +0000</pubDate>
		<dc:creator>rsakowski</dc:creator>
				<category><![CDATA[EJB]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Java EE]]></category>
		<category><![CDATA[Java Fundamentals]]></category>
		<category><![CDATA[JDBC]]></category>
		<category><![CDATA[JSP]]></category>

		<guid isPermaLink="false">http://java.blogs.webucator.com/?p=287</guid>
		<description><![CDATA[In C/C++ memory management is handled by the programmer. Even after a programmer has put in a lot of effort to ensure the code is free of memory management problems, the more likely it is that they are likely to exist. The risk of these bugs increases with code size and complexity.

On the other hand, the Java programming language and runtime environments go a long way to eliminate these problems. Here's how:

]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjava.blogs.webucator.com%2F2010%2F11%2F12%2Fjava-memory-management%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fjava.blogs.webucator.com%2F2010%2F11%2F12%2Fjava-memory-management%2F&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>In C/C++ memory management is handled by the programmer. Even after a programmer has put in a lot of effort to ensure the code is free of memory management problems, the more likely it is that they are likely to exist. The risk of these bugs increases with code size and complexity.<span id="more-287"></span></p>
<p>On the other hand, the <strong>Java programming language</strong> and <strong>runtime environments</strong> go a long way to eliminate these problems. Here&#8217;s how:</p>
<ul>
<li>In Java, memory is allocated only to objects. There is no explicit allocation of memory, only the creation of new objects.</li>
<li>The <strong>Java Virtual Machine</strong> (<strong>JVM</strong>) employs a garbage collector that reclaims the memory occupied by an object once it determines that it is no longer accessible. This automatic process makes it safe to throw away unneeded object references because the garbage collector does not collect the object if it is still needed elsewhere.</li>
<li>Java makes it easy to let go of an entire &#8220;tree&#8221; of objects by setting the reference to the tree&#8217;s root to null; the garbage collector will then reclaim all the objects (unless some of the objects are needed elsewhere).</li>
</ul>
<p>So what about<strong> memory leaks</strong> caused by poor program design? Unfortunately, this can still be a problem.</p>
<p>In these programs unnecessary object references linger in long-lived parts of the application and prevent the garbage collector from reclaiming objects that are no longer needed. Most often, properly encapsulating object references in various parts of the code solves the problem.</p>
<p>Another design flaw occurs at the algorithm level. Not closing JDBC connections properly when using connection pools, threads left running after they have finished their work, and objects retaining their reference to legacy objects are typical examples. The use of Collection objects will magnify this problem.</p>
<p>Programmers should design applications with an object&#8217;s lifecycle in mind, rather than only rely on the features of a JVM implementation. Memory problems can be mitigated by letting go of object references to legacy classes as soon as possible. The KL Group&#8217;s <strong>JProbe</strong> tool can help identify possible sources of memory leaks by showing which objects are holding on to references passed their prime.</p>
<p>Interested in more? Check out our upcoming <a href="http://www.webucator.com/java/index.cfm">Java training classes</a>!</p>
<img src="http://feeds.feedburner.com/~r/JavaTrainingBlog/~4/8x_xST1OfxU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://java.blogs.webucator.com/2010/11/12/java-memory-management/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://java.blogs.webucator.com/2010/11/12/java-memory-management/</feedburner:origLink></item>
		<item>
		<title>JavaServerPages as Servlets</title>
		<link>http://feedproxy.google.com/~r/JavaTrainingBlog/~3/r4dNccwVtbs/</link>
		<comments>http://java.blogs.webucator.com/2010/08/10/javaserverpages-as-servlets/#comments</comments>
		<pubDate>Wed, 11 Aug 2010 01:16:02 +0000</pubDate>
		<dc:creator>rsakowski</dc:creator>
				<category><![CDATA[Java EE]]></category>
		<category><![CDATA[JSP]]></category>
		<category><![CDATA[Tomcat]]></category>

		<guid isPermaLink="false">http://java.blogs.webucator.com/?p=205</guid>
		<description><![CDATA[A common misconception about <strong>JavaServer Pages</strong> is that they are executed by the web container. Actually they only supply the <strong>web container</strong> with the instructions it needs to generate the Java code for a specialized servlet. That generated code is then compiled by the web container, and the resulting class is executed. To illustrate this concept, consider the simple JSP listing below:]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjava.blogs.webucator.com%2F2010%2F08%2F10%2Fjavaserverpages-as-servlets%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fjava.blogs.webucator.com%2F2010%2F08%2F10%2Fjavaserverpages-as-servlets%2F&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>A common misconception about <strong>JavaServer Pages</strong> is that they are executed by the web container. Actually they only supply the <strong>web container</strong> with the instructions it needs to generate the Java code for a specialized servlet. That generated code is then compiled by the web container, and the resulting class is executed.  To illustrate this concept, consider the simple JSP listing below:</p>
<p><span id="more-205"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>html<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>head<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>title<span style="color: #339933;">&gt;</span>JSP to Servlet<span style="color: #339933;">&lt;/</span>title<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;/</span>head<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>jsp<span style="color: #339933;">:</span>useBean id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;bean&quot;</span> <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;com.webucator.bean.NameBean&quot;</span><span style="color: #339933;">/&gt;</span>
		<span style="color: #339933;">&lt;</span>jsp<span style="color: #339933;">:</span>setProperty name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;bean&quot;</span> property<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;firstName&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Roger&quot;</span><span style="color: #339933;">/&gt;</span>
		<span style="color: #339933;">&lt;</span>jsp<span style="color: #339933;">:</span>setProperty name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;bean&quot;</span> property<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;lastName&quot;</span> 
			value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Sakowski&quot;</span><span style="color: #339933;">/&gt;</span>
		My name is<span style="color: #339933;">:</span> 
		<span style="color: #339933;">&lt;</span>jsp<span style="color: #339933;">:</span>getProperty name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;bean&quot;</span> property<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;fullName&quot;</span><span style="color: #339933;">/&gt;</span>
	<span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>When this page is requested, the following is displayed in a web browser:</p>
<p class="style1">My name is: Roger Sakowski</p>
<p>It seems pretty straightforward, but it’s not. The listing below is the generated source code produced by <strong>Tomcat</strong>’s web container:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">org.apache.jsp</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.jsp.*</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">class</span> MyJsp_jsp 
<span style="color: #000000; font-weight: bold;">extends</span> org.<span style="color: #006633;">apache</span>.<span style="color: #006633;">jasper</span>.<span style="color: #006633;">runtime</span>.<span style="color: #006633;">HttpJspBase</span>
	<span style="color: #000000; font-weight: bold;">implements</span> org.<span style="color: #006633;">apache</span>.<span style="color: #006633;">jasper</span>.<span style="color: #006633;">runtime</span>.<span style="color: #006633;">JspSourceDependent</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> JspFactory _jspxFactory <span style="color: #339933;">=</span> 
		JspFactory.<span style="color: #006633;">getDefaultFactory</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> java.<span style="color: #006633;">util</span>.<span style="color: #003399;">List</span> _jspx_dependants<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> javax.<span style="color: #006633;">el</span>.<span style="color: #006633;">ExpressionFactory</span> _el_expressionfactory<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> org.<span style="color: #006633;">apache</span>.<span style="color: #006633;">AnnotationProcessor</span> _jsp_annotationprocessor<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> getDependants<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> _jspx_dependants<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> _jspInit<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		_el_expressionfactory <span style="color: #339933;">=</span> 
			_jspxFactory.<span style="color: #006633;">getJspApplicationContext</span><span style="color: #009900;">&#40;</span>
				getServletConfig<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getServletContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.
				<span style="color: #006633;">getExpressionFactory</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		_jsp_annotationprocessor <span style="color: #339933;">=</span> 
			<span style="color: #009900;">&#40;</span>org.<span style="color: #006633;">apache</span>.<span style="color: #006633;">AnnotationProcessor</span><span style="color: #009900;">&#41;</span> getServletConfig<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
				.<span style="color: #006633;">getServletContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getAttribute</span><span style="color: #009900;">&#40;</span>
					org.<span style="color: #006633;">apache</span>.<span style="color: #006633;">AnnotationProcessor</span>.
						<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> _jspDestroy<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> _jspService<span style="color: #009900;">&#40;</span>HttpServletRequest request,
		HttpServletResponse response<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> java.<span style="color: #006633;">io</span>.<span style="color: #003399;">IOException</span>,
		ServletException <span style="color: #009900;">&#123;</span>
&nbsp;
		PageContext pageContext <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		HttpSession session <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		ServletContext application <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		ServletConfig config <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		JspWriter out <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">Object</span> page <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span><span style="color: #339933;">;</span>
		JspWriter _jspx_out <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		PageContext _jspx_page_context <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			response.<span style="color: #006633;">setContentType</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;text/html&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			pageContext <span style="color: #339933;">=</span> 
				_jspxFactory.<span style="color: #006633;">getPageContext</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, request, 
					response, <span style="color: #000066; font-weight: bold;">null</span>, <span style="color: #000066; font-weight: bold;">true</span>, <span style="color: #cc66cc;">8192</span>, <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			_jspx_page_context <span style="color: #339933;">=</span> pageContext<span style="color: #339933;">;</span>
			application <span style="color: #339933;">=</span> pageContext.<span style="color: #006633;">getServletContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			config <span style="color: #339933;">=</span> pageContext.<span style="color: #006633;">getServletConfig</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			session <span style="color: #339933;">=</span> pageContext.<span style="color: #006633;">getSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			out <span style="color: #339933;">=</span> pageContext.<span style="color: #006633;">getOut</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			_jspx_out <span style="color: #339933;">=</span> out<span style="color: #339933;">;</span>
&nbsp;
			out.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;html&gt;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			out.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>&lt;head&gt;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			out.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span>&lt;title&gt;JSP to Servlet&lt;/title&gt;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			out.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>&lt;/head&gt;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			out.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;  <span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			out.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>&lt;body&gt;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			out.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			com.<span style="color: #006633;">webucator</span>.<span style="color: #006633;">bean</span>.<span style="color: #006633;">NameBean</span> bean <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">synchronized</span> <span style="color: #009900;">&#40;</span>_jspx_page_context<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				bean <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>com.<span style="color: #006633;">webucator</span>.<span style="color: #006633;">bean</span>.<span style="color: #006633;">NameBean</span><span style="color: #009900;">&#41;</span> 
					_jspx_page_context
						.<span style="color: #006633;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;bean&quot;</span>, 
							PageContext.<span style="color: #006633;">PAGE_SCOPE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>bean <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					bean <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> com.<span style="color: #006633;">webucator</span>.<span style="color: #006633;">bean</span>.<span style="color: #006633;">NameBean</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					_jspx_page_context.<span style="color: #006633;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;bean&quot;</span>, 
						bean, PageContext.<span style="color: #006633;">PAGE_SCOPE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
			out.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			out.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			org.<span style="color: #006633;">apache</span>.<span style="color: #006633;">jasper</span>.<span style="color: #006633;">runtime</span>.<span style="color: #006633;">JspRuntimeLibrary</span>.
				<span style="color: #006633;">introspecthelper</span><span style="color: #009900;">&#40;</span>
					_jspx_page_context.
						<span style="color: #006633;">findAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;bean&quot;</span><span style="color: #009900;">&#41;</span>, <span style="color: #0000ff;">&quot;firstName&quot;</span>,
							<span style="color: #0000ff;">&quot;Roger&quot;</span>, <span style="color: #000066; font-weight: bold;">null</span>, <span style="color: #000066; font-weight: bold;">null</span>, <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			out.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			out.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			org.<span style="color: #006633;">apache</span>.<span style="color: #006633;">jasper</span>.<span style="color: #006633;">runtime</span>.
				<span style="color: #006633;">JspRuntimeLibrary</span>.<span style="color: #006633;">introspecthelper</span><span style="color: #009900;">&#40;</span>
					_jspx_page_context.<span style="color: #006633;">findAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;bean&quot;</span><span style="color: #009900;">&#41;</span>, 
						<span style="color: #0000ff;">&quot;lastName&quot;</span>, <span style="color: #0000ff;">&quot;Sakowski&quot;</span>, <span style="color: #000066; font-weight: bold;">null</span>, <span style="color: #000066; font-weight: bold;">null</span>, <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			out.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			out.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span>My name is: <span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			out.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			out.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>org.<span style="color: #006633;">apache</span>.<span style="color: #006633;">jasper</span>.<span style="color: #006633;">runtime</span>.
				<span style="color: #006633;">JspRuntimeLibrary</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>com.<span style="color: #006633;">webucator</span>.
					<span style="color: #006633;">bean</span>.<span style="color: #006633;">NameBean</span><span style="color: #009900;">&#41;</span> _jspx_page_context.
						<span style="color: #006633;">findAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;bean&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getFullName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			out.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			out.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>&lt;/body&gt;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			out.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;/html&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Throwable</span> t<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span>t <span style="color: #000000; font-weight: bold;">instanceof</span> SkipPageException<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				out <span style="color: #339933;">=</span> _jspx_out<span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>out <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> out.<span style="color: #006633;">getBufferSize</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
					<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span> out.<span style="color: #006633;">clearBuffer</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span> 
					<span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>java.<span style="color: #006633;">io</span>.<span style="color: #003399;">IOException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
				<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>_jspx_page_context <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
					_jspx_page_context.<span style="color: #006633;">handlePageException</span><span style="color: #009900;">&#40;</span>t<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span>
			_jspxFactory.<span style="color: #006633;">releasePageContext</span><span style="color: #009900;">&#40;</span>_jspx_page_context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The web container compiles this code and executes the resulting servlet.</p>
<p>You probibly noticed that the listed code isn’t exactly the same sort of servlet you generally develop yourself, but it’s pretty easy to recognize some familiar elements. The <strong>init</strong> and <strong>destroy</strong> methods become <strong>_jspInit</strong> and <strong>_jspDestroy</strong>. The <strong>service</strong> method is <strong>_jspService</strong>.</p>
<p>Check out our <strong><a href="http://www.webucator.com/java/course/introduction-to-javaserver-pages.cfm">Introduction the JavaServer Pages</a></strong>,  <strong><a href="http://www.webucator.com/java/course/jsp-training.cfm">Comprehensive JSP</a></strong>, and<a href="http://www.webucator.com/java/course/advanced-jsp-training.cfm"> <strong>Advanced JSP</strong></a><strong> </strong>outlines.</p>
<img src="http://feeds.feedburner.com/~r/JavaTrainingBlog/~4/r4dNccwVtbs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://java.blogs.webucator.com/2010/08/10/javaserverpages-as-servlets/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://java.blogs.webucator.com/2010/08/10/javaserverpages-as-servlets/</feedburner:origLink></item>
		<item>
		<title>Java SE 6: Top Ten Features</title>
		<link>http://feedproxy.google.com/~r/JavaTrainingBlog/~3/5oYdUmelOHc/</link>
		<comments>http://java.blogs.webucator.com/2010/07/23/java-se-6-top-ten-features/#comments</comments>
		<pubDate>Fri, 23 Jul 2010 12:27:01 +0000</pubDate>
		<dc:creator>rsakowski</dc:creator>
				<category><![CDATA[EJB]]></category>
		<category><![CDATA[Java EE]]></category>
		<category><![CDATA[Java Web Services]]></category>
		<category><![CDATA[JDBC]]></category>

		<guid isPermaLink="false">http://java.blogs.webucator.com/?p=195</guid>
		<description><![CDATA[<p>I remember when Sun released JDK 1.08. It was dubbed the “classic” version. After a few years of feedback from developers, Sun made some meaningful changes and additions and released  the JDK 1.1. The release was given the nickname of Java SE 2. Obviously the 0.02 increment in the version number a little misleading. A few years later, Sun released version 1.5. Once again the industry judged the changes more significant than the version indicated so they dubbed it Java 5. It happened again. The release of version1.6 is called Java SE 6. The following is a list of some of the notable additions and changes:</p>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjava.blogs.webucator.com%2F2010%2F07%2F23%2Fjava-se-6-top-ten-features%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fjava.blogs.webucator.com%2F2010%2F07%2F23%2Fjava-se-6-top-ten-features%2F&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>I remember when Sun released JDK 1.08. It was dubbed the “classic” version. After a few years of feedback from developers, Sun made some meaningful changes and additions and released  the JDK 1.1. The release was given the nickname of Java SE 2. Obviously the 0.02 increment in the version number is a little misleading. A few years later, Sun released version 1.5. Once again the industry judged the changes more significant than the version indicated so they dubbed it Java 5. It happened again. The release of version1.6 is called Java SE 6. The following is a list of some of the notable additions and changes:<span id="more-195"></span></p>
<ol>
<li><strong>Web Services:<br />
</strong>Developers will find better support for writing Web Service clients. They can expose APIs that are interoperable with .NET using a simple annotation. It also adds new parsing and XML capabilities to Java object-mapping, previously only available in Java EE or the Java Web Services Pack.</li>
<li><strong>Scripting:</strong>:<br />
Developers can mix in JavaScript. Useful for prototyping and teams with varying skill sets. Advanced developers can plug in their own scripting engines and use their favorite scripting language with Java code as they see fit.</li>
<li><strong>Database:<br />
</strong>The Java SE 6 development kit – but not in the Java Runtime Environment (JRE) – bundles the Java JDBC database (based on Apache Derby). Developers will also get  JDBC 4.0 offering many  improvements, such as support for XML as a SQL datatype and better integration of Binary Large OBjects (BLOBs) and Character Large OBjects (CLOBs) into the API.</li>
<li><strong>Desktop Deployment:<br />
</strong>Applications deployed to the desktop will benefit from a number of changes that enhance  OS access: better look-and-feel in Swing technology, LCD text rendering, and better performance overall. Java applications integrate better with the native platform with access to the platform&#8217;s System Tray and Start menu. Java SE 6 unifies the Java Plug-in technology and Java WebStart engines. Additionally, Java WebStart intallation got a  needed makeover.</li>
<li><strong>More Desktop APIs:<br />
</strong>GUI developers get a few  new items: SwingWorker streamlines threading, JTable offers sorting and filtering, and a facility for creating splash screens.</li>
<li><strong>Monitoring and Management:<br />
</strong>Developers don&#8217;t have to do anything special to  attach the monitoring and management tools. Java SE 6 adds more diagnostic information, and bundles the memory-heap analysis tool <strong>Jhat</strong> for core dump analysis.</li>
<li><strong>Compiler Access:<br />
</strong>The compiler API opens up programmatic access to <strong>javac</strong> for in-process compilation of dynamically generated Java code. It is not intended for the everyday developer, but it’s sure to improved Java frameworks development and implementation in general.</li>
<li><strong>Pluggable Annotations:<br />
</strong>For every feature missing in Java, an annotation can solve the problem. Java tool and framework vendors can define custom annotations that support plug-ins and processes.</li>
<li><strong>Security:<br />
</strong>This release adds a few more security features:</p>
<ul>
<li>XML-Digital Signature (XML-DSIG) APIs for creating and manipulating digital signatures</li>
<li>Simplified security administration via new access to platform-native security services, such as native Public Key Infrastructure (PKI) and cryptographic services on Microsoft Windows</li>
<li>Java Generic Security Services (Java GSS) and Kerberos services for authentication</li>
<li>Access to LDAP servers for authenticating users</li>
</ul>
</li>
<li><strong>Quality, Compatibility, Stability:<br />
</strong>Sun has done regular releases of  Java SE for years and built a massive set of test cases in the process. Additionally, people have been downloading binary snapshots for over 20 months prior to the beta release filing their bug reports, areas of irritation, compliance issues, etc. This extensive testing has fixed a number of quality and regression issues well in advance of general release. Most notable is better performance than J2SE 5.0.</li>
</ol>
<p><strong><a href="http://www.webucator.com/">Webucator</a></strong>’s<strong> <a href="http://www.webucator.com/java/index.cfm">Java courses</a></strong>, labs, and workshops are Java SE capable. Check them out.</p>
<img src="http://feeds.feedburner.com/~r/JavaTrainingBlog/~4/5oYdUmelOHc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://java.blogs.webucator.com/2010/07/23/java-se-6-top-ten-features/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://java.blogs.webucator.com/2010/07/23/java-se-6-top-ten-features/</feedburner:origLink></item>
		<item>
		<title>Java Database Connectivity (JDBC) Basics using MySQL</title>
		<link>http://feedproxy.google.com/~r/JavaTrainingBlog/~3/N2H0bZbCmsU/</link>
		<comments>http://java.blogs.webucator.com/2010/07/05/java-database-connectivity-jdbc-basics-using-mysql/#comments</comments>
		<pubDate>Mon, 05 Jul 2010 20:15:40 +0000</pubDate>
		<dc:creator>rsakowski</dc:creator>
				<category><![CDATA[Java EE]]></category>
		<category><![CDATA[JDBC]]></category>

		<guid isPermaLink="false">http://java.blogs.webucator.com/?p=172</guid>
		<description><![CDATA[<p>I was asked to go into Java's database API (the <strong>JDBC</strong>) using the open source database, <strong>MySQL</strong>. The JDBC is included in the <strong>Java SE</strong> and the<strong> Java EE</strong> distributions in the <strong>java.sql package</strong>. It&#8217;s designed to hide the specifics of a RDBMS from business logic. In this article I&#8217;ll cover the basics of the JDBC and what it requires to use MySQL.</p>
]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjava.blogs.webucator.com%2F2010%2F07%2F05%2Fjava-database-connectivity-jdbc-basics-using-mysql%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fjava.blogs.webucator.com%2F2010%2F07%2F05%2Fjava-database-connectivity-jdbc-basics-using-mysql%2F&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>I was asked to go into Java&#8217;s database API (the <strong>JDBC</strong>) using the open source database, <strong>MySQL</strong>. The JDBC is included in the <strong>Java SE</strong> and the<strong> Java EE</strong> distributions in the <strong>java.sql package</strong>. It&rsquo;s designed to hide the specifics of a RDBMS from business logic. In this article I&rsquo;ll cover the basics of the JDBC and what it requires to use MySQL.</p>
<p><!--  more --></p>
<p>Before we get started we need to install the software:</p>
<ol>
<li>Of course you will need a current revision of the <strong>JDK </strong>or<strong> Java EE</strong> installed on your computer.</li>
<li>Download MySQL and install it. The MySQL download for Windows comes in two types: as an Installer, and as a simple ZIP file. I use the ZIP version, <strong>mysql-noinstall-5.1.48-win32.zip</strong>. Other OS platforms will require matching versions of MySQL and the installation procedures may vary.</li>
<li>Download the MySQL <strong>jConnector</strong>. I used <strong>mysql-connector-java-5.1.13.zip</strong>. Unzip the archive and locate the<strong> mysql-connector-java-5.1.13-bin.jar</strong> file. You&rsquo;ll need to point your <strong>CLASSPATH </strong>to this JAR. It contains the MySQL driver, <strong>com.mysql.jdbc.Driver</strong>.</li>
</ol>
<p>The <strong>mysql </strong>text console utility in the <strong>bin </strong>directory of MySQL can be used to execute SQL statements. See the documentation for this utility for it&rsquo;s commands and login requirements. In this example, I used the following SQL statements to establish a database and user:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">create database test<span style="color: #339933;">;</span>
use test<span style="color: #339933;">;</span>
&nbsp;
create table person <span style="color: #009900;">&#40;</span>
id integer,
name varchar<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">30</span><span style="color: #009900;">&#41;</span>,
job_id integer,
location varchar<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">30</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
grant all on test.<span style="color: #339933;">*</span> to roger@localhost identified by <span style="color: #0000ff;">'toast'</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>I populated the test database with the following statements:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">insert into person <span style="color: #009900;">&#40;</span>id, name, job_id, location<span style="color: #009900;">&#41;</span> 
	values <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #0000ff;">'Bill Smith'</span>, <span style="color: #cc66cc;">1183</span>, <span style="color: #0000ff;">'OH'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
insert into person <span style="color: #009900;">&#40;</span>id, name, job_id, location<span style="color: #009900;">&#41;</span> 
	values <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span>, <span style="color: #0000ff;">'Sue Jones'</span>, <span style="color: #cc66cc;">529</span>, <span style="color: #0000ff;">'NJ'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
insert into person <span style="color: #009900;">&#40;</span>id, name, job_id, location<span style="color: #009900;">&#41;</span> 
	values <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span>, <span style="color: #0000ff;">'Tom Swift'</span>, <span style="color: #cc66cc;">84443</span>, <span style="color: #0000ff;">'FL'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
insert into person <span style="color: #009900;">&#40;</span>id, name, job_id, location<span style="color: #009900;">&#41;</span> 
	values <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">4</span>, <span style="color: #0000ff;">'Bob the Builder'</span>, <span style="color: #cc66cc;">34693</span>, <span style="color: #0000ff;">'MA'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
insert into person <span style="color: #009900;">&#40;</span>id, name, job_id, location<span style="color: #009900;">&#41;</span> 
	values <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">5</span>, <span style="color: #0000ff;">'Homer Simpson'</span>, <span style="color: #cc66cc;">39</span>, <span style="color: #0000ff;">'NE'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
insert into person <span style="color: #009900;">&#40;</span>id, name, job_id, location<span style="color: #009900;">&#41;</span> 
	values <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">7</span>, <span style="color: #0000ff;">'Margo Lane'</span>, <span style="color: #cc66cc;">2222</span>, <span style="color: #0000ff;">'TX'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
insert into person <span style="color: #009900;">&#40;</span>id, name, job_id, location<span style="color: #009900;">&#41;</span> 
	values <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">8</span>, <span style="color: #0000ff;">'Bud Abbot'</span>, <span style="color: #cc66cc;">5202</span>, <span style="color: #0000ff;">'RI'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>It’s not much to look at, but it does give us something to work against.</p>
<p>The JDBC defines five classes that our example will use:</p>
<ul>
<li><strong>java.sql.DriverManager</strong>: serves as a factory for creating Connection objects.</li>
<li><strong>java.sql.Connection</strong>: fosters the database connection and serves as a factory for creating Statement objects.</li>
<li><strong>java.sql.Statement</strong>: represents the SQL statement to be executed by the database.</li>
<li><strong>java.sql.ResultSet</strong>: represents the rows and fields obtained through a SQL select statement.</li>
<li><strong>java.sql.SQLException</strong>: all JDBC operations require this exception to be caught.</li>
</ul>
<p>The following code serves as my <strong>Data Access Object </strong>(<strong>DAO</strong>). The block comments explain how it works:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.webucator.jdbc</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.Connection</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.DriverManager</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.ResultSet</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.SQLException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.Statement</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DAO <span style="color: #009900;">&#123;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">/* These variable values are used to setup
      the Connection object */</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> <span style="color: #003399;">URL</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;jdbc:mysql://localhost:3306/test&quot;</span><span style="color: #339933;">;</span>
   <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> USER <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;roger&quot;</span><span style="color: #339933;">;</span>
   <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> PASSWORD <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;toast&quot;</span><span style="color: #339933;">;</span>
   <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> DRIVER <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;com.mysql.jdbc.Driver&quot;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">/* This method is used to create a connection using 
      the values listed above. Notice the throws clause 
      in the method signature. This allows the calling method 
      to deal with the exception rather than catching it in 
      both places. The ClassNotFoundException must be caught 
      because the forName method requires it. */</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Connection</span> getConnection<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">SQLException</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #003399;">Connection</span> con <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #000000; font-weight: bold;">Class</span>.<span style="color: #006633;">forName</span><span style="color: #009900;">&#40;</span>DRIVER<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
         con <span style="color: #339933;">=</span> <span style="color: #003399;">DriverManager</span>.<span style="color: #006633;">getConnection</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">URL</span>, USER, PASSWORD<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">ClassNotFoundException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>e.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #003399;">System</span>.<span style="color: #006633;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000000; font-weight: bold;">return</span> con<span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">/* This method does most of the work. Note the try 
      and catch. Virtually all JDBC methods throw a 
      SQLException that must be tended to. The Connection 
      object is used to create a Statement object. 
      The executeQuery method is used to submit a 
      SELECT SQL query. The executeUpdate method can be 
      used to delete, change, or insert records. 
      The executeQuery method returns a ResultSet object. 
      It contains methods to navigate through the records 
      in the ResultSet object (the next method for 
      example moves the cursor to the next row; it 
      returns false when it runs out of rows) as well 
      as methods to access fields in those rows. Notice 
      that the id and job_id fields are long data types 
      while name and location are Strings. The ResultSet 
      object provides methods to deal with most common data 
      types, but it&amp;rsquo;s a good idea to review how Java data 
      types align with database data types. The report is 
      formatted using the format method introduced in Java 5.  */</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> getEmployees<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #003399;">ResultSet</span> rs <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #003399;">Statement</span> s <span style="color: #339933;">=</span> getConnection<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">createStatement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         rs <span style="color: #339933;">=</span> s.<span style="color: #006633;">executeQuery</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM PERSON&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;%3s %-15s %-7s %-7s%n&quot;</span>, 
            <span style="color: #0000ff;">&quot;ID&quot;</span>, <span style="color: #0000ff;">&quot;NANE&quot;</span>, <span style="color: #0000ff;">&quot;JOB ID&quot;</span>, 
			<span style="color: #0000ff;">&quot;LOCATION&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;%3s %15s %7s %7s%n&quot;</span>, 
            <span style="color: #0000ff;">&quot;---&quot;</span>, <span style="color: #0000ff;">&quot;---------------&quot;</span>, 
			<span style="color: #0000ff;">&quot;-------&quot;</span>, <span style="color: #0000ff;">&quot;--------&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
         <span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>rs.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">long</span> id <span style="color: #339933;">=</span> rs.<span style="color: #006633;">getLong</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">String</span> name <span style="color: #339933;">=</span> rs.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">long</span> job <span style="color: #339933;">=</span> rs.<span style="color: #006633;">getLong</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;job_id&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">String</span> location <span style="color: #339933;">=</span> rs.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;location&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;%-3d %-15s %7d %5s%n&quot;</span>, 
               id, name, job, location<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">SQLException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>e.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #003399;">System</span>.<span style="color: #006633;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The following class just exercises the DOA object:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.webucator.jdbc</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> JDBCClient <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		DAO dao <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DAO<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		dao.<span style="color: #006633;">getEmployees</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The output of this program is shown below:</p>
<pre>ID NANE            JOB ID  LOCATION
--- --------------- ------- --------
1   Bill Smith         1183    OH
2   Sue Jones           529    NJ
3   Tom Swift         84443    FL
4   Bob the Builder   34693    MA
5   Homer Simpson        39    NE
7   Margo Lane         2222    TX
8   Bud Abbot          5202    RI</pre>
<p>Check out our <a href="http://www.webucator.com/java/course/building-web-applications-with-servlets-jdbc-jsp.cfm">JDBC course</a> for more information. </p>
<img src="http://feeds.feedburner.com/~r/JavaTrainingBlog/~4/N2H0bZbCmsU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://java.blogs.webucator.com/2010/07/05/java-database-connectivity-jdbc-basics-using-mysql/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://java.blogs.webucator.com/2010/07/05/java-database-connectivity-jdbc-basics-using-mysql/</feedburner:origLink></item>
		<item>
		<title>Java Application Servers: Which One  Fits Your Project Best?</title>
		<link>http://feedproxy.google.com/~r/JavaTrainingBlog/~3/cZfjlTUnpdU/</link>
		<comments>http://java.blogs.webucator.com/2010/06/27/java-application-servers-which-one-fits-your-project-best/#comments</comments>
		<pubDate>Sun, 27 Jun 2010 15:50:23 +0000</pubDate>
		<dc:creator>rsakowski</dc:creator>
				<category><![CDATA[EJB]]></category>
		<category><![CDATA[Java EE]]></category>
		<category><![CDATA[Java Web Services]]></category>
		<category><![CDATA[JBoss]]></category>
		<category><![CDATA[JDBC]]></category>
		<category><![CDATA[JSP]]></category>
		<category><![CDATA[Tomcat]]></category>
		<category><![CDATA[WebLogic]]></category>
		<category><![CDATA[WebSphere]]></category>

		<guid isPermaLink="false">http://java.blogs.webucator.com/?p=167</guid>
		<description><![CDATA[<p>A common question students ask me is  what’s the difference between a Java Application server and a Java  EE Application server? Actually the question usually goes a bit  further to include a HTTP (HyperText Transport Protocol) Server as  well. Seeing as how the application server is a central player in  running your projects, the question isn’t trivial.</p>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjava.blogs.webucator.com%2F2010%2F06%2F27%2Fjava-application-servers-which-one-fits-your-project-best%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fjava.blogs.webucator.com%2F2010%2F06%2F27%2Fjava-application-servers-which-one-fits-your-project-best%2F&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>A common question students ask me is  what’s the difference between a Java Application server and a Java  EE Application server? Actually the question usually goes a bit  further to include a HTTP (HyperText Transport Protocol) Server as  well. Seeing as how the application server is a central player in  running your projects, the question isn’t trivial.</p>
<p><!-- more --></p>
<p><strong>A HTTP Server:</strong><br />Client requests to a HTTP Server create  a server response, usually in the form of an <a href="http://www.webucator.com/webdesign/html.cfm">HTML</a> (<a href="http://www.webucator.com/webdesign/html.cfm">HyperText Markup  Language</a>) document or dynamic content through CGI (Common Gateway  Interface) programs. The most popular HTTP Server is <a href="http://www.webucator.com/servers/apache.cfm">Apache</a>, an  open  source Web server.  The first version of Apache was developed in 1995. A distant second  place in popularity is Microsoft’s IIS.</p>
<p><strong>Java Application Server</strong>:<br />
These servers are focused on Java  technologies for dynamic content rather than CGI. They provide a web  container capable of executing <a href="http://www.webucator.com/java/course/building-web-applications-with-servlets-jdbc-jsp.cfm">JSP</a> <a href="http://www.webucator.com/java/course/building-web-applications-with-servlets-jdbc-jsp.cfm">(JavaServer Pages</a>) and <a href="http://www.webucator.com/java/course/building-web-applications-with-servlets-jdbc-jsp.cfm">Servlets</a>, a  subset of the Java EE specification. They do have some HTTP Server  capabilities as well, but they are usually not strong enough to meet  the needs of a large, complex web site. It is common to front a Java  Application Server with a HTTP Server to capitalize on the best of  both worlds. The most popular Java Application Server is <a href="http://www.webucator.com/servers/course/apache-tomcat-administration-training.cfm">Tomcat</a> from  the Apache Software Foundation. Described as a &quot;reference  implementation&quot; of the Java Servlet and the JSP specifications,  Tomcat is the result of an open collaboration of developers and is  available from the Apache Web site in both binary and source  versions. Tomcat requires a Java Runtime Environment that conforms to  JRE 1.1 or later. </p>
<p><strong>Java EE Application Server:</strong><br />
Enterprise servers provide everything  Java Application Servers do and a lot more.<a href="http://www.webucator.com/java/j2ee.cfm"> Java EE </a>includes several  API specifications, such as <a href="http://www.webucator.com/java/j2ee.cfm">JDBC</a>, <a href="http://www.webucator.com/java/j2ee.cfm">RMI</a>, <a href="http://www.webucator.com/java/j2ee.cfm">e-mai</a>l, <a href="http://www.webucator.com/java/j2ee.cfm">JMS</a>, <a href="http://www.webucator.com/java/javaxmlws.cfm">web services</a>,  <a href="http://www.webucator.com/xml/index.cfm">XML</a>, etc., and defines how to coordinate them. Java EE also features  some specifications unique to Java EE components. These include  <a href="http://www.webucator.com/java/ejb.cfm">Enterprise JavaBeans</a> (<a href="http://www.webucator.com/java/ejb.cfm">EJB</a>), Connectors, servlets, portlets,  JavaServer Pages and several Web Service technologies. This allows  developers to create enterprise applications that are portable and  scalable, and that integrate with legacy technologies. A Java EE  Application Server can handle transactions, security, scalability,  concurrency and management of the components that are deployed to it.</p>
<p><a href="http://www.webucator.com/servers/jboss.cfm">JBoss</a> is the most popular Java EE  Application Server. It is a Red Hat, open source project supporting  the JBoss Enterprise Middleware Suite (JEMS) brand. It is an  alternative to commercial offerings from IBM WebSphere, Oracle BEA  Services, and SAP NetWeaver. Like Tomcat, JBoss is often fronted by  Apache to support clustering and cash in on its Web Server power.</p>
<img src="http://feeds.feedburner.com/~r/JavaTrainingBlog/~4/cZfjlTUnpdU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://java.blogs.webucator.com/2010/06/27/java-application-servers-which-one-fits-your-project-best/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://java.blogs.webucator.com/2010/06/27/java-application-servers-which-one-fits-your-project-best/</feedburner:origLink></item>
		<item>
		<title>JavaServer Faces (JSF): A Feature Rich Framework</title>
		<link>http://feedproxy.google.com/~r/JavaTrainingBlog/~3/P0wAAw2Sgx8/</link>
		<comments>http://java.blogs.webucator.com/2010/06/21/javaserver-faces-jsf-a-feature-rich-framework/#comments</comments>
		<pubDate>Mon, 21 Jun 2010 23:43:46 +0000</pubDate>
		<dc:creator>rsakowski</dc:creator>
				<category><![CDATA[Java EE]]></category>
		<category><![CDATA[JavaServer Faces]]></category>
		<category><![CDATA[JSP]]></category>

		<guid isPermaLink="false">http://java.blogs.webucator.com/?p=158</guid>
		<description><![CDATA[<p><strong>JavaServer Faces</strong> (<strong>JSF</strong>) is a web-based application  framework. It is a request-driven MVC  technology based on a component driven UI design model. As with  Struts, JSF uses XML files for configuration and the integration of  MVC elements. </p>&#60;!-- ]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjava.blogs.webucator.com%2F2010%2F06%2F21%2Fjavaserver-faces-jsf-a-feature-rich-framework%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fjava.blogs.webucator.com%2F2010%2F06%2F21%2Fjavaserver-faces-jsf-a-feature-rich-framework%2F&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><strong>JavaServer Faces</strong> (<strong>JSF</strong>) is a web-based application  framework. It is a request-driven MVC  technology based on a component driven UI design model. As with  Struts, JSF uses XML files for configuration and the integration of  MVC elements.<span id="more-158"></span></p>
<p>Requests are processed by the <strong>FacesServlet</strong>, which loads the  appropriate view template, builds a component tree, processes events,  and renders the response to the client. The state of UI components  (and other objects) is saved at the end of each request, and restored  upon next creation of that view.</p>
<p>Several types of state persistence are available, including  client-side and server-side state-saving.</p>
<p>JSF 1.x uses <strong>JavaServer Pages </strong>(<strong>JSP</strong>) for its display  technology, but it can accommodate other technologies. <strong>JSF2</strong> uses <strong>Facelets</strong>, a powerful view description language.</p>
<h3>JSF Features:</h3>
<ul>
<li><strong>Inversion of Control</strong> (<strong>IoC</strong>), a methodology for leveraging <strong>Plain Old Java Objects </strong>(<strong>POJO</strong>).</li>
<li>Template-based component system provides a means of creating composite component creation.</li>
<li><strong>Ajax</strong> support minimizes the requirement for JavaScript.</li>
<li><strong>Unified Expression Language</strong> (<strong>EL</strong>) which allows views to access Managed Bean properties and methods.</li>
<li>A set of HTML and web-application specific UI components.</li>
<li>A server-side event model for dispatching events and attaching listeners to system operations.</li>
<li>State management for &#8220;request&#8221;, &#8220;session&#8221;, &#8220;application&#8221;, &#8220;flash&#8221;, and &#8220;view&#8221; scoped beans.</li>
<li>XML-based<strong> tag libraries</strong> defining core and html functionality.</li>
</ul>
<p><em>For more information on JSF, check out our <a href="http://www.webucator.com/java/jsf.cfm">JavaServer Faces training</a> opportunities.</em></p>
<img src="http://feeds.feedburner.com/~r/JavaTrainingBlog/~4/P0wAAw2Sgx8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://java.blogs.webucator.com/2010/06/21/javaserver-faces-jsf-a-feature-rich-framework/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://java.blogs.webucator.com/2010/06/21/javaserver-faces-jsf-a-feature-rich-framework/</feedburner:origLink></item>
		<item>
		<title>Apache Struts: Putting the Pieces Together</title>
		<link>http://feedproxy.google.com/~r/JavaTrainingBlog/~3/aYgB4rYsdT0/</link>
		<comments>http://java.blogs.webucator.com/2010/06/07/apache-struts-putting-the-pieces-together/#comments</comments>
		<pubDate>Mon, 07 Jun 2010 18:08:25 +0000</pubDate>
		<dc:creator>rsakowski</dc:creator>
				<category><![CDATA[Apache Struts]]></category>
		<category><![CDATA[Java EE]]></category>

		<guid isPermaLink="false">http://java.blogs.webucator.com/?p=134</guid>
		<description><![CDATA[<p>The following Struts <b>Hello World</b> application introduces the primary players in a <b>Struts</b> application. It might seem like a lot of work to do a very simple job (display <i>Hello World!</i> in your browser), but don’t let that put you off. The real value of any framework is realized after it’s been setup. Building on it is extremely easy, efficient and maintainable from then on.</p>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjava.blogs.webucator.com%2F2010%2F06%2F07%2Fapache-struts-putting-the-pieces-together%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fjava.blogs.webucator.com%2F2010%2F06%2F07%2Fapache-struts-putting-the-pieces-together%2F&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>The following Struts <strong>Hello World</strong> application introduces the primary players in a <strong>Struts</strong> application. It might seem like a lot of work to do a very simple job (display <i>Hello World!</i> in your browser), but don’t let that put you off. The real value of any framework is realized after it’s been setup. Building on it is extremely easy, efficient and maintainable from then on.</p>
<p>The first order of business is to create the web.xml so we can use Struts&#8217; <strong>ActionServlet</strong>.</p>
<p><span id="more-134"></span></p>
<p><strong>web.xml:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;?</span>xml version<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1.0&quot;</span> encoding<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #339933;">?&gt;</span>
<span style="color: #339933;">&lt;</span>web<span style="color: #339933;">-</span>app xmlns<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://java.sun.com/xml/ns/javaee&quot;</span> xmlns<span style="color: #339933;">:</span>xsi<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span>
	version<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;2.5&quot;</span>
	xsi<span style="color: #339933;">:</span>schemaLocation<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://java.sun.com/xml/ns/javaee   
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&quot;</span><span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>servlet<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>servlet<span style="color: #339933;">-</span>name<span style="color: #339933;">&gt;</span>action<span style="color: #339933;">&lt;/</span>servlet<span style="color: #339933;">-</span>name<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>servlet<span style="color: #339933;">-</span>class<span style="color: #339933;">&gt;</span>org.<span style="color: #006633;">apache</span>.<span style="color: #006633;">struts</span>.<span style="color: #006633;">action</span>.<span style="color: #006633;">ActionServlet</span><span style="color: #339933;">&lt;/</span>servlet<span style="color: #339933;">-</span>class<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>init<span style="color: #339933;">-</span>param<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>param<span style="color: #339933;">-</span>name<span style="color: #339933;">&gt;</span>config<span style="color: #339933;">&lt;/</span>param<span style="color: #339933;">-</span>name<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>param<span style="color: #339933;">-</span>value<span style="color: #339933;">&gt;/</span>WEB<span style="color: #339933;">-</span>INF<span style="color: #339933;">/</span>struts<span style="color: #339933;">-</span>config.<span style="color: #006633;">xml</span><span style="color: #339933;">&lt;/</span>param<span style="color: #339933;">-</span>value<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;/</span>init<span style="color: #339933;">-</span>param<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>init<span style="color: #339933;">-</span>param<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>param<span style="color: #339933;">-</span>name<span style="color: #339933;">&gt;</span>debug<span style="color: #339933;">&lt;/</span>param<span style="color: #339933;">-</span>name<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>param<span style="color: #339933;">-</span>value<span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">&lt;/</span>param<span style="color: #339933;">-</span>value<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;/</span>init<span style="color: #339933;">-</span>param<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>init<span style="color: #339933;">-</span>param<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>param<span style="color: #339933;">-</span>name<span style="color: #339933;">&gt;</span>detail<span style="color: #339933;">&lt;/</span>param<span style="color: #339933;">-</span>name<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>param<span style="color: #339933;">-</span>value<span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">&lt;/</span>param<span style="color: #339933;">-</span>value<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;/</span>init<span style="color: #339933;">-</span>param<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>load<span style="color: #339933;">-</span>on<span style="color: #339933;">-</span>startup<span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">&lt;/</span>load<span style="color: #339933;">-</span>on<span style="color: #339933;">-</span>startup<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;/</span>servlet<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>servlet<span style="color: #339933;">-</span>mapping<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>servlet<span style="color: #339933;">-</span>name<span style="color: #339933;">&gt;</span>action<span style="color: #339933;">&lt;/</span>servlet<span style="color: #339933;">-</span>name<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>url<span style="color: #339933;">-</span>pattern<span style="color: #339933;">&gt;*</span>.<span style="color: #006633;">do</span><span style="color: #339933;">&lt;/</span>url<span style="color: #339933;">-</span>pattern<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;/</span>servlet<span style="color: #339933;">-</span>mapping<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>welcome<span style="color: #339933;">-</span>file<span style="color: #339933;">-</span>list<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>welcome<span style="color: #339933;">-</span>file<span style="color: #339933;">&gt;</span>index.<span style="color: #006633;">jsp</span><span style="color: #339933;">&lt;/</span>welcome<span style="color: #339933;">-</span>file<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;/</span>welcome<span style="color: #339933;">-</span>file<span style="color: #339933;">-</span>list<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>web<span style="color: #339933;">-</span>app<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p><strong>index.jsp:</strong></p>
<p>The gateway for our Struts &#8220;hello world&#8221; application is the <strong>index.jsp</strong> file. It forwards the request to the hello world action as follows using the  <strong>*.do</strong> URL pattern used in <strong>web.xml</strong>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>jsp<span style="color: #339933;">:</span>forward page<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;HelloWorld.do&quot;</span><span style="color: #339933;">/&gt;</span></pre></td></tr></table></div>

<p><strong>struts-config.xml:</strong></p>
<p>The <strong>struts-config.xml</strong> file is used to configure the Struts framework.<br />
This file contains the details regarding the form bean and the action mapping.</p</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;?</span>xml version<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1.0&quot;</span> encoding<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #339933;">?&gt;</span>
<span style="color: #339933;">&lt;!</span>DOCTYPE struts<span style="color: #339933;">-</span>config <span style="color: #000000; font-weight: bold;">PUBLIC</span> 
<span style="color: #0000ff;">&quot;-//Apache Software Foundation//DTD Struts Configuration 1.2//EN&quot;</span> 
<span style="color: #0000ff;">&quot;http://struts.apache.org/dtds/struts-config_1_2.dtd&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>struts<span style="color: #339933;">-</span>config<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>form<span style="color: #339933;">-</span>beans<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>form<span style="color: #339933;">-</span>bean name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;HelloWorldActionForm&quot;</span>
			type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;com.webucator.struts.HelloWorldActionForm&quot;</span> <span style="color: #339933;">/&gt;</span>
	<span style="color: #339933;">&lt;/</span>form<span style="color: #339933;">-</span>beans<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>action<span style="color: #339933;">-</span>mappings<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>action input<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;/index.jsp&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;HelloWorldActionForm&quot;</span>
			path<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;/HelloWorld&quot;</span>
			scope<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;session&quot;</span> type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;com.webucator.struts.HelloWorldAction&quot;</span><span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>forward name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;success&quot;</span> path<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;/helloWorld.jsp&quot;</span> <span style="color: #339933;">/&gt;</span>
		<span style="color: #339933;">&lt;/</span>action<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;/</span>action<span style="color: #339933;">-</span>mappings<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>struts<span style="color: #339933;">-</span>config<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p><strong>HelloWorldActionForm.java:</strong></p>
<p><strong>HelloWorldActionForm</strong> class has one String variable, <strong>message</strong>, and the corresponding <strong>getter</strong> and <strong>setter</strong> methods.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.webucator.struts</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HelloWorldActionForm <span style="color: #000000; font-weight: bold;">extends</span> org.<span style="color: #006633;">apache</span>.<span style="color: #006633;">struts</span>.<span style="color: #006633;">action</span>.<span style="color: #006633;">ActionForm</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> message<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> HelloWorldActionForm<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getMessage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> message<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setMessage<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> message<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">message</span> <span style="color: #339933;">=</span> message<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>HelloWorldAction.java:</strong></p>
<p>The <strong>HelloWorldAction</strong> class contains the <strong>execute</strong> method which contains the business logic of the application. To access the <strong>HelloWorldActionForm</strong> variables in the <strong>HelloWorldAction</strong> we need to type caste the form object from the <strong>Object</strong> class. Then we can access its variable using the <strong>getter</strong> and <strong>setter</strong> methods. The <strong>execute</strong> method returns <strong>ActionForward</strong>. Based on the String value set in this class, the corresponding view will be called. This configuration is done in <strong>struts-config.xml</strong> file where the <strong>forward</strong> tag indicates that <strong>&#8220;success&#8221;</strong> is mapped to the view <strong>helloWorld.jsp</strong>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.webucator.struts</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletRequest</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletResponse</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.ActionForm</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.ActionForward</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.ActionMapping</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HelloWorldAction <span style="color: #000000; font-weight: bold;">extends</span> org.<span style="color: #006633;">apache</span>.<span style="color: #006633;">struts</span>.<span style="color: #006633;">action</span>.<span style="color: #003399;">Action</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">String</span> SUCCESS <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;success&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> ActionForward execute<span style="color: #009900;">&#40;</span>ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response<span style="color: #009900;">&#41;</span>
			<span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span>
		HelloWorldActionForm helloWorldForm <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>HelloWorldActionForm<span style="color: #009900;">&#41;</span> form<span style="color: #339933;">;</span>
		helloWorldForm.<span style="color: #006633;">setMessage</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hello World!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> mapping.<span style="color: #006633;">findForward</span><span style="color: #009900;">&#40;</span>SUCCESS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>helloWorld.jsp:</strong></p>
<p>In <strong>helloWorld.jsp</strong> we get the value of the form variable, <strong>message</strong>, and display it. We use Struts <strong>bean</strong> tag to do this. The <strong>name</strong> attribute of the <strong>bean</strong> tag holds the value of the action form and the property attribute holds the value of the variable to be displayed.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;%</span>@ page language<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;java&quot;</span> <span style="color: #000000; font-weight: bold;">import</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;java.util.*&quot;</span> pageEncoding<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;ISO-8859-1&quot;</span><span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;%</span>@ taglib prefix<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;bean&quot;</span> uri<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://struts.apache.org/tags-bean&quot;</span><span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;!</span>DOCTYPE <span style="color: #003399;">HTML</span> <span style="color: #000000; font-weight: bold;">PUBLIC</span> <span style="color: #0000ff;">&quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>html<span style="color: #339933;">&gt;</span>
   <span style="color: #339933;">&lt;</span>head<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>title<span style="color: #339933;">&gt;</span>Hello World<span style="color: #339933;">&lt;/</span>title<span style="color: #339933;">&gt;</span>
   <span style="color: #339933;">&lt;/</span>head<span style="color: #339933;">&gt;</span>
   <span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>h1<span style="color: #339933;">&gt;</span>
         <span style="color: #339933;">&lt;</span>bean<span style="color: #339933;">:</span>write name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;HelloWorldActionForm&quot;</span> property<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;message&quot;</span> <span style="color: #339933;">/&gt;</span>
      <span style="color: #339933;">&lt;/</span>h1<span style="color: #339933;">&gt;</span>
   <span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>There you go, your first Struts program. Learn how to bring the full power of Struts to your projects in our <a href="http://www.webucator.com/java/course/apache-struts-training.cfm">Apache Struts class</a>.</p>
<img src="http://feeds.feedburner.com/~r/JavaTrainingBlog/~4/aYgB4rYsdT0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://java.blogs.webucator.com/2010/06/07/apache-struts-putting-the-pieces-together/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://java.blogs.webucator.com/2010/06/07/apache-struts-putting-the-pieces-together/</feedburner:origLink></item>
		<item>
		<title>Apache Struts: A Time-Tested Java EE Framework</title>
		<link>http://feedproxy.google.com/~r/JavaTrainingBlog/~3/U2VosIkcyaI/</link>
		<comments>http://java.blogs.webucator.com/2010/05/22/apache-struts-a-time-tested-java-ee-framework/#comments</comments>
		<pubDate>Sat, 22 May 2010 20:48:53 +0000</pubDate>
		<dc:creator>rsakowski</dc:creator>
				<category><![CDATA[Apache Struts]]></category>
		<category><![CDATA[Java EE]]></category>
		<category><![CDATA[JSP]]></category>

		<guid isPermaLink="false">http://java.blogs.webucator.com/?p=127</guid>
		<description><![CDATA[<p>In a standard Java EE web application, a client typically submits information via an HTML or JSP form. The information is handed a Java servlet or another JSP for processing. This approach is  not  recommended for large projects. How come? You guessed it. It usually mixes model and presentation logic inappropriately making maintenance difficult. It would be nice if we had a framework that inherently supported MVC. <a href="http://struts.apache.org/"><strong>Apache Struts</strong></a> to the rescue!</p>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjava.blogs.webucator.com%2F2010%2F05%2F22%2Fapache-struts-a-time-tested-java-ee-framework%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fjava.blogs.webucator.com%2F2010%2F05%2F22%2Fapache-struts-a-time-tested-java-ee-framework%2F&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>In a standard Java EE web application, a client typically submits information via an HTML or JSP form. The information is handed a Java servlet or another JSP for processing. This approach is  not  recommended for large projects. How come? You guessed it. It usually mixes model and presentation logic inappropriately making maintenance difficult. It would be nice if we had a framework that inherently supported MVC. <a href="http://struts.apache.org/"><strong>Apache Struts</strong></a> to the rescue!</p>
<p><span id="more-127"></span></p>
<p>Struts was originally created by Craig McClanahan and donated to the <a href="http://apache.org/"><strong>Apache Foundation</strong></a> in May, 2000. It became prime a Apache project in 2005. It’s an open-source, web application framework with a focus on the View, but embraces the Model and the Controller as well.</p>
<p>The <strong>ActionServlet</strong> plays the role of Controller in Struts. This servlet takes care of the intricacies of linking things together via declarations in the <strong>struts-config.xml</strong> configuration file.</p>
<p>Requests from the client are sent to the <strong>ActionServlet</strong>  in the form of <em>Actions</em> defined in the configuration file; such a request calls the corresponding <strong>Action</strong> class that interacts with the application’s Model . The Model code returns an <strong>ActionForward</strong> object that tells the servlet what  page to send to the client. Information is passed between model and view in the form of <strong>JavaBeans</strong>. A comprehensive<strong> custom tag library</strong> allows it to read and write the content of these <strong>JavaBeans</strong> to and from the presentation layer without the need for any embedded Java code.</p>
<p>Struts also supports internationalization, provides validation capability for submitted data, and includes a templating technology called <a href="http://tiles.apache.org"><strong>Tiles</strong></a> that allows the presentation layer to be composed from independent HTML or JSP fragments.</p>
<p><strong>Apache Struts</strong> offers a lot to developers faced with complex projects. In the next article I’ll go through a simple <strong>Struts</strong> example to demonstrate how the pieces fit together.</p>
<p><a href="http://www.webucator.com/java/struts.cfm">Apache Struts training</a> can put you on the fast track. Check it out.</p>
<img src="http://feeds.feedburner.com/~r/JavaTrainingBlog/~4/U2VosIkcyaI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://java.blogs.webucator.com/2010/05/22/apache-struts-a-time-tested-java-ee-framework/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://java.blogs.webucator.com/2010/05/22/apache-struts-a-time-tested-java-ee-framework/</feedburner:origLink></item>
		<item>
		<title>Java Frameworks: Why You Should Care</title>
		<link>http://feedproxy.google.com/~r/JavaTrainingBlog/~3/nIjsb-Jofuo/</link>
		<comments>http://java.blogs.webucator.com/2010/05/14/java-frameworks-why-you-should-care/#comments</comments>
		<pubDate>Fri, 14 May 2010 16:41:49 +0000</pubDate>
		<dc:creator>rsakowski</dc:creator>
				<category><![CDATA[Apache Struts]]></category>
		<category><![CDATA[EJB]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Java EE]]></category>
		<category><![CDATA[Java Fundamentals]]></category>
		<category><![CDATA[JavaServer Faces]]></category>
		<category><![CDATA[JSP]]></category>
		<category><![CDATA[Spring]]></category>

		<guid isPermaLink="false">http://java.blogs.webucator.com/?p=119</guid>
		<description><![CDATA[<p>In previous articles, I talked a lot  about the <strong>Model/View/Controlle</strong>r design pattern. Using it is  recommended for all programming projects in the same way it's  recommended to pull a parachute's ripcord when sky diving. The more independent (<strong>loosely coupled</strong>) each MVC tier is  in relation to the complexities of the others, the more likely our  applications will survive maintenance changes and enhancements. Equally I demonstrated how loose coupling can be employed by using JSP and JavaBeans. </p> ]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjava.blogs.webucator.com%2F2010%2F05%2F14%2Fjava-frameworks-why-you-should-care%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fjava.blogs.webucator.com%2F2010%2F05%2F14%2Fjava-frameworks-why-you-should-care%2F&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>In previous articles, I talked a lot about the <strong>Model/View/Controlle</strong>r design pattern. Using it is recommended for all programming projects in the same way it&#8217;s recommended to pull a parachute&#8217;s ripcord when sky diving. The more independent (<strong>loosely coupled</strong>) each MVC tier is in relation to the complexities of the others, the more likely our applications will survive maintenance changes and enhancements. Equally I demonstrated how loose coupling can be employed by using JSP and JavaBeans.<span id="more-119"></span></p>
<p>So we&#8217;re set to develop that enterprise project our boss just put on our desk, right? Not really. An enterprise application will get very complicated, and it will get there very fast. Program features will interact with each other creating a ridge overall design. Endless hours will be spent solving problems over and over again. New features will be added, and old features will be removed or changed. And on it goes.</p>
<p>Here is another &#8220;ripcord&#8221; recommendation: get acquainted with Java design patterns.</p>
<p>Four eggheads (Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides), nicknamed the <strong>Gang of Four </strong>or<strong> GoF</strong>, with a lot of time on their hands created a set of programmatic design patterns. Their book, <a href="http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612">Design Patterns: Elements of Reusable Object-Oriented Software</a> (ISBN 0-201-63361-2), describes recurring solutions to common problems in software design. The book is divided into two parts, with the first two chapters exploring the capabilities and pitfalls of object-oriented programming, and the remaining chapters describing 23 classic patterns. Besides providing time-tested solutions, most of them have something else in common: they employ fine-grained, loose coupling. Although the book is over 15 years old, the recommendations are still applicable today, which is why it remains in the top 20 on <a href="http://www.amazon.com/gp/bestsellers/books/3839/ref=pd_ts_b_nav">Amazon&#8217;s top best-selling programing books list</a>.</p>
<p>So, armed with an understanding of MVC and GoF design patterns, all our bases are covered; we have everything we need to develop that robust, flexible, and scalable, enterprise application, right? Sort of. With a lot of care and planning (and I do mean A LOT) we can create an application that lives up to these goals, but the plumbing that links all the business logic together can add a huge amount of infrastructure complexity to a project; and worse: it tends to be tightly coupled to the business components! It would be better if we could just plug our business logic into some existing structure. If you had something like that, you could focus on what a program is supposed to do and not how it&#8217;s glued together. And better yet, if it does it using design patterns, our loosely couple business classes will be loosely bound to the infrastructure.</p>
<p>Eureka!</p>
<p><strong>Java Frameworks</strong> do just that:</p>
<ul>
<li>provide all the plumbing infrastructure</li>
<li>implement design patterns</li>
<li>enforce MVC</li>
</ul>
<p>And they do in a way that is open to most any application development requirements. In my upcoming articles I will take a look at some of the most popular frameworks: <strong><a href="http://www.webucator.com/java/course/hibernate-training.cfm">Hibernate</a></strong>, <strong><a href="http://www.webucator.com/java/course/introduction-ejb-3-0.cfm">Enterprise JavaBeans (EJB 3.0)</a>, <strong><a href="http://www.webucator.com/java/course/javaserver-faces-training.cfm">JavaServer Faces</a></strong><a href="http://www.webucator.com/java/course/javaserver-faces-training.cfm"> (<strong>JSF</strong>)</a>, <strong><a href="http://www.webucator.com/java/struts.cfm">Struts</a></strong>, <strong><a href="http://www.webucator.com/java/spring.cfm">Spring</a></strong>, and others.</p>
<img src="http://feeds.feedburner.com/~r/JavaTrainingBlog/~4/nIjsb-Jofuo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://java.blogs.webucator.com/2010/05/14/java-frameworks-why-you-should-care/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://java.blogs.webucator.com/2010/05/14/java-frameworks-why-you-should-care/</feedburner:origLink></item>
		<item>
		<title>Creating Custom JSP Tags</title>
		<link>http://feedproxy.google.com/~r/JavaTrainingBlog/~3/AsYZND4QGgE/</link>
		<comments>http://java.blogs.webucator.com/2010/05/09/creating-custom-jsp-tags/#comments</comments>
		<pubDate>Sun, 09 May 2010 15:22:25 +0000</pubDate>
		<dc:creator>rsakowski</dc:creator>
				<category><![CDATA[Java EE]]></category>
		<category><![CDATA[JSP]]></category>

		<guid isPermaLink="false">http://java.blogs.webucator.com/?p=101</guid>
		<description><![CDATA[Tag files contain JSP elements that define a custom action. You can do anything in a tag file that you can do in a normal JSP with three exceptions: you can not use the page directive, there are some JSP directives that are only allowed in a tag file, and the file name must use tag as a suffix. Everything else is the same as creating a JSP page.&#60;/]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjava.blogs.webucator.com%2F2010%2F05%2F09%2Fcreating-custom-jsp-tags%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fjava.blogs.webucator.com%2F2010%2F05%2F09%2Fcreating-custom-jsp-tags%2F&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Tag files contain JSP elements that define a custom action. A JSP tag file is similar to a normal JSP file with three exceptions:</p>
<ol>
<li>The page directive is not allowed in a tag file.</li>
<li>There are certain JSP directives that are only allowed in a tag file.</li>
<li>The file name extension for a tag file is &#8220;.tag&#8221;.</li>
</ol>
<p>Everything else is the same as creating a JSP page.<span id="more-101"></span></p>
<p>Using custom JSP tag files provide a number of benefits:</p>
<ol>
<li>Promotes reusability</li>
<li>Enhances loose coupling</li>
<li>Creates single point of maintenance</li>
<li>Allows a clear division of labor between technical and esthetic skill sets</li>
</ol>
<p>The following is a JSP page that uses a custom tag. For all intents and purposes, it eliminates coding from the page with the exceptions of the directives and the me prefix on the <strong>sayHello</strong> element.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;%</span>@ page language<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;java&quot;</span> pageEncoding<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;ISO-8859-1&quot;</span><span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;%</span>@ taglib prefix<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;me&quot;</span> tagdir<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;/WEB-INF/tags&quot;</span> <span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;!</span>DOCTYPE <span style="color: #003399;">HTML</span> <span style="color: #000000; font-weight: bold;">PUBLIC</span> <span style="color: #0000ff;">&quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>html<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>head<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>title<span style="color: #339933;">&gt;</span>Hello with Custom Tag<span style="color: #339933;">&lt;/</span>title<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;/</span>head<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>me<span style="color: #339933;">:</span>sayHello firstName<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Roger&quot;</span> lastName<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Sakowski&quot;</span><span style="color: #339933;">/&gt;</span>
	<span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>When this page is viewed in a browser, it will provide the following display:<br/><img src="http://java.blogs.webucator.com/files/2010/05/hello-with-custom-jsp-tag.jpg" alt="Hello with Custom JSP tag" title="hello-with-custom-jsp-tag" width="443" height="222" class="alignnone size-full wp-image-112" /></p>
<p>Here is the custom tag (sayHello.tag) used in the JSP example page above:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;%</span>@ tag body<span style="color: #339933;">-</span>content<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;empty&quot;</span> <span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;%</span>@ attribute name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;firstName&quot;</span> required<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;true&quot;</span> rtexprvalue<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;true&quot;</span> <span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;%</span>@ attribute name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;lastName&quot;</span> required<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;true&quot;</span> rtexprvalue<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;true&quot;</span> <span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;</span>jsp<span style="color: #339933;">:</span>useBean id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;bean&quot;</span> <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;com.webucator.beans.YourName&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>jsp<span style="color: #339933;">:</span>setProperty name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;bean&quot;</span> property<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;firstName&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#{firstName}&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>jsp<span style="color: #339933;">:</span>setProperty name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;bean&quot;</span> property<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;lastName&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#{lastName}&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>jsp<span style="color: #339933;">:</span>getProperty name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;bean&quot;</span> property<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;greeting&quot;</span> <span style="color: #339933;">/&gt;</span></pre></td></tr></table></div>

<p>Note the directives. They declare the required attributes <strong>firstName</strong> and <strong>lastName</strong>. These attributes were used in expressions to set the values on a JavaBean. Here is the JavaBean (YourName.java) used by this tag:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.webucator.beans</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> YourName <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> firstName<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> lastName<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> YourName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getGreeting<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;&lt;h1&gt;Hello, &quot;</span> <span style="color: #339933;">+</span> firstName <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; &quot;</span> <span style="color: #339933;">+</span> lastName <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;!&lt;/h1&gt;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setFirstName<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> firstName<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">firstName</span> <span style="color: #339933;">=</span> firstName<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setLastName<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> lastName<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">lastName</span> <span style="color: #339933;">=</span> lastName<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This brings me back to the benefits or custom JSP tags that I listed at the beginning of this article:</p>
<ol>
<li>Promotes reusability: The tag can be used in any number of JSP pages that need it.</li>
<li>Enhances loose coupling: The JSP page is completely decoupled from the underlining code.</li>
<li>Creates single point of maintenance: Changes made to the tag will also change the JSP documents that use it.</li>
<li>Allows a clear division of labor between technical and aesthetic skill sets: It frees page designers to deal with design while freeing programmers to focus on programming.</li>
<p>In short, it is a win/win proposition for web application development in general.</p>
<p>In my upcoming articles I will cover Java frameworks in general and explore the most popular.</p>
<p><em>Custom tags are covered in our <a href="http://www.webucator.com/java/course/introduction-to-javaserver-pages.cfm">Introduction to JSP</a>, <a href="http://www.webucator.com/java/course/advanced-jsp-training.cfm">Advanced JSP</a>, and <a href="http://www.webucator.com/java/course/jsp-training.cfm">Comprehensive JSP</a> classes.</em></p>
<img src="http://feeds.feedburner.com/~r/JavaTrainingBlog/~4/AsYZND4QGgE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://java.blogs.webucator.com/2010/05/09/creating-custom-jsp-tags/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://java.blogs.webucator.com/2010/05/09/creating-custom-jsp-tags/</feedburner:origLink></item>
		<item>
		<title>JSTL Example: More MVC-friendly JSP</title>
		<link>http://feedproxy.google.com/~r/JavaTrainingBlog/~3/4gR_yDNx-9w/</link>
		<comments>http://java.blogs.webucator.com/2010/05/02/jstl-example-more-mvc-friendly-jsp/#comments</comments>
		<pubDate>Sun, 02 May 2010 20:29:38 +0000</pubDate>
		<dc:creator>rsakowski</dc:creator>
				<category><![CDATA[Apache Struts]]></category>
		<category><![CDATA[Java EE]]></category>
		<category><![CDATA[JavaServer Faces]]></category>
		<category><![CDATA[JSP]]></category>

		<guid isPermaLink="false">http://java.blogs.webucator.com/?p=92</guid>
		<description><![CDATA[In previous articles, I said that the Model/View/Controller (MVC) design pattern is a driving force in application development. I demonstrated how JavaServer Pages (JSP) help mitigate MVC obstacles that dynamic Web page development often presents, but the handful of convenience tags  native to JSP  do not cover all the bases. In short, we might still need to introduce Java code into documents where programmatic logic is required. How do we get rid of that code completely?]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjava.blogs.webucator.com%2F2010%2F05%2F02%2Fjstl-example-more-mvc-friendly-jsp%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fjava.blogs.webucator.com%2F2010%2F05%2F02%2Fjstl-example-more-mvc-friendly-jsp%2F&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>In previous articles, I said that the Model/View/Controller (MVC) design pattern is a driving force in application development. I demonstrated how JavaServer Pages (JSP) help mitigate MVC obstacles that dynamic Web page development often presents, but the handful of convenience tags  native to JSP  do not cover all the bases. In short, we might still need to introduce Java code into documents where programmatic logic is required. How do we get rid of that code completely?<span id="more-92"></span></p>
<p>JSP Taglibs go a long way to answer that question. JSP TagLibs are custom, HTML-style elements that define reusable JSP tags that represent some complex behavior. That behavior might be as simple as JSP markup, but it can also include JavaScript and calls to Java classes and their methods. That means you can extract common functionality from a JSP page, define it in a TagLib and reuse it in other pages as easily as reusing a familiar HTML tag. (In a future article, I&#8217;ll show you how to create custom tags.)</p>
<p>One of the most popular open source TabLibs is the <a href="http://java.sun.com/products/jsp/jstl/">JavaServer Pages Standard Tag Library (JSTL)</a>. It is now included with the Java EE Web application development platform. The JSTL extends the JSP specification by adding tag libraries for common tasks, such as XML and SQL processing, conditional and looping execution, form handling,  internationalization and formatting. The JSTL was developed under the Java Community Process (JCP) as JSR 52 and released in 2002. The current version (JSTL 1.2) was released May 8, 2006.</p>
<p>The JSTL provides an effective way to embed logic within a JSP page without using embedded Java code directly. The use of a standardized tag set, rather than breaking in and out of Java code leads to more maintainable code and enables separation of concerns between the development of the application code and the user interface.</p>
<p>While the JSTL is commonly referred to as a single tag library, it is actually composed of four separate  libraries:</p>
<ol>
<li>Core</li>
<li>XML</li>
<li>SQL</li>
<li>Internationalization/formatting</li>
</ol>
<p>Let&#8217;s consider an example. Assume I want to develop a JSP that displays the contents of a shopping cart in a table. We could use a scriptlet as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>table<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;%</span>
   <span style="color: #003399;">Iterator</span> i <span style="color: #339933;">=</span> cart.<span style="color: #006633;">getItems</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">iterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>i.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      ShoppingCartItem item <span style="color: #339933;">=</span> ShoppingCartItem<span style="color: #009900;">&#41;</span>i.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">%&gt;</span>
   <span style="color: #339933;">&lt;</span>tr<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>td align<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;right&quot;</span> bgcolor<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#ffffff&quot;</span><span style="color: #339933;">&gt;</span>
         $<span style="color: #009900;">&#123;</span>item.<span style="color: #006633;">quantity</span><span style="color: #009900;">&#125;</span>
      <span style="color: #339933;">&lt;/</span>td<span style="color: #339933;">&gt;</span>
   <span style="color: #339933;">&lt;/</span>tr<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;%</span>
   <span style="color: #009900;">&#125;</span>
 <span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;/</span>table<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Now consider the markup the JSTL might use to do the same thing.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>table<span style="color: #339933;">&gt;</span>
   <span style="color: #339933;">&lt;</span>c<span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">forEach</span> var<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;item&quot;</span> items<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;${sessionScope.cart.items}&quot;</span><span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>tr<span style="color: #339933;">&gt;</span>
         <span style="color: #339933;">&lt;</span>td align<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;right&quot;</span> bgcolor<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#ffffff&quot;</span><span style="color: #339933;">&gt;</span>
            $<span style="color: #009900;">&#123;</span>item.<span style="color: #006633;">quantity</span><span style="color: #009900;">&#125;</span>
         <span style="color: #339933;">&lt;/</span>td<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;/</span>tr<span style="color: #339933;">&gt;</span>
   <span style="color: #339933;">&lt;/</span>c<span style="color: #339933;">:</span>forEach<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>table<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>The difference between the two is more than cosmetic. The JSTL approaches the job in a more View-oriented manner then the scriplet. Granted, they both do looping logic, but the JSTL fits more comfortably with HTML and is easier to read and therefore easier to maintain. The benefit of JSTL illustrated here grows as the complexity of the page increases. In my next article I&#8217;ll go into the details of how to implement TagLibs in a JSP.</p>
<p><em>See <a href="http://www.webucator.com/java/course/introduction-to-javaserver-pages.cfm">Introduction to JSP</a>, <a href="http://www.webucator.com/java/course/jsp-training.cfm">Comprehensive JSP</a>, <a href="http://www.webucator.com/java/course/advanced-jsp-training.cfm">Advanced JSP</a>, and <a href="http://www.webucator.com/servers/course/jstl-training-with-weblogic.cfm">JSTL Training with WebLogic</a> for some of our classes that cover JSP and JSTL.</em></p>
<img src="http://feeds.feedburner.com/~r/JavaTrainingBlog/~4/4gR_yDNx-9w" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://java.blogs.webucator.com/2010/05/02/jstl-example-more-mvc-friendly-jsp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://java.blogs.webucator.com/2010/05/02/jstl-example-more-mvc-friendly-jsp/</feedburner:origLink></item>
		<item>
		<title>Reuse, Maintenance, and MVC Benefits of JSP and JavaBeans</title>
		<link>http://feedproxy.google.com/~r/JavaTrainingBlog/~3/WFgq-IOwNWM/</link>
		<comments>http://java.blogs.webucator.com/2010/04/25/reuse-maintenance-and-mvc-benefits-of-jsp-and-javabeans/#comments</comments>
		<pubDate>Mon, 26 Apr 2010 00:53:39 +0000</pubDate>
		<dc:creator>rsakowski</dc:creator>
				<category><![CDATA[Java EE]]></category>
		<category><![CDATA[JSP]]></category>

		<guid isPermaLink="false">http://java.blogs.webucator.com/?p=70</guid>
		<description><![CDATA[<p>There are several common ways to include source code in JavaServer Pages (JSP): put the code directly in each JSP as a declaration or scriptlet, use an include statement to reference another file with the code, or put the code in a JavaBean. So what's the best way to go?</p>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjava.blogs.webucator.com%2F2010%2F04%2F25%2Freuse-maintenance-and-mvc-benefits-of-jsp-and-javabeans%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fjava.blogs.webucator.com%2F2010%2F04%2F25%2Freuse-maintenance-and-mvc-benefits-of-jsp-and-javabeans%2F&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>One oft-touted benefit of JSP is that it&#8217;s much easier to write than straight Java code; however, most JSP programmers end up having to dig deeper into Java at some point.  That said, if JSP applications are designed properly, it is possible to separate the hardcore Java programming from the presentational aspects of you application. I call this the MVC benefit of JSP.<br />
<span id="more-70"></span><br />
There are several common ways to include Java source code in JavaServer Pages (JSP):</p>
<ul>
<li>Put the code directly in each JSP as a declaration or scriptlet.</li>
<li>Use an include statement to reference another file with the code.</li>
<li>Put the code in a JavaBean.</li>
</ul>
<p>So what&#8217;s the best way to include Java code in your JSP?</p>
<p>Including Java directly in a JSP is the most straightforward, but it limits code reuse and requires that even small changes to the document are made by a Java programmer. Using include statements lends itself to some code reuse, but it still requires a Java programmer. Both of these methodologies have another and more important problem: they normally wind up blurring the lines between the Model/View/Controller (MVC) design pattern tiers.</p>
<p>JavaBeans and JSP tags provide everything these methodologies do while separating business logic (Model) from the generation of the display (View). This always leads to better design and more maintainable applications.</p>
<p>The following is a small example of a JSP that uses scriptlets to display a message if the text field contains a name or no message if it&#8217;s blank:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;%</span>@ page language<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;java&quot;</span> pageEncoding<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;ISO-8859-1&quot;</span><span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;!</span>DOCTYPE <span style="color: #003399;">HTML</span> <span style="color: #000000; font-weight: bold;">PUBLIC</span> <span style="color: #0000ff;">&quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>html<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>head<span style="color: #339933;">&gt;</span>
   <span style="color: #339933;">&lt;</span>title<span style="color: #339933;">&gt;</span>Hello with Scriptlets<span style="color: #339933;">&lt;/</span>title<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
   <span style="color: #339933;">&lt;%</span>
      <span style="color: #003399;">String</span> name <span style="color: #339933;">=</span> request.<span style="color: #006633;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #339933;">%&gt;</span>
   <span style="color: #339933;">&lt;</span>form method<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;get&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;form&quot;</span><span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;</span>Enter your name please<span style="color: #339933;">:</span> <span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #339933;">/&gt;&lt;/</span>p<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;submit&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Run Me&quot;</span><span style="color: #339933;">/&gt;&lt;/</span>p<span style="color: #339933;">&gt;</span>
   <span style="color: #339933;">&lt;/</span>form<span style="color: #339933;">&gt;</span>
   <span style="color: #339933;">&lt;%</span>
      <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>name <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">else</span> name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&lt;h1&gt;Hello, &quot;</span> <span style="color: #339933;">+</span> name <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;!&lt;/h1&gt;&quot;</span><span style="color: #339933;">;</span>
      out.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>The JavaServer Page dynamically generates a display and handles user interaction, but reuse, maintenance, and MVC suffer. A JavaBean would solve those problems. First let&#8217;s look at the bean:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.webucator.beans</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> YourName <span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> name<span style="color: #339933;">;</span>
   <span style="color: #000000; font-weight: bold;">public</span> YourName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>name <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">else</span> name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&lt;h1&gt;Hello, &quot;</span> <span style="color: #339933;">+</span> name <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;!&lt;/h1&gt;&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">return</span> name<span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setName<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">name</span> <span style="color: #339933;">=</span> name<span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Now let&#8217;s look at the JSP that uses the JavaBean:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;%</span>@ page language<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;java&quot;</span> pageEncoding<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;ISO-8859-1&quot;</span><span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;!</span>DOCTYPE <span style="color: #003399;">HTML</span> <span style="color: #000000; font-weight: bold;">PUBLIC</span> <span style="color: #0000ff;">&quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>html<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>head<span style="color: #339933;">&gt;</span>
   <span style="color: #339933;">&lt;</span>title<span style="color: #339933;">&gt;</span>Hello with Bean<span style="color: #339933;">&lt;/</span>title<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
   <span style="color: #339933;">&lt;</span>jsp<span style="color: #339933;">:</span>useBean id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;bean&quot;</span> <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;com.webucator.beans.YourName&quot;</span><span style="color: #339933;">/&gt;</span>
   <span style="color: #339933;">&lt;</span>jsp<span style="color: #339933;">:</span>setProperty name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;bean&quot;</span> property<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;*&quot;</span><span style="color: #339933;">/&gt;</span>
   <span style="color: #339933;">&lt;</span>form method<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;get&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;form&quot;</span><span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;</span>Enter your name please<span style="color: #339933;">:</span> <span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #339933;">/&gt;&lt;/</span>p<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;submit&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Run Me&quot;</span><span style="color: #339933;">/&gt;&lt;/</span>p<span style="color: #339933;">&gt;</span>
   <span style="color: #339933;">&lt;/</span>form<span style="color: #339933;">&gt;</span>
   <span style="color: #339933;">&lt;</span>jsp<span style="color: #339933;">:</span>getProperty name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;bean&quot;</span> property<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Changes to the JavaBean will be reflected in all the JSPs that use it, the page can be maintained easily, and MVC is preserved. A final point is that a project can be split between a Java development team and HTML experts. These benefits can be leveraged further by using JSP tag libraries. We&#8217;ll take a look at that topic in the next article.</p>
<p>We cover these concepts in our <a href="http://www.webucator.com/java/course/introduction-to-javaserver-pages.cfm">Introduction to JavaServer Pages</a> course. The next class is scheduled for May 24, 2010.</p>
<img src="http://feeds.feedburner.com/~r/JavaTrainingBlog/~4/WFgq-IOwNWM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://java.blogs.webucator.com/2010/04/25/reuse-maintenance-and-mvc-benefits-of-jsp-and-javabeans/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://java.blogs.webucator.com/2010/04/25/reuse-maintenance-and-mvc-benefits-of-jsp-and-javabeans/</feedburner:origLink></item>
		<item>
		<title>A Simple JavaBeans Tutorial</title>
		<link>http://feedproxy.google.com/~r/JavaTrainingBlog/~3/eMQFu_7o58M/</link>
		<comments>http://java.blogs.webucator.com/2010/04/17/javabeans-tutorial/#comments</comments>
		<pubDate>Sat, 17 Apr 2010 22:52:47 +0000</pubDate>
		<dc:creator>rsakowski</dc:creator>
				<category><![CDATA[Java EE]]></category>
		<category><![CDATA[Java Fundamentals]]></category>
		<category><![CDATA[JavaServer Faces]]></category>
		<category><![CDATA[JSP]]></category>

		<guid isPermaLink="false">http://java.blogs.webucator.com/?p=43</guid>
		<description><![CDATA[A JavaBean is a specialized Java class. It must comply with a few
rules:]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjava.blogs.webucator.com%2F2010%2F04%2F17%2Fjavabeans-tutorial%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fjava.blogs.webucator.com%2F2010%2F04%2F17%2Fjavabeans-tutorial%2F&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>A <strong>JavaBean</strong> is a specialized Java class. In this short JavaBeans tutorial, I&#8217;ll show you the rules with which a JavaBean must comply and then I&#8217;ll give you examples of creating a JavaBean, first in straight Java and then in JSP.<span id="more-43"></span></p>
<h2>JavaBean Rules</h2>
<ul>
<li>A JavaBean must have a public, no-argument constructor (a default constructor). This is required so that Java frameworks can facilitate automated instantiation.</li>
<li>The JavaBean class attributes must be accessed via accessor and mutator methods that follow a standard naming convention (<strong>getXxxx</strong> and <strong>setXxxx</strong>, <strong>isXxxx</strong> for boolean attributes). It&#8217;s important to note that an &#8220;attribute&#8221; is a named memory location that contains a value, while a &#8220;property&#8221; refers to this set of methods used to access an attribute. This allows frameworks to automate operations on attribute values.</li>
<li>The JavaBean class should be serializable. This allows Java applications and frameworks to save, store, and restore the JavaBean&#8217;s state.</li>
</ul>
<h2>JavaBean Example</h2>
<p>Here is a simple JavaBean example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// The class is serialized for IO operations</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Person <span style="color: #000000; font-weight: bold;">implements</span> java.<span style="color: #006633;">io</span>.<span style="color: #003399;">Serializable</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// attributes declared as private</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> name<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> deceased<span style="color: #339933;">;</span>
 
&nbsp;
	<span style="color: #666666; font-style: italic;">// Default Constructor</span>
	<span style="color: #000000; font-weight: bold;">public</span> Person<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// getXxxx to access the name attribute</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">name</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// setXxxx to mutate the name attribute</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setName<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">name</span> <span style="color: #339933;">=</span> name<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// isXxxx to access boolean attribute</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> isDeceased<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">deceased</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// setXxxx to mutate boolean attribute</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setDeceased<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">boolean</span> deceased<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">deceased</span> <span style="color: #339933;">=</span> deceased<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h2>JavaBean Example with JSP</h2>
<p>This JavaBean can be implemented by a JavaServer Page (JSP) without all the code used to instantiate it and initialize its attributes:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;!--</span> Creates an instance of the Person JavaBean <span style="color: #339933;">--&gt;</span>
<span style="color: #339933;">&lt;</span>jsp<span style="color: #339933;">:</span>useBean id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;person&quot;</span> <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Person&quot;</span> scope<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;page&quot;</span><span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;!--</span> Sets the properties of the bean with the values of the <span style="color: #003399;">HTML</span> form <span style="color: #339933;">--&gt;</span>
<span style="color: #339933;">&lt;</span>jsp<span style="color: #339933;">:</span>setProperty name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;person&quot;</span> property<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;*&quot;</span><span style="color: #339933;">/&gt;</span></pre></td></tr></table></div>

<p>In my next article, I&#8217;ll delve into this JSP/JavaBean relationship a bit more.</p>
<p><em>JavaBeans are covered in different forms through Webucator&#8217;s <a href="http://www.webucator.com/java/index.cfm">Java classes</a>.</em></p>
<img src="http://feeds.feedburner.com/~r/JavaTrainingBlog/~4/eMQFu_7o58M" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://java.blogs.webucator.com/2010/04/17/javabeans-tutorial/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://java.blogs.webucator.com/2010/04/17/javabeans-tutorial/</feedburner:origLink></item>
		<item>
		<title>Casting Primitives in Java</title>
		<link>http://feedproxy.google.com/~r/JavaTrainingBlog/~3/lH7NSOHUnO0/</link>
		<comments>http://java.blogs.webucator.com/2010/04/09/casting-primitives-in-java/#comments</comments>
		<pubDate>Fri, 09 Apr 2010 18:15:56 +0000</pubDate>
		<dc:creator>rsakowski</dc:creator>
				<category><![CDATA[Java Fundamentals]]></category>

		<guid isPermaLink="false">http://java.blogs.webucator.com/?p=29</guid>
		<description><![CDATA[The subtleties of  the Java programming language can have some interesting implications. 
Sometimes a subtlety can be create real problems. Here's a question that underscores that point]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjava.blogs.webucator.com%2F2010%2F04%2F09%2Fcasting-primitives-in-java%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fjava.blogs.webucator.com%2F2010%2F04%2F09%2Fcasting-primitives-in-java%2F&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>The subtleties of the Java programming language can have some interesting implications. Sometimes a subtlety can create real problems. Here&#8217;s a question that underscores that point:</p>
<p><span id="more-29"></span><br />
<strong>What is the result of compiling and executing the following Java code?</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">char</span> x1 <span style="color: #339933;">=</span> <span style="color: #0000ff;">'A'</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">char</span> x2 <span style="color: #339933;">=</span> x1 <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
&nbsp;
a<span style="color: #009900;">&#41;</span> x2 <span style="color: #339933;">=</span> <span style="color: #0000ff;">'B'</span>
b<span style="color: #009900;">&#41;</span> x2 <span style="color: #339933;">=</span> <span style="color: #cc66cc;">65</span>
c<span style="color: #009900;">&#41;</span> You cannot <span style="color: #000000; font-weight: bold;">do</span> math with a <span style="color: #000066; font-weight: bold;">char</span> data type
d<span style="color: #009900;">&#41;</span> The code will not compile</pre></div></div>

<p>The answer is<strong> d)</strong>. Do you see the problem?</p>
<p>The expression <strong>x1 = x2 + 1</strong> is the culprit, of course, but why? It&#8217;s the literal <strong>1</strong>. By default, Java will consider that literal as an <strong>int</strong> data type and not a <strong>char</strong>.</p>
<p>Mathematical operations in Java will implicitly cast the result of an operation to the widest scope member. As a result, the operation produces an <strong>int</strong>. A <strong>char</strong> is a two byte value and an <strong>int</strong> is a four byte value.  While primitive Java data types can be implicitly cast to a wider scope, it requires an explicit cast to narrower scopes. The possibility that the expression could return a value too large for a <strong>char</strong> to hold (possibly truncating the result) is something the Java compiler won&#8217;t allow unless it is explicitly given permission.</p>
<p>If the expression were rewritten as follows</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">char</span> x2 <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>x1 <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>the compiler would understand your intentions and compile it without complaint.</p>
<p><em>Check out <a href="http://www.webucator.com/java/course/introduction-java-training-for-new-programmers.cfm">Introduction to Java Training for New Programmers</a>, which we run monthly. It&#8217;s a great opportunity to get started with Java.</em></p>
<img src="http://feeds.feedburner.com/~r/JavaTrainingBlog/~4/lH7NSOHUnO0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://java.blogs.webucator.com/2010/04/09/casting-primitives-in-java/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://java.blogs.webucator.com/2010/04/09/casting-primitives-in-java/</feedburner:origLink></item>
		<item>
		<title>Java Optimization with StringBuilder</title>
		<link>http://feedproxy.google.com/~r/JavaTrainingBlog/~3/naBVPCfNEJ8/</link>
		<comments>http://java.blogs.webucator.com/2010/04/04/java-optimization-with-stringbuilder/#comments</comments>
		<pubDate>Sun, 04 Apr 2010 23:37:55 +0000</pubDate>
		<dc:creator>rsakowski</dc:creator>
				<category><![CDATA[Java Fundamentals]]></category>

		<guid isPermaLink="false">http://java.blogs.webucator.com/?p=15</guid>
		<description><![CDATA[The question keeps coming up: How do I optimize a Java application? It&#8217;s not a trivial question. One easy route is to limit unnecessary object instantiation. The biggest culprits here are String objects. Consider the following Java code snippet: String test = &#34;&#34;; long start = System.currentTimeMillis&#40;&#41;; for&#40;int i = 0; i &#60; 50000; i++&#41; [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjava.blogs.webucator.com%2F2010%2F04%2F04%2Fjava-optimization-with-stringbuilder%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fjava.blogs.webucator.com%2F2010%2F04%2F04%2Fjava-optimization-with-stringbuilder%2F&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>The question keeps coming up: How do I optimize a Java application? It&#8217;s not a trivial question. One easy route is to limit unnecessary object instantiation. The biggest culprits here are <strong>String</strong> objects. Consider the following Java code snippet:<span id="more-15"></span></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">String</span> test <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">long</span> start <span style="color: #339933;">=</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">50000</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	test <span style="color: #339933;">+=</span> <span style="color: #0000ff;">&quot;abc&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Time lapse using +=: &quot;</span> <span style="color: #339933;">+</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> start<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; milliseconds&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This Java code just prints the time it takes to concatenate the string &#8220;abc&#8221; 50,000 times. The result on my system is:</p>
<blockquote style="color: #000000;font-family: Courier New, monospace;font-size: small;"><p>
Time lapse using +=: 11700 milliseconds</p></blockquote>
<p>Now consider the following Java code snippet that uses <strong>StringBuilder</strong> to accomplish the same thing:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">test <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
start <span style="color: #339933;">=</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
StringBuilder builder <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StringBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">50000</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	builder.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;abc&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Time lapse using StringBuilder: &quot;</span> <span style="color: #339933;">+</span>
	<span style="color: #009900;">&#40;</span><span style="color: #003399;">System</span>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> start<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span>
	<span style="color: #0000ff;">&quot; milliseconds&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The output from this code is:</p>
<blockquote style="color: #000000;font-family: Courier New, monospace;font-size: small;"><p>Time lapse using StringBuilder: 0 milliseconds</span></span></p></blockquote>
<p>Now that&#8217;s what I call optimized Java code! Using the <strong>+=</strong> operator to concatenate strings generates a new <strong>String</strong> object for each pass in the for loop plus another object to contain the new value. <strong>StringBuilder</strong>, on the other hand, buffers the process. That&#8217;s just one example. There&#8217;s lots more offered in the <a href="http://www.webucator.com/java/course/java-performance-tuning.cfm">Java Performance Tuning course</a>. Check it out.</p>
<img src="http://feeds.feedburner.com/~r/JavaTrainingBlog/~4/naBVPCfNEJ8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://java.blogs.webucator.com/2010/04/04/java-optimization-with-stringbuilder/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://java.blogs.webucator.com/2010/04/04/java-optimization-with-stringbuilder/</feedburner:origLink></item>
		<item>
		<title>Saying “Hello World!” in Your Language using Java</title>
		<link>http://feedproxy.google.com/~r/JavaTrainingBlog/~3/7HrcYq3sGmY/</link>
		<comments>http://java.blogs.webucator.com/2010/03/31/saying-hello-world-in-your-language-using-java/#comments</comments>
		<pubDate>Wed, 31 Mar 2010 22:01:22 +0000</pubDate>
		<dc:creator>rsakowski</dc:creator>
				<category><![CDATA[Java Fundamentals]]></category>

		<guid isPermaLink="false">http://java.blogs.webucator.com/?p=8</guid>
		<description><![CDATA[The first two W’s in WWW stands for &#8220;World Wide,&#8221; and that infers many languages, dialects and character sets Consider the following &#8220;HelloWorld&#8221; application: public class HelloWorld &#123; public static void main&#40;String&#91;&#93; args&#41; &#123; System.out.println&#40;&#34;Hello World&#34;&#41;; &#125; &#125; The application is not very useful if it&#8217;s used in Germany. We could add logic to distinguish [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjava.blogs.webucator.com%2F2010%2F03%2F31%2Fsaying-hello-world-in-your-language-using-java%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fjava.blogs.webucator.com%2F2010%2F03%2F31%2Fsaying-hello-world-in-your-language-using-java%2F&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>The first two W’s in WWW stands for &#8220;World Wide,&#8221; and that infers many languages, dialects and character sets  Consider the following &#8220;HelloWorld&#8221; application:<span id="more-8"></span></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HelloWorld <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hello World&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The application is not very useful if it&#8217;s used in Germany. We could add logic to distinguish a language and add the string literal to our code, but that would become a maintenance nightmare if the list of languages grows as well as requiring a lot of recompiling with each addition. Java provides the <strong>Locale</strong> class that can be used to both problems:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> InternationalHello <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">String</span> language <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">String</span> country <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>args.<span style="color: #006633;">length</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			language <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#40;</span>args<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			country <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#40;</span>args<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #003399;">Locale</span> currentLocale <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Locale</span><span style="color: #009900;">&#40;</span>language, country<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">ResourceBundle</span> messages <span style="color: #339933;">=</span>
			<span style="color: #003399;">ResourceBundle</span>.<span style="color: #006633;">getBundle</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MessagesBundle&quot;</span>,
				currentLocale<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>messages.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;hello&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>&#8220;MessagesBundle&#8221; references a <strong>MessagesBundle*.properties</strong> file that contains the strings to be displayed. The following sample files and their contents:</p>
<p><strong>MessagesBundle.properties (the default)</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	hello <span style="color: #339933;">=</span> Anyone Out There</pre></div></div>

<p><strong>MessagesBundle_en_US.properties</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	Hello<span style="color: #339933;">=</span>HelloWorld</pre></div></div>

<p><strong>MessagesBundle_de_DE.properties</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	hello <span style="color: #339933;">=</span> Hallo Welt</pre></div></div>

<p><strong>MessagesBundle_fr_FR.properties</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	hello <span style="color: #339933;">=</span> Bonjour Monde</pre></div></div>

<p>Run the program using the following:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Java InternationalHello</pre></div></div>

<p>The output will be:</p>
<p style="padding-left: 30px;">Anyone Out There</p>
<p>When the program receives no arguments it will use <strong>MessagesBundle.properties</strong> as the default bundle.</p>
<p>Try:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Java InternationalHello de DE</pre></div></div>

<p>The output is:</p>
<p style="padding-left: 30px;">Hallo Welt</p>
<p>These arguments are used to identify the dialect and language. They are used to identify <strong>MessagesBundle_de_DE.properties</strong> as the bundle. Try using &#8220;fr FR&#8221; and &#8220;us EN&#8221;.</p>
<p>More and more languages can be added simply by adding a <strong>MessagesBundle*.properties</strong> files and the code doesn&#8217;t need to be recompiled. You gone &#8220;World Wide&#8221;.</p>
<p><em>Internationalization techniques are covered in our <a href="http://www.webucator.com/java/course/jsp-training.cfm">JSP</a>, <a href="http://www.webucator.com/java/course/spring-mvc-web-applications.cfm">Spring MVC</a>, <a href="http://www.webucator.com/servers/course/jstl-training-with-weblogic.cfm">JSTL</a> and <a href="http://www.webucator.com/java/course/apache-struts-training.cfm">Struts</a> classes.</em></p>
<img src="http://feeds.feedburner.com/~r/JavaTrainingBlog/~4/7HrcYq3sGmY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://java.blogs.webucator.com/2010/03/31/saying-hello-world-in-your-language-using-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://java.blogs.webucator.com/2010/03/31/saying-hello-world-in-your-language-using-java/</feedburner:origLink></item>
	</channel>
</rss>

