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

<channel>
	<title>9tutorials - The best collection of tutorials &#187; Java</title>
	<atom:link href="http://9tutorials.com/category/programming/java/feed" rel="self" type="application/rss+xml" />
	<link>http://9tutorials.com</link>
	<description>Photoshop tutorials , Flash tutorials, PHP tutorials and much more</description>
	<lastBuildDate>Sat, 18 May 2013 11:52:29 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5</generator>
		<item>
		<title>GUI 2 &#8211; Tiny Window &#8211; Your first window</title>
		<link>http://9tutorials.com/2009/02/20/gui-tiny-window-your-first-window.html</link>
		<comments>http://9tutorials.com/2009/02/20/gui-tiny-window-your-first-window.html#comments</comments>
		<pubDate>Fri, 20 Feb 2009 08:52:59 +0000</pubDate>
		<dc:creator>rubywind</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://9tutorials.com/2009/02/20/gui-tiny-window-your-first-window.html</guid>
		<description><![CDATA[<p>The following is a very small program that creates a window. It doesn&#8217;t do anything, and it&#8217;s so small you might have a hard time finding it in the upper left corner of your screen. Try resizing it. A default JFrame can do a few things &#8212; it understands minimize and maximize, it can be [...]</p><p>The post <a href="http://9tutorials.com/2009/02/20/gui-tiny-window-your-first-window.html">GUI 2 &#8211; Tiny Window &#8211; Your first window</a> appeared first on <a href="http://9tutorials.com">9tutorials - The best collection of tutorials</a>.</p>]]></description>
				<content:encoded><![CDATA[<p>The following is a very small program that creates a window. It doesn&#8217;t do anything, and it&#8217;s so small you might have a hard time finding it in the upper left corner of your screen. Try resizing it. A default JFrame can do a few things &#8212; it understands minimize and maximize, it can be dragged from the title bar, and it can be resized. If you resize it, you&#8217;ll notice that the content pane is completely empty &#8212; we haven&#8217;t added any components to it yet.awhen the close box is clicked, which is done below.</p>
<p>The post <a href="http://9tutorials.com/2009/02/20/gui-tiny-window-your-first-window.html">GUI 2 &#8211; Tiny Window &#8211; Your first window</a> appeared first on <a href="http://9tutorials.com">9tutorials - The best collection of tutorials</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://9tutorials.com/2009/02/20/gui-tiny-window-your-first-window.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating the Ajax application with Java</title>
		<link>http://9tutorials.com/2007/06/22/creating-the-ajax-application-with-java.html</link>
		<comments>http://9tutorials.com/2007/06/22/creating-the-ajax-application-with-java.html#comments</comments>
		<pubDate>Fri, 22 Jun 2007 07:27:44 +0000</pubDate>
		<dc:creator>dangtruong</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[ajax]]></category>

		<guid isPermaLink="false">http://9tutorials.com/2007/06/22/creating-the-ajax-application-with-java.html</guid>
		<description><![CDATA[<p>We&#8217;ll begin with the complete HTML and JavaScript code for our first application, a simple web page that displays the decimal value of any character. Then we&#8217;ll break apart the JavaScript and examine it. [source:html]&#60;html&#62; &#60;head&#62; &#60;link rel=&#8221;stylesheet&#8221; type=&#8221;text/css&#8221; href=&#8221;style.css&#8221;&#62; &#60;SCRIPT language=&#8221;JavaScript&#8221; src=&#8221;ajax.js&#8221;&#62;&#60;/SCRIPT&#62; &#60;title&#62;Ajax On Java, Chapter 2 Example&#60;/title&#62; &#60;/head&#62; &#60;body onload=&#8221;focusIn( );&#8221;&#62; &#60;h1&#62; AJAX [...]</p><p>The post <a href="http://9tutorials.com/2007/06/22/creating-the-ajax-application-with-java.html">Creating the Ajax application with Java</a> appeared first on <a href="http://9tutorials.com">9tutorials - The best collection of tutorials</a>.</p>]]></description>
				<content:encoded><![CDATA[<div class="spost"><p>We&#8217;ll begin with the complete HTML and JavaScript code for our first application, a simple web page that displays the decimal value of any character. Then we&#8217;ll break apart the JavaScript and examine it.</p>
<p><span id="more-1345"></span></p>
<p>[source:html]&lt;html&gt;<br />
&lt;head&gt;<br />
&lt;link rel=&#8221;stylesheet&#8221; type=&#8221;text/css&#8221; href=&#8221;style.css&#8221;&gt;<br />
&lt;SCRIPT language=&#8221;JavaScript&#8221; src=&#8221;ajax.js&#8221;&gt;&lt;/SCRIPT&gt;<br />
&lt;title&gt;Ajax On Java, Chapter 2 Example&lt;/title&gt;<br />
&lt;/head&gt;<br />
&lt;body onload=&#8221;focusIn( );&#8221;&gt;<br />
&lt;h1&gt; AJAX CHARACTER DECODER &lt;/h1&gt;<br />
&lt;h2&gt; Press a key to find its value. &lt;/h2&gt;<br />
&lt;table&gt;<br />
&lt;tr&gt;<br />
&lt;td&gt;<br />
Enter Key Here -&gt;<br />
&lt;input type=&#8221;text&#8221; id=&#8221;key&#8221; name=&#8221;key&#8221;<br />
onkeyup=&#8221;convertToDecimal( );&#8221;&gt;</p>
<p>&lt;/td&gt;<br />
&lt;/tr&gt;<br />
&lt;/table&gt;<br />
&lt;br /&gt;<br />
&lt;table&gt;<br />
&lt;tr&gt;<br />
&lt;td colspan=&#8221;5&#8243; style=&#8221;border-bottom:solid black 1px;&#8221;&gt;<br />
Key Pressed:<br />
&lt;input type=&#8221;text&#8221; readonly id=&#8221;keypressed&#8221;&gt;<br />
&lt;/td&gt;<br />
&lt;/tr&gt;<br />
&lt;tr&gt;<br />
&lt;td&gt; Decimal &lt;/td&gt;<br />
&lt;/tr&gt;<br />
&lt;tr&gt;<br />
&lt;td&gt;&lt;input type=&#8221;text&#8221; readonly id=&#8221;decimal&#8221;&gt;&lt;/td&gt;<br />
&lt;/tr&gt;<br />
&lt;/table&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;[/source]</p>
<p>For the most part, this is standard HTML. There are only two JavaScript references: focusIn( ) and convertToDecimal( ). The focusIn( ) function merely puts the cursor in the right input field when the page loads, so the user doesn&#8217;t have to move it there with the mouse.</p>
<p>The convertToDecimal( ) function will be our entry into the Ajax world. The lays out the JavaScript code that supports our web page, ajax.js.</p>
<p>[source:javascript]var req;</p>
<p>function convertToDecimal( ) {<br />
var key = document.getElementById(&#8220;key&#8221;);<br />
var keypressed = document.getElementById(&#8220;keypressed&#8221;);<br />
keypressed.value = key.value;<br />
var url = &#8220;/ajaxdecimalcodeconverter/response?key=&#8221; + escape(key.value);<br />
if (window.XMLHttpRequest) {<br />
req = new XMLHttpRequest( );<br />
}<br />
else if (window.ActiveXObject) {<br />
req = new ActiveXObject(&#8220;Microsoft.XMLHTTP&#8221;);<br />
}<br />
req.open(&#8220;Get&#8221;,url,true);<br />
req.onreadystatechange = callback;<br />
req.send(null);<br />
}</p>
<p>function callback( ) {<br />
if (req.readyState==4) {<br />
if (req.status == 200) {<br />
var decimal = document.getElementById(&#8216;decimal&#8217;);<br />
decimal.value = req.responseText;<br />
}<br />
}<br />
clear( );<br />
}<br />
function clear( ) {<br />
var key = document.getElementById(&#8220;key&#8221;);<br />
key.value=&#8221;";<br />
}<br />
function focusIn( ) {<br />
document.getElementById(&#8220;key&#8221;).focus( );<br />
}[/source]</p>
<p>Let&#8217;s take a look at convertToDecimal( ), which is our entry point from index.html. The main JavaScript object we&#8217;ll use is XMLHttpRequest. Unfortunately, one problem with JavaScript is that the code isn&#8217;t the same on all browsers. In Mozilla, Firefox, and Safari, we get an XMLHttpRequest object like this:</p>
<p><em>new XMLHttpRequest( );</em></p>
<p>In Internet Explorer, we use an ActiveX object:</p>
<p><em>new ActiveXObject(&#8220;Microsoft.XMLHTTP&#8221;);</em></p>
<p>Because we can&#8217;t tell in advance which browsers users will view our web page with, we have to write code that will work on any of the likely candidates. First, we must determine whether the user is using Internet Explorer or some other browser, such as Firefox or Mozilla. This task is handled by the following code:</p>
<p>[source:javascript]if (window.XMLHttpRequest) {<br />
req = new XMLHttpRequest( );<br />
}<br />
else if (window.ActiveXObject) {<br />
req = new ActiveXObject(&#8220;Microsoft.XMLHTTP&#8221;);<br />
}[/source]</p>
<p>That&#8217;s basically it: req is now an object that we can use to build our Ajax page.</p>
<p>Now let&#8217;s look at some code that does some real work. We will be using the code from axax.js in the next chapter, so examine it closely and pay special attention to the mechanism that talks to the server. Since we&#8217;re Java developers, the backend will be a servlet, but the web page doesn&#8217;t care.</p>
<p>The convertToDecimal( ) function first gets a String from the form and then sets the url variable to &#8220;/ajaxdecimalcodeconverter/response?key=&#8230;&#8221;. Eventually, we&#8217;ll send this URL to the server (in our case, a servlet) and expect a response (the decimal value of the key), but we&#8217;re not going to send it in response to a Submit button press; we&#8217;re going to send it asynchronously (that is, as soon as we have the keystroke that we want to convert).</p>
<p>After the if/else block, where we figure out which browser is being used and get an appropriate req object, we open a connection to the servlet with the call:</p>
<p><em>req.open(&#8220;Get&#8221;,url,true);</em></p>
<p>Let&#8217;s look at the three parameters in the req.open( ) function:</p>
<p><strong>&#8220;Get&#8221; &#8211; </strong>The first parameter tells JavaScript whether to submit the request to the server using HTTPPost( ) or HTTPGet( ). The HTTPPost( ) method hides the parameters in the request; the HTTPGet( ) method puts the parameters in the URL for everyone to see. For this example, I chose HTTPGet( ) because it is easier to see what parameters are being passed, and the number of parameters is relatively small. If I were sending a complex set of parameters, I&#8217;d use &#8220;Post&#8221; instead.[*]</p>
<p>[*] I&#8217;m getting quite a ways ahead of the story, but it&#8217;s a good idea to use Get only when the request doesn&#8217;t make any changes to the data on the server. That&#8217;s clearly the case here. Conversely, it&#8217;s a bad idea to use Get when you are changing data on the server (for example, if you&#8217;re sending new data, or deleting existing data); in this case, use Post instead.</p>
<p><strong>url -</strong> The second parameter is the URL we&#8217;re passing to the server. We created that URL earlier in the method.</p>
<p><strong>true -</strong> The last parameter determines whether or not the call is asynchronous. When this parameter is TRue, the request is sent asynchronously. When designing Ajax applications, you always want to set the asynchronous flag to TRue; basically, it means &#8220;don&#8217;t stop anything, just notify me when the data comes back.&#8221;</p>
<p>Now, notice the next statement:</p>
<p><em>req.onreadystatechange=callback;</em></p>
<p>This line allows us to use the call asynchronously. We&#8217;re telling the req object to call the callback( ) function whenever a state transition occurs. Therefore, we can process data coming back from the server as soon as it arrives; whenever something happens, we&#8217;ll be notified.</p>
<p><em>What Is a Callback?<br />
A callback is executable code that is passed as a parameter to another function. In our example, we pass code to the XMLHTTPRequest object, which tells us what function to call when it is ready.</em></p>
<p><em>The JavaScript code generates a request that is sent to a servlet. When the servlet returns with the information, the callback function is invoked; in turn, the callback function can display the new information to the user. We specified which function to call with the following JavaScript code:</em></p>
<p><em>req.onreadystatechange = callback;</em></p>
<p><em>This is really powerful. There&#8217;s no more waiting on the page; when the data returns, the user will see it without having to wait for a page reload.</em></p>
<p>The last statement of convertToDecimal( ) sends the request:</p>
<p>req.send(null);</p>
<p>Now, let&#8217;s look at the callback( ) function:</p>
<p>[source:javascript]function callback( ) {<br />
if (req.readyState==4) {<br />
if (req.status == 200) {<br />
if (window.XMLHttpRequest) {<br />
nonMSPopulate( );<br />
}<br />
else if (window.ActiveXObject) {<br />
msPopulate( );<br />
}<br />
}<br />
}<br />
clear( );<br />
}[/source]</p>
<p>This function checks the readyState and the status returned by the server. The readyState can have one of five values, listed in table below</p>
<table cellspacing="0" cellpadding="4" width="100%" frame="hsides" rules="all">
<thead>
<tr>
<th class="thead" align="left" valign="bottom" scope="col">
<p class="docText">Value</p>
</th>
<th class="thead" align="left" valign="bottom" scope="col">
<p class="docText">State</p>
</th>
</tr>
<tr>
<td class="docTableCell" align="left" valign="top">
<p class="docText">0</p>
</td>
<td class="docTableCell" align="left" valign="top">
<p class="docText">Uninitialized</p>
</td>
</tr>
<tr>
<td class="docTableCell" align="left" valign="top">
<p class="docText">1</p>
</td>
<td class="docTableCell" align="left" valign="top">
<p class="docText">Loading</p>
</td>
</tr>
<tr>
<td class="docTableCell" align="left" valign="top">
<p class="docText">2</p>
</td>
<td class="docTableCell" align="left" valign="top">
<p class="docText">Loaded</p>
</td>
</tr>
<tr>
<td class="docTableCell" align="left" valign="top">
<p class="docText">3</p>
</td>
<td class="docTableCell" align="left" valign="top">
<p class="docText">Interactive</p>
</td>
</tr>
<tr>
<td class="docTableCell" align="left" valign="top">
<p class="docText">4</p>
</td>
<td class="docTableCell" align="left" valign="top">
<p class="docText">Complete</p>
</td>
</tr>
</thead>
</table>
<p>The callback( ) function is called on every state change, but that&#8217;s not exactly what we want. We don&#8217;t want to do anything until our request has completed, so we wait until req.readyState == 4.</p>
<p>The next check, req.status == 200, ensures that the HTTPRequest returned a status of OK (200). If the page is not found, status will equal 404. In this example, the code should be activated only when the request has been completed. Note that a readyState of 4 doesn&#8217;t tell us that the request completed correctly; all we know is that it completed. We still have to check the req.status code.</p>
<p><strong>How Is Our JavaScript Function Called?</strong></p>
<p>We&#8217;ve written a nice JavaScript function, convertToDecimal( ), that does some interesting things: it sends a request to the server without intervention by the user, and it arranges for the server&#8217;s response to be added to the web page. But how does convertToDecimal( ) get called? The browser calls it when it detects the keyup event on the &#8220;Enter Key Here -&gt;&#8221; input field. Here is the complete HTML for the input field:</p>
<p><em>&lt;input type=&#8221;text&#8221; id=&#8221;key&#8221; name=&#8221;key&#8221; onkeyup=&#8221;convertToDecimal( );&#8221;&gt;</em></p>
<p>onkeyup=&#8221;convertToDecimal( );&#8221; tells the browser to call the JavaScript function convertToDecimal( ) whenever the user presses and releases a key in the input field.</p>
<p><strong>How Do We Get the Value of the Key Pressed?</strong></p>
<p>Once control is passed to convertToXML( ), we make this call:</p>
<p><em>var key = document.getElementById(&#8220;key&#8221;);</em></p>
<p>At this point, the object with the id of key contains the decimal value of the key that was pressed. All that&#8217;s left for us to do is retrieve the value that the object named key contains. This value is kept in the value parameter of the key element, so key.value contains the value of the key that was pressed.</p>
<p>Once we&#8217;ve retrieved it, we want to place this value in a field for display. That allows us to clear the field used to enter the key. We&#8217;ve named the field for displaying the key keypressed. Here&#8217;s how to retrieve the keypressed field:</p>
<p><em>var keypressed = document.getElementById(&#8220;keypressed&#8221;);</em></p>
<p>The next step is to put the value of key into the value of keypressed:</p>
<p><em>keypressed.value = key.value;</em></p>
<p><strong>Formatting the Page</strong></p>
<p>The final step in developing our application is to create a CSS file to give the page some formatting</p>
<p>[source:css]body {<br />
font-family: Arial, Helvetica, sans-serif;<br />
font-size: small;<br />
text-align:center;<br />
background:#cbdada;<br />
}<br />
#keypressed{<br />
width:30;<br />
border:none;<br />
}<br />
#key {<br />
width:20px;<br />
padding:0;<br />
margin:0;<br />
border:none;<br />
text-align:left<br />
}<br />
h1, h2 {<br />
font-size:120%;<br />
text-align:center;<br />
}<br />
h2 {<br />
font-size:110%<br />
}<br />
table, input {<br />
margin-left:auto;<br />
margin-right:auto;<br />
padding:0px 10px;<br />
text-align:center;<br />
color:black;<br />
text-align:center;<br />
background: #a0f6f5;<br />
border:solid black 1px;<br />
}<br />
td {<br />
margin:10px 10px;<br />
padding: 0px 5px;<br />
border: none;<br />
}<br />
input {<br />
width: 80;<br />
border: none;<br />
border-top:solid #999999 1px;<br />
font-size: 80%;<br />
color: #555555;<br />
}[/source]</p>
<p>The post <a href="http://9tutorials.com/2007/06/22/creating-the-ajax-application-with-java.html">Creating the Ajax application with Java</a> appeared first on <a href="http://9tutorials.com">9tutorials - The best collection of tutorials</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://9tutorials.com/2007/06/22/creating-the-ajax-application-with-java.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

 Served from: 9tutorials.com @ 2013-05-24 06:30:33 by W3 Total Cache -->