<?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>Prog how-to</title>
	<atom:link href="http://proghowto.com/feed" rel="self" type="application/rss+xml" />
	<link>http://proghowto.com</link>
	<description>general programming</description>
	<lastBuildDate>Sat, 09 May 2009 19:24:26 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>PHP Calendar Control</title>
		<link>http://proghowto.com/php-calendar-control</link>
		<comments>http://proghowto.com/php-calendar-control#comments</comments>
		<pubDate>Sat, 09 May 2009 18:47:03 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://proghowto.com/?p=92</guid>
		<description><![CDATA[Short description
Here is a fully customizable calendar control for your PHP applications. It is entirely written in php and can be easily adapted to your needs. Also it is free. The download links are displayed below:
Download
Download the component only here. This package contains only the classes afferent to the control.
I strongly recommend you download all [...]]]></description>
			<content:encoded><![CDATA[<h3>Short description</h3>
<p>Here is a fully customizable calendar control for your PHP applications. It is entirely written in php and can be easily adapted to your needs. Also it is free. The download links are displayed below:</p>
<h3>Download</h3>
<p>Download the component only <a href="http://proghowto.com/products/download/?file=calendar.zip"><strong>here</strong></a>. This package <strong>contains only the classes afferent to the control</strong>.</p>
<p>I strongly recommend you download <strong>all the test classes</strong> (which include the Calendar classes as well), for a &#8220;live&#8221; use example. The full test package can be downloaded <strong><a href="http://proghowto.com/products/download/?file=full_calendar.zip">here</a></strong>. Here is the <a href="http://proghowto.com/products/calendar/"><strong>test link</strong></a> <strong>to see the control at work</strong>. One thing: prototype.js contained in the archive is the prototype framework used for Ajax and it is just a bundled reference, not an actual part of the project.</p>
<h3>How it works</h3>
<p>The calendar shows a full month. The header contains the current month and the year, plus two links to be used to switch to the previous / next month.</p>
<p>A date considered &#8220;selected&#8221; is rendered with a different CSS.</p>
<p>There is no formatting data in the calendar other than the generated CSS classes attached to various html tags. The programmer and the designer are the ones that will decide how to put together the needed CSS in order to make the control blend in the website.</p>
<p>Here is how the calendar looks like (it was taken from the test that can be found <a href="http://proghowto.com/products/calendar/">here</a>).</p>
<p><a href="http://proghowto.com/wp-content/uploads/2009/05/simple_calendar.jpg"><img title="simple_calendar" src="http://proghowto.com/wp-content/uploads/2009/05/simple_calendar.jpg" alt="" /></a></p>
<p>There are links in the header on the left and right arrow shown and on each date. Each hyperlink triggers a Javascript function &#8211; there are default names for these functions. These names can be also changed</p>
<p>The control is not rendered in Javascript. Reasons: it requires a lot of javascript, plus if you need to display information other than just the days (and we need to display this additional information) then you need a roundtrip anyway.</p>
<p>Then what would be a good approach to change the calendar state (rendering):</p>
<ul>
<li>Wrap the calendar in another control let&#8217;s say&lt;div class=&#8217;calendar&#8217; id=&#8217;item1&#8242;&gt;</li>
<li>If the page is entirely rendered, just echo the component html where you want the calendar displayed</li>
<li>If you want to change the calendar in place, then
<ol>
<li>The Javascript functions triggered by the calendar shall send an Ajax request</li>
<li>The callback function receives a json containing among other things the html associated with the calendar new state</li>
<li>Replace the current calendar with the new one like that: document.getElementById(&#8217;item1&#8242;).innerHTML=retrieved_html</li>
</ol>
</li>
<li><strong>Please try the <a href="http://proghowto.com/products/calendar/">test</a> &#8211; it has all the Ajax stuff. It sounds tough, but actually it is extremely simple to implement!!!!</strong></li>
</ul>
<h3>Features</h3>
<p>Here is a list of features:</p>
<ul>
<li>Shows the current month, year</li>
<li>Uses only CSS class names for rendering control. The programmer has to fully provide his own CSS definition that will make the calendar blend in his website</li>
<li>There is a &#8220;selected date&#8221; that will be rendered using a different CSS class</li>
<li>Besides the days in the calendar, you can show some additional data defined in a hash array before the rendering.</li>
<li>The control has some links that trigger Javascript functions. The functions will be implemented by the programmer usually in order to whatever is needed on the server side according to the business requirements and prepare the calendar control for a new state.</li>
</ul>
<h3>Structure</h3>
<p>The calendar is shipped in two PHP clases, a value object class with all the settings called CalendarData and the Calendar class with all the rendering, etc.</p>
<p>In a nutshell, here is how to use the control:</p>
<ul>
<li>Create an instance of CalendarData</li>
<li>Fill out the needed attributes</li>
<li>Create an instance of Calendar (passing the reference to the already created CalendarData object)</li>
<li>Retrieve the control html using the accessor getHtml() from the calendar object</li>
</ul>
<p>Depending where you are in your application, you can do the following:</p>
<ul>
<li>Echo the html in the appropriate place in order to have it rendered in the page</li>
<li>If you are in the middle of an Ajax request processing, put the html in the object that will be converted later to json so that you will have the information in the request callback</li>
</ul>
<div class="table">
<table border="0" cellspacing="0" cellpadding="2">
<tbody>
<tr>
<td>public $year;</td>
<td>The year for current displayed month</td>
</tr>
<tr>
<td>public $month;</td>
<td>The month for the current displayed month. The year and the month are mandatory for the control to be displayed. The rest of the values are not mandatory and default values are provided by the CalendarData constructor</td>
</tr>
<tr>
<td>public $selmonth;</td>
<td>The month for the selected date</td>
</tr>
<tr>
<td>public $selyear;</td>
<td>The year for the selected date</td>
</tr>
<tr>
<td>public $selday;</td>
<td>The day of month associated with the selected date. If the selected date is defined and belongs to the month currently displayed, then the selected date will be displayed using the css class &#8220;selected&#8221;. Appropriately modifying the CSS file you can make this date show different than the others.</td>
</tr>
<tr>
<td>public $values;</td>
<td>The values attached to the current calendar. If they are within the currently displayed month and $showValues is true, then the values will be displayed besides the calendar days. The following things must be taken into account:<br />
The key format is yyyy-mm-dd, without trailing zeros for mm and dd (use 2009-2-2 instead of 2009-02-02)<br />
The value associated with the key must be any string. That string will be retrieved if the values are to be displayed and placed as we mentioned before, besides the associated day.</td>
</tr>
<tr>
<td>public $showValues;</td>
<td>True to show the values associated with the days, false otherwise</td>
</tr>
<tr>
<td>public $showDefault;</td>
<td>True to show the default values for the days that don&#8217;t have values already defined in the $values array. Attention &#8211; in order for the default values to be displayed you need to have $showValues true as well.</td>
</tr>
<tr>
<td>public $left;</td>
<td>In case the values are displayed, then this string is placed between the day and the value. The defaut value is &amp;nbsp;( this will open a braket and will ensure it is on the same line as the day of month</td>
</tr>
<tr>
<td>public $right;</td>
<td>Please see the explanation above for $left. $right has a similar meaning, except will be added immediately after the value.</td>
</tr>
<tr>
<td>public $defaultValue;</td>
<td>If showDefault is true, then the days that don&#8217;t have specific values defined in the $values array will have this value displayed. By default is 0.</td>
</tr>
<tr>
<td>public $functionSelect;</td>
<td>The name of the Javascript function that is triggered when any calendar date is clicked. The default name is select, however you might want to define your own.</td>
</tr>
<tr>
<td>public $functionPrevious;</td>
<td>The name of the Javascript function that is triggered when the &#8220;previous&#8221; link on the control header is clicked.</td>
</tr>
<tr>
<td>public $functionNext;</td>
<td>The name of the Javascript function that is triggered when the &#8220;next&#8221; link on the control header is clicked.</td>
</tr>
</tbody>
</table>
</div>
<h3>Formatting</h3>
<p>The control only generates classes attached to some of the table rows and all the table cells. It is programmer responsibility to tweak the css file in order to obtain the desired result. For example, the formatting obtained for our example that can be seen here, was obtained solely by appropriately adjusting the CSS file.</p>
<p>The classes generated by the control rendering functionality are the following:</p>
<ul>
<li>weekday &#8211; class attached to the td html tags that wrap around week days.</li>
<li>weekend &#8211; class attached to the td html tags that wrap around week end days</li>
<li>selected &#8211; class attached to the td html tag that corresponds to the selected day</li>
</ul>
<p>If you need to have two calendars on the same page and render them differently, please wrap them in two div tags with different class names (div.calendar1 and div.calendar2) and provide the formatting info in your css file accordingly. Also, worth mentioning that the header lines are wrapped in a thead tag while the other ones are wrapped in a tbody. This will also help with organizing the formatting styles in the css file.</p>
<h3>Some Images</h3>
<p>Here are some images for the calendar control, in various configurations:<br />
Without any values displayed<br />
<a href="http://proghowto.com/wp-content/uploads/2009/05/valhidden.jpg"><img title="valhidden" src="http://proghowto.com/wp-content/uploads/2009/05/valhidden.jpg" alt="" /></a></p>
<p>With both the values and the default values displayed<br />
<a href="http://proghowto.com/wp-content/uploads/2009/05/valall.jpg"><img title="valall" src="http://proghowto.com/wp-content/uploads/2009/05/valall.jpg" alt="" width="329" height="174" /></a></p>
<p>With the &#8220;width&#8221; style associated with the cells removed &#8211; so that will occupy minimum space and will look more like the calendars you saw already.</p>
<p><a href="http://proghowto.com/wp-content/uploads/2009/05/calendar.jpg"><img title="calendar" src="http://proghowto.com/wp-content/uploads/2009/05/calendar.jpg" alt="" width="190" height="171" /></a></p>
<h3>Some Code</h3>
<h2>Setting Calendar Data</h2>
<pre>function getData(){
	$dt = new CalendarData();

	// setup some information
	$dt-&gt;month = 3;
	$dt-&gt;year = 2009;
	$dt-&gt;values = array();

	$dt-&gt;selday = 12;
	$dt-&gt;selmonth = 3;
	$dt-&gt;selyear = 2009;

	$dt-&gt;values['2009-3-2'] = "4";
	$dt-&gt;values['2009-3-5'] = "2.5";
	$dt-&gt;values['2009-3-12'] = "1";

	$dt-&gt;showValues = true;
	$dt-&gt;showDefault = false;

	return $dt;
}</pre>
<h2>Rendering the Calendar</h2>
<pre>$dt = getData(); // get the calendar data

$showValues = getToggle($dt-&gt;showValues);
$showDefault = getToggle($dt-&gt;showDefault);

$cal = new Calendar($dt);

$html = $cal-&gt;getHtml();</pre>
<p>And then somewhere in your html page:</p>
<pre>
&lt;?php echo $html; ?&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://proghowto.com/php-calendar-control/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Configure Apache, PHP and MySQL</title>
		<link>http://proghowto.com/configure-apache-php-and-mysql</link>
		<comments>http://proghowto.com/configure-apache-php-and-mysql#comments</comments>
		<pubDate>Tue, 28 Apr 2009 16:39:51 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://proghowto.com/?p=70</guid>
		<description><![CDATA[Another tutorial about how to configure Apache, PHP and MySQL]]></description>
			<content:encoded><![CDATA[<p>This is just another tutorial of how to configure a development machine for Apache, PHP and MySQL</p>
<p>There are packages bundling the three technologies (install the package and everything is there ready to go). They are good for start. However a serious developer should install the latest production bundles from scratch. Couple or reasons: </p>
<ul>
<li>Have complete control of the process</li>
<li>Use the versions that you have or intend to have on the production machine</li>
<li>Learn how to configure everything. As a programmer, you eventually need to know that.</li>
</ul>
<p>We are going to present the simplest approach to have all the three packages installed on your machine and have you up and running in the shortest time possible.</p>
<p>We assume you got a machine running Windows XP or 2000 or Vista, and you don&#8217;t have any of the required packages installed (if you tried to install them and it did not work, please start fresh).</p>
<p>Please install the packages in the order mentioned below. The reason: for example if you install PHP after you install Apache, then the PHP installation script will automatically integrate with Apache, saving you some time.</p>
<p>Please do not install in the default Windows <strong>Program Files</strong> folder. Instead, do it in a folder with a short name, directly under the root folder. We installed everything under a folder called <strong>c:\devweb</strong></p>
<h3>Install Apache</h3>
<p>We recommend the use of a Windows msi installation kit. Please follow the installation steps:</p>
<ul>
<li>Proceed to <a href="http://www.apache.org/">http://www.apache.org/</a></li>
<li>Follow the link &#8220;<strong>http server</strong>&#8220;</li>
<li>Then select &#8220;<strong>download from a mirror</strong>&#8220;</li>
<li>Download the installation kit for Apache 2.2 without Crypto. You don&#8217;t need SSL for a development machine at least not for this example</li>
<li>At the time of this writing, the file name we downloaded is: <strong>apache_2.2.11-win32-x86-no_ssl.msi</strong></li>
</ul>
<p>Now run the installation kit. Choose the installation folder under the c:\devweb as mentioned above. We chose: <strong>c:\devweb\apache2.2.11\</strong></p>
<h3>Install PHP</h3>
<p>There are Windows installation kits .msi available for PHP as well. That means that the PHP installation is as simple as the one we just did for Apache http server. </p>
<p>Please follow the installation steps as follows:</p>
<ul>
<li>Proceed to <a href="http://www.php.net/">http://www.php.net/</a></li>
<li>Select the link under <strong>Stable Releases</strong> that points to the latest PHP version ready for production</li>
<li>Download and launch the installation kit</li>
<li>Select the folder where PHP will be installed; we chose <strong>C:\devweb\php\</strong></li>
<li>When asked for which web server you require installation, please <strong>carefully </strong> select the web server that that will be used &#8211; as per the previous paragraph, we chose <strong>Apache 2.2</strong></li>
<li>When asked, select the Apache subfolder where httpd.conf resides &#8211; in our case, <strong>c:\devweb\apache2.2.11\conf</strong>. The PHP installation kit will appropriately modify the Apache configuration file to point to the newly installed version</li>
<li>Select the extensions that are to be installed during the current process. Please consider that in this case less is more, and only select what you need. We selected only mysql and mysqli. If you need other extensions in the future, just run the PHP installation script again and add the required extensions (the current installation settings will not be affected)</li>
</ul>
<p>The installation will then be completed. You are now ready to test it. Before doing so, please have a look at the file <strong>httpd.conf</strong> in the Apache configuration folder to see what are the changes operated during PHP installation. You will see the following lines added at the end:</p>
<pre>
#BEGIN PHP INSTALLER EDITS - REMOVE ONLY ON UNINSTALL
PHPIniDir "C:/devweb/php/"
LoadModule php5_module "C:/devweb/php/php5apache2_2.dll"
#END PHP INSTALLER EDITS - REMOVE ONLY ON UNINSTALL
</pre>
</p>
<ul>
<li>Restart the Apache server using the Apache monitor (see the icon in the tray) or directly restart the corresponding Windows service using the management console</li>
</ul>
<p><a href="http://proghowto.com/wp-content/uploads/2009/04/apache_restart.jpg"><img src="http://proghowto.com/wp-content/uploads/2009/04/apache_restart.jpg" alt="" title="apache_restart" width="190" height="100" class="alignnone size-full wp-image-86" /></a></p>
<ul>
<li><span style="color:red"><strong>Rename</strong></span> the file <strong>index.html</strong> at the location c:\devweb\apache2.2.11\htdocs to <strong>index.php</strong> and open it for editing</li>
<li>Delete all the existent html and insert the following:</li>
</ul>
<pre>
<font color="#2040a0"><strong><font color="4444FF"><strong>&lt;</strong></font><font color="#2040a0">html</font><font color="4444FF"><strong>&gt;</strong></font></strong></font>
<font color="#2040a0"><strong><font color="4444FF"><strong>&lt;</strong></font><font color="#2040a0">head</font><font color="4444FF"><strong>&gt;</strong></font></strong></font>
<font color="#2040a0"><strong><font color="4444FF"><strong>&lt;</strong></font><font color="#2040a0">title</font><font color="4444FF"><strong>&gt;</strong></font></strong></font>Test functionality php<font color="#2040a0"><strong><font color="4444FF"><strong>&lt;</strong></font><font color="#2040a0">/title</font><font color="4444FF"><strong>&gt;</strong></font></strong></font>
<font color="#2040a0"><strong><font color="4444FF"><strong>&lt;</strong></font><font color="#2040a0">/head</font><font color="4444FF"><strong>&gt;</strong></font></strong></font>
<font color="#2040a0"><strong><font color="4444FF"><strong>&lt;</strong></font><font color="#2040a0">body</font><font color="4444FF"><strong>&gt;</strong></font></strong></font>
<font color="#2040a0"><strong><font color="4444FF"><strong>&lt;</strong></font><font color="#2040a0">?php
</font>  <font color="#2040a0">phpinfo();
?</font><font color="4444FF"><strong>&gt;</strong></font></strong></font>
<font color="#2040a0"><strong><font color="4444FF"><strong>&lt;</strong></font><font color="#2040a0">/body</font><font color="4444FF"><strong>&gt;</strong></font></strong></font>
<font color="#2040a0"><strong><font color="4444FF"><strong>&lt;</strong></font><font color="#2040a0">/html</font><font color="4444FF"><strong>&gt;</strong></font></strong></font>
</pre>
<ul>
<li>Access the index.php at the url <a href="http://localhost/index.php">http://localhost/index.php</a>. You should show a lengthy table that contains all the PHP configuration information. </li>
<li>Please search for the extension names you mentioned at the PHP installation (we mentioned mysql and mysqli) and double check they are there. </li>
</ul>
<p><a href="http://proghowto.com/wp-content/uploads/2009/04/php_info.jpg"><img src="http://proghowto.com/wp-content/uploads/2009/04/php_info.jpg" alt="" title="php_info" width="307" height="115" class="aligncenter size-full wp-image-83" /></a></p>
<p>At this moment you are done, and (almost) ready to start the development activity on your computer. </p>
<h3>Install MySQL</h3>
<p>We want a similar easy installation process for MySQL as well. Follow the steps:</p>
<ul>
<li>Proceed to the address http://www.mysql.org</li>
<li>Click on &#8220;downloads&#8221;</li>
<li>Under the title MySQL Community Server, click on the &#8220;Download&#8221; button</li>
<li>Proceed with all the needed steps until you are in position to download the intallation kit. Under the Windows binaries, there are many options, please choose the msi insallation kit. At the time of this writing, the file we downloaded is mysql-5.1.34-win32.msi</li>
</ul>
<p>Now you can proceed with the installation. There are many steps to follow, please run the installation kit and consider the following:</p>
<ul>
<li>Don&#8217;t use &#8220;default configuration&#8221; or &#8220;default installation&#8221; as you will have to choose some of the settings different than the default ones</li>
<li>Install the MySQL under the same top folder as PHP and Apache. We installed ours in C:\devweb\mysql5.1</li>
<li>Choose it to run as a service. It is extremely convenient.</li>
<li>Choose a root password different than empty one (we chose the word &#8220;password&#8221;).</li>
<li>Just to be on the safe side, after the MySQL installation is done, please restart the Apache web server</li>
</ul>
<p>Now we are in position to test the end to end installation. We already confirmed that Apache works nicely with our PHP installation, now we have to test that MySQL can be accessed from our PHP scripts</p>
<p>Edit again the file index.php under the folder c:\devweb\apache2.2.11\htdocs (the Apache default document root) and paste the script below. The script connects to the database using the root account you just created and lists the existent databases. Most likely this is the only time you will ever use this sql from a PHP page, however right now it will do the job by showing us we can access the database server from our PHP script.</p>
<pre>
&lt;?php
	// try to test the database
	// access from the current
	// machine - the program should
	// appropriately list the databases
	// that are currently available on
	// the current machine db server

	$sql = &quot;show databases&quot;;
	$connection =
		mysqli_connect(&quot;localhost&quot;, &quot;root&quot;, &quot;password&quot;);

	$statement = $connection-&gt;prepare($sql);
	$statement-&gt;bind_result($val);
	$statement-&gt;execute();

	$items = array();
	while($statement-&gt;fetch()){
		$items[] = $val;
	}

	$html = &quot;&quot;;
	$index = 0;
	foreach($items as $current){
		$index ++;
		$html .= &quot;value at index:
			{$index} is:
			&lt;strong&gt;{$current}&lt;/strong&gt; &lt;br/&gt;&quot;;
	}
?&gt;
&lt;html&gt;
&lt;head&gt;
	&lt;title&gt;Test database access&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;?php echo $html; ?&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>And here is the result, when accessing the page at url http://localhost/index.php :</p>
<p><a href="http://proghowto.com/wp-content/uploads/2009/04/test_db.jpg"><img src="http://proghowto.com/wp-content/uploads/2009/04/test_db.jpg" alt="" title="Test Database" class="aligncenter size-medium wp-image-82" /></a></p>
<p>Now we are 100% done and ready to start the development. You have to update Apache configuration to have an alias for each project you work at, so that you will be able to work a couple of projects at a time without having to restart Apache every time, however this is just a detail, your main development environment is ready.</p>
]]></content:encoded>
			<wfw:commentRss>http://proghowto.com/configure-apache-php-and-mysql/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Faces context not found. getResponseWriter will fail</title>
		<link>http://proghowto.com/faces-context-not-found-getresponsewriter-will-fail</link>
		<comments>http://proghowto.com/faces-context-not-found-getresponsewriter-will-fail#comments</comments>
		<pubDate>Thu, 15 Jan 2009 16:03:25 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://proghowto.com/?p=52</guid>
		<description><![CDATA[Did you get this error?
javax.faces.FacesException: Faces context not found. getResponseWriter will fail. Check if the FacesServlet has been initialized at all in your web.xml configuration fileand if you are accessing your jsf-pages through the correct mapping. E.g.: if your FacesServlet is mapped to  *.jsf (with the &#60;servlet-mapping&#62;-element), you need to access your pages as [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Did you get this error?</strong></p>
<p>javax.faces.FacesException: Faces context not found. getResponseWriter will fail. Check if the FacesServlet has been initialized at all in your web.xml configuration fileand if you are accessing your jsf-pages through the correct mapping. E.g.: if your FacesServlet is mapped to  *.jsf (with the &lt;servlet-mapping&gt;-element), you need to access your pages as &#8217;sample.jsf&#8217;. If you tried to access &#8217;sample.jsp&#8217;, you&#8217;d get this error-message.<br />
javax.faces.webapp.UIComponentTag.setupResponseWriter(UIComponentTag.java:926)<br />
javax.faces.webapp.UIComponentTag.doStartTag(UIComponentTag.java:313)<br />
org.apache.myfaces.taglib.core.ViewTag.doStartTag(ViewTag.java:73)<br />
org.apache.jsp.list_jsp._jspx_meth_f_005fview_005f0(list_jsp.java:161)</p>
<p><strong>There are a couple of reasons the error appears.</strong></p>
<p><strong>First situation</strong></p>
<p>The first one is explained in the error itself &#8211; at least with this implementation(Apache MyFaces).  So, in some Java web containers, notably Tomcat, first time when you access a JSF resource (a jsp page that contains the f:view and other JSF components) you need to access it using the url patter declared in the web.xml for the Java Server Faces servlet. It is usually *.jsf or *.faces &#8211; so instead of putting whatever.jsp, you use whatever.faces. Then, the faces context is created and the application will run fine. Usually the jsf applications starts by setting a welcome page that only forwards to a genuine JSF url &#8211; ex: index.jsp only has a <strong>&lt;jsp:forward page=&#8221;second.jsf&#8221; /&gt;</strong></p>
<p><strong>The other situation</strong></p>
<p>Lets focus on the second situation: you have a JSF application that works just fine, and then after being left idle for some time, the session expires and then shows the above error message.</p>
<p>The reason: the JSF framework expects to find the faces context in the session. It does not find it, then will show this exception.</p>
<p>Now, any programmer provides a functionality in his / her application to deal with the expired session. Usually a tag is placed in each JSP at the very beginning, the tag would check the session object to see whether the user is still logged, and if not the application usually forwards to the login page showing in the same time an appropriate error message &#8220;The session expired please login again&#8221;.</p>
<p>The problem here is that the error from JSF takes precedence, meaning that the garbled error message will be shown on the screen rather than having the application behave as described in the previous paragraph.</p>
<p><strong>How you can fix it:</strong><br />
The error triggered by JSF is a &#8220;HTTP error 500&#8243; one, so you need to handle this at the application level, by inserting the follwing xml tags in the web.xml file:</p>
<p><code>&lt;error-page&gt;<br />
&lt;error-code&gt;500&lt;/error-code&gt;<br />
&lt;location&gt;/error.jsp&lt;/location&gt;<br />
&lt;/error-page&gt;<br />
</code></p>
<p>Please make sure you defined error.jsp file in your application.</p>
<p><strong>Very important:</strong> please make sure that error.jsp is a plain jsp and not a JSF one otherwise you did not fix anything.</p>
<p>From this point, you have options, either you show an error message and provide a link to the login page, or you directly forward to the login page and show the error message there.</p>
]]></content:encoded>
			<wfw:commentRss>http://proghowto.com/faces-context-not-found-getresponsewriter-will-fail/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Move a Private Key Between Two JKS Keystore Files</title>
		<link>http://proghowto.com/move-a-private-key-between-two-jks-keystore</link>
		<comments>http://proghowto.com/move-a-private-key-between-two-jks-keystore#comments</comments>
		<pubDate>Sat, 11 Oct 2008 19:33:25 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://proghowto.com/?p=3</guid>
		<description><![CDATA[Using keytool to move a private key from a Java jks keystore to another one is possible. Here is a programmatic solution for this operation.]]></description>
			<content:encoded><![CDATA[<p>The current article shows you how to move a private key between two JKS keystore files.</p>
<p><strong>General</strong><br />
JKS keystores are binary files holding one or more keys in Java. The keys are used for SSL connectivity. There are two key types, private and public. The private key holds the site identity. The public key is generated from the private key and it is passed to the other party where it is installed in a special JKS keystore.</p>
<p>Most of the time, the keystore files are manipulated with a special Java tool called keytool. The tool can be found in the bin folder of the Java installation.</p>
<p>In order to move a key from a keystore file to the other, you need to export the key from the original keystore and import it in the second one.</p>
<p><strong>Problem</strong><br />
When you use keytool with the export option on a private key, the tool does not extract the actual private key from the store (for security reasons). Only the public key is extracted.</p>
<p>That means you can&#8217;t use keytool for this operation.</p>
<p><strong>Solution</strong><br />
There is a class in JDK called java.security.KeyStore. Keytool is implemented using this class functionality as well.</p>
<p>Given a JKS keystore file, we will write the code that will proceed through the following steps:</p>
<ol>
<li>Open the keystore file</li>
<li>Load both the key and the keychain</li>
<li>Open another keystore</li>
<li>Save the loaded key in the keystore</li>
</ol>
<p>First we assume we have two keystores, one is called first.jks and contains a private key under the alias first, and one called second.jks empty.</p>
<p>The full page code is <a href="http://www.proghowto.com/samples/movekey/MoveKey.java.html">here</a></p>
<p>And here is the result while listing the second keystore:</p>
<p>C:\temp\certs&gt;keytool -list -storepass second -keystore second.jks</p>
<p>Keystore type: jks<br />
Keystore provider: SUN</p>
<p>Your keystore contains 1 entry</p>
<p>second, 10-Oct-2008, keyEntry,<br />
Certificate fingerprint (MD5): 33:8A:A3:7C:9E:FD:31:39:17:A1:E4:F4:0D:3B:C1:8F</p>
]]></content:encoded>
			<wfw:commentRss>http://proghowto.com/move-a-private-key-between-two-jks-keystore/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
