<?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>Code Snippets &#8211; Tech-Freaks.com</title>
	<atom:link href="https://www.tech-freaks.com/java/code-snippets/feed" rel="self" type="application/rss+xml" />
	<link>https://www.tech-freaks.com</link>
	<description></description>
	<lastBuildDate>Wed, 09 Sep 2009 23:39:07 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://www.tech-freaks.com/wp-content/uploads/2025/07/cropped-tech-freaks-site-icon-512x512-1-32x32.png</url>
	<title>Code Snippets &#8211; Tech-Freaks.com</title>
	<link>https://www.tech-freaks.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Check All checkbox using Jquery</title>
		<link>https://www.tech-freaks.com/java/code-snippets/jquery-checkall-checkbox.html</link>
					<comments>https://www.tech-freaks.com/java/code-snippets/jquery-checkall-checkbox.html#respond</comments>
		
		<dc:creator><![CDATA[Tech Programmer]]></dc:creator>
		<pubDate>Wed, 09 Sep 2009 23:39:07 +0000</pubDate>
				<category><![CDATA[Code Snippets]]></category>
		<guid isPermaLink="false">http://localhost/tfcom_wp/2009/09/09/jquery-checkall-checkbox/</guid>

					<description><![CDATA[This article explains how Jquery can be used to implement a check all checkbox. On checking this &#8216;check all&#8217; checkbox, [&#8230;]]]></description>
										<content:encoded><![CDATA[		<div class="wpulike wpulike-default " ><div class="wp_ulike_general_class wp_ulike_is_restricted"><button type="button"
					aria-label="Like Button"
					data-ulike-id="86"
					data-ulike-nonce="a0115f9544"
					data-ulike-type="post"
					data-ulike-template="wpulike-default"
					data-ulike-display-likers=""
					data-ulike-likers-style="popover"
					class="wp_ulike_btn wp_ulike_put_image wp_post_btn_86"></button><span class="count-box wp_ulike_counter_up" data-ulike-counter-value="0"></span>			</div></div>
	<p>This article explains how Jquery can be used to implement a check all checkbox. On checking this &#8216;check all&#8217; checkbox, all other checkboxes present in the page will be checked and when unchecked all the checkboxes will be unchecked.</p>
<p>The below HTML code provides such an example:</p>
<pre class="language-markup"><code>&lt;html&gt;
 &lt;head&gt;
  &lt;script type="text/javascript" src="static/jquery-1.3.1.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript"&gt;
   //Gets called after the page is completely loaded
   $(document).ready( function() {
    //Get the click event of id checkAll
    $('input#checkAll').click(function(){
     //Get the state of checkAll button.
     //We will use it later to assign it to all other checkboxes
     //See this currently points to checkAll element
     var checkAllState = this.checked;
     //Fetch all checkboxes
     $('input:checkbox').each(function() {
      //this now points to each checkbox iterated
      this.checked = checkAllState;
     });
    });
   });
  &lt;/script&gt;
 &lt;/head&gt;
 &lt;body&gt;
  &lt;div&gt;&lt;h2&gt;Check All Check Box&lt;/h2&gt;&lt;/div&gt;
  &lt;div&gt;&lt;input type="checkbox" id="checkAll" name="checkAll"/&gt;&amp;nbsp;&lt;b&gt;(Un)Check All Color&lt;/b&gt;&lt;/div&gt;
  &lt;div&gt;&lt;input type="checkbox" name="yellow"/&gt;&amp;nbsp;Yellow&lt;/div&gt;
  &lt;div&gt;&lt;input type="checkbox" name="green"/&gt;&amp;nbsp;Green&lt;/div&gt;
  &lt;div&gt;&lt;input type="checkbox" name="red"/&gt;&amp;nbsp;Red&lt;/div&gt;
  &lt;div&gt;&lt;input type="checkbox" name="blue"/&gt;&amp;nbsp;Blue&lt;/div&gt;
  &lt;div&gt;&lt;input type="checkbox" name="pink"/&gt;&amp;nbsp;Pink&lt;/div&gt;
    &lt;/body&gt;
&lt;/html&gt;</code></pre>
<p>In the javascript section, comments are provided in line explaining the code.</p>
<p>Save the above code as a HTML including the Jquery lib in correct path and see the above code in action.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.tech-freaks.com/java/code-snippets/jquery-checkall-checkbox.html/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Jquery way to display error message</title>
		<link>https://www.tech-freaks.com/java/code-snippets/jquery-error-display.html</link>
					<comments>https://www.tech-freaks.com/java/code-snippets/jquery-error-display.html#respond</comments>
		
		<dc:creator><![CDATA[Tech Programmer]]></dc:creator>
		<pubDate>Wed, 09 Sep 2009 00:47:38 +0000</pubDate>
				<category><![CDATA[Code Snippets]]></category>
		<guid isPermaLink="false">http://localhost/tfcom_wp/2009/09/09/jquery-error-display/</guid>

					<description><![CDATA[This article explains how to display client side validation errors in web page instead of the classical alert message using [&#8230;]]]></description>
										<content:encoded><![CDATA[		<div class="wpulike wpulike-default " ><div class="wp_ulike_general_class wp_ulike_is_restricted"><button type="button"
					aria-label="Like Button"
					data-ulike-id="85"
					data-ulike-nonce="39a491a2f3"
					data-ulike-type="post"
					data-ulike-template="wpulike-default"
					data-ulike-display-likers=""
					data-ulike-likers-style="popover"
					class="wp_ulike_btn wp_ulike_put_image wp_post_btn_85"></button><span class="count-box wp_ulike_counter_up" data-ulike-counter-value="0"></span>			</div></div>
	<p style="margin: 1em 0px; padding: 0px; text-align: left;">This article explains how to display client side validation errors in web page instead of the classical alert message using Jquery. The working is based on a error message div element. The error messages is added to it, if any, along with a change in background color.</p>
<p style="text-align: left;">The below code validates two fields email address and confirm email address. The example does not check for validity of email field. It just checks for empty string for the two fields. If both the fields are not empty, it just compares and displays a message telling that the two fields do not match. </p>
<pre class="language-markup"><code>&lt;html&gt;
    &lt;head&gt;
        &lt;script type="text/javascript" src="static/jquery-1.3.1.js"&gt;&lt;/script&gt;
        &lt;script type="text/javascript"&gt;
            //Gets called after the page is completely loaded
            $(document).ready( function() {
                $('input#submit').click(function(event){
                    $("div#errMsg").html("");
                    var email = $("input#email").val();
                    var confEmail = $("input#confEmail").val();
                   
                    if(email!="" &amp;&amp; confEmail!="") {
                        if(email!=confEmail) {
                            $("div#errMsg").css("background-color", "red");
                            $("div#errMsg").html("Confirm email does not match with email");
                        }
                    } else {
                        var errMsg = "&lt;ul&gt;";
                        if(email=="")
                            errMsg = errMsg+"&lt;li&gt;Please enter email address&lt;/li&gt;";
                        if(confEmail=="")
                            errMsg = errMsg+"&lt;li&gt;Please enter confirm email&lt;/li&gt;";
                           
                        errMsg=errMsg+"&lt;/ul&gt;";
                        $("div#errMsg").css("background-color", "red");
                        $("div#errMsg").html(errMsg);
                    }   
                });
            });
        &lt;/script&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;div&gt;&lt;h2&gt;Jquery Way of message display&lt;/h2&gt;&lt;/div&gt;
        &lt;div id="errMsg"&gt;&lt;/div&gt;
        &lt;div id="emailDiv"&gt;Email Address: &lt;input type="text" id="email" name="email"/&gt;&lt;/div&gt;
        &lt;div id="confEmailDiv"&gt;Confirm Email: &lt;input type="text" id="confEmail" name="confirmEmail"/&gt;&lt;/div&gt;
        &lt;div&gt;&lt;input type="submit" name="submit" id="submit"/&gt;&lt;/div&gt;
    &lt;/body&gt;
&lt;/html&gt;</code></pre>
<pre class="brush:xml" style="text-align: left;"> </pre>
<p style="margin: 1em 0px; padding: 0px; text-align: left;">The code purposely uses <em>element.css() </em>method for adding background color to the error div. Ideally, we should define a CSS class for the error state and add class using addClass method.</p>
<p style="margin: 1em 0px; padding: 0px; text-align: left;">It also explains the <em>element.html() </em>method for injecting content between the div tags.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.tech-freaks.com/java/code-snippets/jquery-error-display.html/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>A Jquery way to include file</title>
		<link>https://www.tech-freaks.com/java/code-snippets/jquery-ajax-file-include.html</link>
					<comments>https://www.tech-freaks.com/java/code-snippets/jquery-ajax-file-include.html#respond</comments>
		
		<dc:creator><![CDATA[Tech Programmer]]></dc:creator>
		<pubDate>Fri, 04 Sep 2009 01:18:00 +0000</pubDate>
				<category><![CDATA[Code Snippets]]></category>
		<guid isPermaLink="false">http://localhost/tfcom_wp/2009/09/04/jquery-ajax-file-include/</guid>

					<description><![CDATA[Although all server side components provide the option of including file like &#60;jsp:include&#62; etc, having a way to include files [&#8230;]]]></description>
										<content:encoded><![CDATA[		<div class="wpulike wpulike-default " ><div class="wp_ulike_general_class wp_ulike_is_restricted"><button type="button"
					aria-label="Like Button"
					data-ulike-id="84"
					data-ulike-nonce="b8d87b2b14"
					data-ulike-type="post"
					data-ulike-template="wpulike-default"
					data-ulike-display-likers=""
					data-ulike-likers-style="popover"
					class="wp_ulike_btn wp_ulike_put_image wp_post_btn_84"></button><span class="count-box wp_ulike_counter_up" data-ulike-counter-value="0"></span>			</div></div>
	<p style="text-align: left;">Although all server side components provide the option of including file like &lt;jsp:include&gt; etc, having a way to include files using clients side script can come handy if the pages are made of plain old HTML.</p>
<p style="text-align: left;">We provide a code for explaining how Jquery can be used to include files.</p>
<p style="text-align: left;">Main file:</p>
<pre class="language-javascript"><code>&lt;html&gt;
 &lt;head&gt;
  &lt;script type="text/javascript" src="static/jquery-1.3.1.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript"&gt;
   //Gets called after the page is completely loaded
   $(document).ready( function() {
    //loads include page using AJAX passing parameters and executes the function after completion
    $("#include").load("incJquery.jsp", {param1:25, param2:30}, function() {
     alert("Loading include file completed!");
    });
   });
  &lt;/script&gt;
 &lt;/head&gt;
 &lt;body&gt;
  &lt;div id="include"&gt;Loading....&lt;/div&gt;
  Welcome to Jquery &lt;br/&gt;
  You see me in the main file!
 &lt;/body&gt;
&lt;/html&gt;</code></pre>
<p style="text-align: left;">Include file:</p>
<pre class="language-markup"><code>&lt;%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %&gt;
&lt;h2&gt;Am from include file, thanks to Jquery!&lt;/h2&gt;
Value of Parameter1 is &lt;c:out value="${param.param1}"/&gt;
&lt;br/&gt;Value of Parameter1 is &lt;c:out value="${param.param2}"/&gt;&lt;p/&gt;</code></pre>
<p style="text-align: left;">The jquery load function basically is an AJAX call which loads the content and inserts it into the div with id=include.</p>
<p style="text-align: left;">The load used uses the three parameter function. The first parameter being the URL, second is data (which is basically a key/value pair object) and lastly a function. The data (key/value) gets passed to the URL as parameters. The include file can access the parameters like any GET parameters received.</p>
<p style="text-align: left;">Add both the files in a Java application server context. The files should be placed in same directory. Access the main file from browsers using a localhost url for going through the application server.</p>
<p style="text-align: left;">See the contents loaded from include file and also the parameter contents received in the include file.</p>
<blockquote>
<p style="text-align: left;"><em>Do not try to load pages from a different domain using the above script. It will not work and will provide a cross domain access error.</em></p>
</blockquote>
<p style="text-align: left;"> </p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.tech-freaks.com/java/code-snippets/jquery-ajax-file-include.html/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Accessing Request/session attributes using JSTL</title>
		<link>https://www.tech-freaks.com/java/code-snippets/request-session-jstl.html</link>
					<comments>https://www.tech-freaks.com/java/code-snippets/request-session-jstl.html#respond</comments>
		
		<dc:creator><![CDATA[Tech Programmer]]></dc:creator>
		<pubDate>Wed, 02 Sep 2009 21:35:38 +0000</pubDate>
				<category><![CDATA[Code Snippets]]></category>
		<guid isPermaLink="false">http://localhost/tfcom_wp/2009/09/02/request-session-jstl/</guid>

					<description><![CDATA[JSTL and EL together provide an easy way to access session, request and application scope variables. EL provides implicit variables [&#8230;]]]></description>
										<content:encoded><![CDATA[		<div class="wpulike wpulike-default " ><div class="wp_ulike_general_class wp_ulike_is_restricted"><button type="button"
					aria-label="Like Button"
					data-ulike-id="83"
					data-ulike-nonce="64a65c5435"
					data-ulike-type="post"
					data-ulike-template="wpulike-default"
					data-ulike-display-likers=""
					data-ulike-likers-style="popover"
					class="wp_ulike_btn wp_ulike_put_image wp_post_btn_83"></button><span class="count-box wp_ulike_counter_up" data-ulike-counter-value="0"></span>			</div></div>
	<div style="text-align: left;">JSTL and EL together provide an easy way to access session, request and application scope variables. EL provides implicit variables like sessionScope, requestScope, applicationScope and many more which are actually Map type. They can be navigated easily by using dot operator to access any attributes added to it.</div>
<div style="text-align: left;"> </div>
<div style="text-align: left;">Below JSP code provides a simple way of accessing and displaying the values.</div>
<div style="text-align: left;"> </div>
<div style="text-align: left;">Note that we have added the scriptlet in the page, just to show that we are adding value in session and request variables. Scriptlets should be avoided when JSTL and EL are used.</div>
<div style="text-align: left;">
<pre class="language-markup"><code>&lt;%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %&gt;
 
&lt;%  //Scriptlets are not to be used
 request.setAttribute("param1", "Hello JSTL World!");
 session.setAttribute("userName", "Generic User");
%&gt;

Hello &lt;c:out value="${sessionScope.userName}"/&gt;,&lt;/p&gt;
Welcome to &lt;c:out value="${requestScope.param1}"/&gt;&lt;/p&gt;</code></pre>
<p> </p></div>
<div style="text-align: left;">It provides a output as below:</div>
<div style="text-align: left;"> </div>
<div style="text-align: left;"><em>Hello Generic User,</em></div>
<div style="text-align: left;"><em>Welcome to Hello JSTL World!</em></div>
]]></content:encoded>
					
					<wfw:commentRss>https://www.tech-freaks.com/java/code-snippets/request-session-jstl.html/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Jquery way to simple popup window</title>
		<link>https://www.tech-freaks.com/java/code-snippets/jquery-popup-window.html</link>
					<comments>https://www.tech-freaks.com/java/code-snippets/jquery-popup-window.html#respond</comments>
		
		<dc:creator><![CDATA[Tech Programmer]]></dc:creator>
		<pubDate>Tue, 01 Sep 2009 23:49:17 +0000</pubDate>
				<category><![CDATA[Code Snippets]]></category>
		<guid isPermaLink="false">http://localhost/tfcom_wp/2009/09/01/jquery-popup-window/</guid>

					<description><![CDATA[One thing cool about Jquery is the ability to access an element based on class or id attribute and handle [&#8230;]]]></description>
										<content:encoded><![CDATA[		<div class="wpulike wpulike-default " ><div class="wp_ulike_general_class wp_ulike_is_restricted"><button type="button"
					aria-label="Like Button"
					data-ulike-id="82"
					data-ulike-nonce="bec5710649"
					data-ulike-type="post"
					data-ulike-template="wpulike-default"
					data-ulike-display-likers=""
					data-ulike-likers-style="popover"
					class="wp_ulike_btn wp_ulike_put_image wp_post_btn_82"></button><span class="count-box wp_ulike_counter_up" data-ulike-counter-value="0"></span>			</div></div>
	<div style="text-align: left;">One thing cool about Jquery is the ability to access an element based on class or id attribute and handle events without attaching the traditional onClick, onSubmit events on the element.</div>
<div style="text-align: left;"> </div>
<div style="text-align: left;">We examine a simple way for providing a link that will open in a popup window using Jquery.</div>
<div style="text-align: left;"> </div>
<div style="text-align: left;">Below is the code in a page. The Jquery function is added in the html page, but in real applications we may want to segregate it in a JS file. </div>
<div style="text-align: left;">
<pre class="language-javascript"><code>&lt;script type="text/javascript" src="static/jquery-1.3.1.js"&gt;&lt;/script&gt;
 
&lt;script type="text/javascript"&gt;
	$(document).ready( function() {
		$("a.home").click(
		function() {
			var href= $(this).attr("href");
			var arrHref = href.split("|");
			window.open(arrHref[0], "PopUp", arrHref[1])
			return false;
		});
	});
&lt;/script&gt;
 
&lt;a href="http://www.tech-freaks.com|toolbar=0,menubar=0,width=800,height=700,scrollbars=1"
class="home"&gt;Home Page&lt;/a&gt;</code></pre>
<p> </p></div>
<div style="text-align: left;">See how the href attribute is used to pass other window configuration attributes. The Jquery splits by the delimiter (|) and calls the popup function. This provides amazing configuration ability, although the function looks big compared to our traditional popup. The a href tag is clean with no onclick function which is a plus. </div>
<div style="text-align: left;"> </div>
<div style="text-align: left;">The only downside a developer unaware of Jquery may be confused on how that href attribute is able to generate a popup window. </div>
<div style="text-align: left;"> </div>
]]></content:encoded>
					
					<wfw:commentRss>https://www.tech-freaks.com/java/code-snippets/jquery-popup-window.html/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Iterating a Map using JSTL/EL</title>
		<link>https://www.tech-freaks.com/java/code-snippets/iterate-map-jstl-el.html</link>
					<comments>https://www.tech-freaks.com/java/code-snippets/iterate-map-jstl-el.html#respond</comments>
		
		<dc:creator><![CDATA[Tech Programmer]]></dc:creator>
		<pubDate>Mon, 31 Aug 2009 22:05:47 +0000</pubDate>
				<category><![CDATA[Code Snippets]]></category>
		<guid isPermaLink="false">http://localhost/tfcom_wp/2009/08/31/iterate-map-jstl-el/</guid>

					<description><![CDATA[Normally, we iterate through a list (like ArrayList, LinkedList etc) in JSP. For map (HashMap, TreeMap etc), we would generally [&#8230;]]]></description>
										<content:encoded><![CDATA[		<div class="wpulike wpulike-default " ><div class="wp_ulike_general_class wp_ulike_is_restricted"><button type="button"
					aria-label="Like Button"
					data-ulike-id="35"
					data-ulike-nonce="d08e9e1e2c"
					data-ulike-type="post"
					data-ulike-template="wpulike-default"
					data-ulike-display-likers=""
					data-ulike-likers-style="popover"
					class="wp_ulike_btn wp_ulike_put_image wp_post_btn_35"></button><span class="count-box wp_ulike_counter_up" data-ulike-counter-value="0"></span>			</div></div>
	<div style="text-align: left;">Normally, we iterate through a list (like ArrayList, LinkedList etc) in JSP. For map (HashMap, TreeMap etc), we would generally be picking up &#8216;value&#8217; based on the &#8216;key&#8217;.</div>
<div style="text-align: left;"> </div>
<div style="text-align: left;">However, there are situations in which we would like to iterate all the elements of a map and display its key and value. For example, display a list of attribute name and value of a guitar! Each attribute name of the guitar represented as a key and the attribute value as a value. The key value pair would be something like this (&#8216;Type&#8217;, &#8216;Electric&#8217;), (&#8216;Brand&#8217;, &#8216;Gibson&#8217;) etc.</div>
<div style="text-align: left;"><strong> </strong></div>
<div style="text-align: left;">
<div>
<pre class="language-markup"><code>  ....
  .... 
  &lt;c:forEach var="item" items="${itemsMap}"&gt;
${item.key} --&gt;  ${item.value} &lt;/br&gt;
  &lt;/c:forEach&gt;
  ....</code></pre>
</div>
</div>
<div style="text-align: left;">&#8216;itemsMap&#8217; would be a type of Map which is in request scope. </p>
<p>The below code is an excerpt from a servlet which adds &#8216;itemsMap&#8217; in request scope and forwards to the JSP which iterates the Map.</p></div>
<div style="text-align: left;">
<pre class="language-java"><code>public void service(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  Map map = new HashMap();
  map.put("Type", "Electric");
  map.put("Brand", "Gibson");
  map.put("Model", "Les Paul Black Beauty");
  request.setAttribute("itemsMap", map);
  
  request.getRequestDispatcher("/DisplayMap.jsp").forward(request, response);
 }</code></pre>
</div>
<div style="text-align: left;">One thing to worry about while iterating this way is the ordering of elements. Ensure to use a LinkedHashMap instead of a HashMap, if the order of the elements to be displayed is important. </div>
]]></content:encoded>
					
					<wfw:commentRss>https://www.tech-freaks.com/java/code-snippets/iterate-map-jstl-el.html/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
