tag:blogger.com,1999:blog-66119304039007721362024-03-13T20:02:38.398-07:00Overloaded With Java?Clarification of your queries on javaNatarajahttp://www.blogger.com/profile/03634459879357983679noreply@blogger.comBlogger14125tag:blogger.com,1999:blog-6611930403900772136.post-5407811136451300912010-08-30T23:56:00.001-07:002010-08-30T23:59:53.307-07:00How to remove HTML tags from a Java String?Some times we need to remove HTML tags from a java string before publishing it to the page. We can achieve this by using a regular express. Sample code is given below for your reference.<br />
<br />
public class RemoveTags{<br />
public static void main(String a[]){<br />
String text = "<b>I dont want this to be bold<\b>";<br />
text = text.replaceAll("\\<.*?\\>", "");<br />
System.out.println(text);<br />
}<br />
}<br />
<br />
<b>Output:</b><br />
<br />
I dont want this to be boldNatarajahttp://www.blogger.com/profile/03634459879357983679noreply@blogger.com0tag:blogger.com,1999:blog-6611930403900772136.post-70399390930073817112010-08-30T23:42:00.001-07:002010-08-30T23:42:31.506-07:00How to remove non-ASCII characters from a Java String?We can remove non-ASCII characters from a Java String by using below regular expression.<br />
<br />
<i><b>String.replaceAll("[^\\p{ASCII}]", ""))</b></i>Natarajahttp://www.blogger.com/profile/03634459879357983679noreply@blogger.com0tag:blogger.com,1999:blog-6611930403900772136.post-45971458435228655412010-04-06T07:30:00.000-07:002010-04-06T07:30:26.394-07:00How to formate date into user defined patterns?<div style="font-family: "Trebuchet MS",sans-serif;"><i>SimpleDateFormat </i>allows you to start by choosing any user-defined patterns for date-time formatting. All that you need to do is provide the date format string along with the constructor. You can find the format letters in <i>SimpleDateFormat </i>javadocs. For example we have given few formats below:<br />
<br />
<table border="0" cellpadding="0" cellspacing="3" summary="Examples of date and time patterns interpreted in the U.S. locale"><tbody>
<tr bgcolor="#ccccff"> <th align="left">Date and Time Pattern</th><th align="left">Result</th></tr>
<tr> <td><code>"yyyy.MM.dd G 'at' HH:mm:ss z"</code><br />
<br />
</td><td><code>2001.07.04 AD at 12:08:56 PDT</code></td></tr>
<tr bgcolor="#eeeeff"> <td><code>"EEE, MMM d, ''yy"</code></td><td><code>Wed, Jul 4, '01</code></td></tr>
<tr> <td><code>"h:mm a"</code></td><td><code>12:08 PM</code><br />
<br />
</td></tr>
<tr bgcolor="#eeeeff"> <td><code>"hh 'o''clock' a, zzzz"</code></td><td><code>12 o'clock PM, Pacific Daylight Time</code></td></tr>
<tr> <td><code>"K:mm a, z"</code></td><td><code>0:08 PM, PDT</code></td></tr>
</tbody></table><br />
<br />
The sample code also given below for your reference.<br />
<br />
<b>Sample Code:</b><br />
<i><br />
import java.text.SimpleDateFormat;<br />
import java.util.Date;<br />
<br />
public class DateFormatter {<br />
<br />
public static void main(String a[]){<br />
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");<br />
System.out.println("yyyy.MM.dd G 'at' HH:mm:ss z ---> "+sdf.format(new Date()));<br />
sdf = new SimpleDateFormat("hh 'o''clock' a, zzzz");<br />
System.out.println("hh 'o''clock' a, zzzz ---> "+sdf.format(new Date()));<br />
}<br />
}</i><br />
<br />
<b>Output:</b><br />
<br />
<i>yyyy.MM.dd G 'at' HH:mm:ss z ---> 2010.04.06 AD at 19:30:50 IST<br />
hh 'o''clock' a, zzzz ---> 07 o'clock PM, India Standard Time</i></div>Natarajahttp://www.blogger.com/profile/03634459879357983679noreply@blogger.com1tag:blogger.com,1999:blog-6611930403900772136.post-41896005911776567552010-04-05T08:53:00.000-07:002010-04-05T09:11:18.332-07:00How to get different country currency formats in java?<span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;">Java provides <i>NumberFormat</i> class to support currency formats based on local. All you need to do is get currency instance for NumberFormat class and calling format() method on top of the instance. An example is given below for your reference.</span><br />
<span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"><br />
</span><br />
<span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"><b>Sample Code:</b></span><br />
<span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"><br />
</span><br />
<span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"><i>import java.text.NumberFormat;</i></span><br />
<span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"><i>import java.util.Locale;</i></span><br />
<span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"><i><br />
</i></span><br />
<span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"><i>public class Currency {</i></span><br />
<span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"><i><br />
</i></span><br />
<span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"><i> public static void main(String a[]){</i></span><br />
<span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"><i> NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);</i></span><br />
<span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"><i> double currency = 52.12;</i></span><br />
<span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"><i> System.out.println("In Dollars: "+nf.format(currency));</i></span><br />
<span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"><i> nf = NumberFormat.getCurrencyInstance(Locale.UK);</i></span><br />
<span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"><i> System.out.println("In British Pound: "+nf.format(currency));</i></span><br />
<span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"><i> }</i></span><br />
<span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"><i>}</i></span><br />
<span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"><br />
</span><br />
<span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"><b>Output:</b></span><br />
<span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"><br />
</span><br />
<span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;">In Dollars: $52.12</span><br />
<span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;">In British Pound: £52.12</span>Natarajahttp://www.blogger.com/profile/03634459879357983679noreply@blogger.com0tag:blogger.com,1999:blog-6611930403900772136.post-79146590583625818322010-03-27T05:24:00.000-07:002010-04-06T06:15:45.033-07:00How to display special characters in HTML using Java?<span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;">To make special characters and accented letters show up on your web pages, use special set of codes called character entities, which you insert into your HTML code and which your browser will display as the corresponding symbols or characters you want. You can achieve this in java using apachie's String Escape Utils function. It provides functions to encode the special characters from java. The method is given below for your reference:</span><br />
<span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"><br />
</span><br />
<b><i><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;">StringEscapeUtils.escapeHtml(text);</span></i></b><br />
<span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"><br />
</span><br />
<span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;">Include </span><b><i><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;">commons-lang-2.4.jar</span></i></b><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"> jar file in your classpath.</span><br />
<span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"><br />
</span><br />
<span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;">Sometimes we need to display special characters in encoded format itself, you can use this class either way, it provides unescape functions for special characters to display its code in your web page itself.</span><br />
<span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"><br />
</span><br />
<span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;">This class provides escape util functions for </span><b><i><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;">XML, Javascript, CSV</span></i></b><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"> also.</span>Natarajahttp://www.blogger.com/profile/03634459879357983679noreply@blogger.com0tag:blogger.com,1999:blog-6611930403900772136.post-91832621198375003972010-02-12T06:40:00.000-08:002010-04-07T06:55:30.405-07:00oncontextmenu Event<span style="font-family: "Trebuchet MS",sans-serif;">How to disable right mouse click on a browser window? </span><br style="font-family: "Trebuchet MS",sans-serif;" /><br style="font-family: "Trebuchet MS",sans-serif;" /><span style="font-family: "Trebuchet MS",sans-serif;">It can be resolved by the useage of oncontextmenu event in your html body tag, the usage is shown given below.</span><br style="font-family: "Trebuchet MS",sans-serif;" /><br style="font-family: "Trebuchet MS",sans-serif;" /><b><i><span style="font-family: "Trebuchet MS",sans-serif;"><body oncontextmenu="return false;"></span></i></b>Natarajahttp://www.blogger.com/profile/03634459879357983679noreply@blogger.com1tag:blogger.com,1999:blog-6611930403900772136.post-54052273586817774802010-02-12T06:14:00.000-08:002010-04-06T06:15:34.531-07:00AllowEncodedSlashes Directive<span style="font-family: "Trebuchet MS",sans-serif;">Sometimes our web application fails to process request when the URL contains encoded slashes (it can be forward slash or backword slash). To avoid such kind of situations we should modify configurations in <i>Http Apache</i> server. Add below directive to <i>httpd.conf</i> file.</span><br />
<br />
<b><i><span style="font-family: "Trebuchet MS",sans-serif;">AllowEncodedSlashes On|Off</span></i></b><br />
<br />
<span style="font-family: "Trebuchet MS",sans-serif;">It determines whether encoded path separators in URLs are allowed to be passed through.</span><br />
<span style="font-family: "Trebuchet MS",sans-serif;">The default value will be Off.</span> <span style="font-family: "Trebuchet MS",sans-serif;"><b>Turn </b></span><b><span style="font-family: "Trebuchet MS",sans-serif;">it On</span></b>.<br />
<span style="font-family: "Trebuchet MS",sans-serif;">The AllowEncodedSlashes directive allows URLs which contain encoded path separators (%2F for / and additionally %5C for \ on according systems) to be used. Normally such URLs are refused with a 404 (Not found) error.</span>Natarajahttp://www.blogger.com/profile/03634459879357983679noreply@blogger.com0tag:blogger.com,1999:blog-6611930403900772136.post-33374185995400352612010-02-11T06:13:00.000-08:002010-04-07T06:56:08.536-07:00Should white spaces in template text between actions or directives be trimmed?<span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;">Sometimes when we generate response text from a JSP page, we will get white spaces/lines in the response text. To avoid that we have to configure our tomcat </span><i><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;">web.xml</span></i><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"> file. Follow below steps to avoid the white spaces.</span><br />
<span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"><br />
</span><br />
<span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;">Goto Tomcat Directory/conf folder</span><br />
<span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;">Open </span><i><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;">web.xml</span></i><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"> file to edit.</span><br />
<span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;">Search for a </span><i><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"><servlet></span></i><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"> tag, where you can find </span><i><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"><servlet-name>jsp</servlet-name></span></i><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"> .</span><br />
<span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;">Add </span><i><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"><init-param></span></i><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"> tag as like below:</span><br />
<span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"><br />
</span><br />
<b><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"><i><init-param></i></span></b><br />
<span class="Apple-tab-span" style="white-space: pre;"><b><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"><i> </i></span></b></span><b><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"><i><param-name>trimSpaces</param-name></i></span></b><br />
<span class="Apple-tab-span" style="white-space: pre;"><b><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"><i> </i></span></b></span><b><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"><i><param-value>true</param-value></i></span></b><br />
<b><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"><i></init-param></i></span></b><br />
<span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"><br />
</span><br />
<span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;">Save the web.xml file and restart the tomcat.</span><br />
<span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"><br />
</span><br />
<span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;">It will remove white spaces from your response text.</span>Natarajahttp://www.blogger.com/profile/03634459879357983679noreply@blogger.com0tag:blogger.com,1999:blog-6611930403900772136.post-73081715253025868322010-02-05T23:48:00.000-08:002010-04-07T06:57:26.906-07:00How to get auto-generated keys from database using jdbc in java<div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><br />
</span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';">There are cases that our database insert statements can generate auto-increment ids when we perform executeUpdate() method on our PreparedStatement object. The example given below gives an idea how to get those auto increment ids. After perfoming executeUpdate() method on PreparedStatement, call getGeneratedKeys() method on PreparedStatement. It will return you ResultSet, from which you can get auto increment column values. </span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><br />
</span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><i><b>Connection conn = null;</b></i> </span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><i><b>PreparedStatement pstmt = null;</b></i></span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><i><b>ResultSet rset = null;</b></i></span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><i><b>String query = "insert into emps (name, dept, salary) values (?,?,?)";</b></i></span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><i><b>try{</b></i></span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><b><i> //get connection object here</i></b></span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><b><i> conn = Get Connection Object Here;</i></b></span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><i><b><span class="Apple-tab-span" style="white-space: pre;"> </span>pstmt = conn.prepareStatement(query,Statement.RETURN_GENERATED_KEYS<span class="Apple-style-span" style="font-family: monospace; font-size: 11px; font-style: normal; font-weight: normal; white-space: nowrap;"><span class="Apple-style-span" style="font-family: 'trebuchet ms'; font-size: medium; white-space: normal;"><i><b>);</b></i></span></span></b></i></span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><i><b><span class="Apple-tab-span" style="white-space: pre;"> </span>pstmt.setString(1, "Junk Fellow");</b></i></span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><i><b><span class="Apple-tab-span" style="white-space: pre;"> </span>pstmt.setString(2, "Junk Dept");</b></i></span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><i><b><span class="Apple-tab-span" style="white-space: pre;"> </span>pstmt.setFloat(3, 10000.00);</b></i></span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><i><b><span class="Apple-tab-span" style="white-space: pre;"> </span>pstmt.executeUpdate();</b></i></span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><i><b><span class="Apple-tab-span" style="white-space: pre;"> </span>rset = pstmt.getGeneratedKeys();</b></i></span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><i><b><span class="Apple-tab-span" style="white-space: pre;"> </span>if(rset != null && rset.next()){</b></i></span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><i><b><span class="Apple-tab-span" style="white-space: pre;"> </span>System.out.println("Generated Emp Id: "+rset.getInt(1));</b></i></span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><i><b><span class="Apple-tab-span" style="white-space: pre;"> </span>}</b></i></span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><i><b>} catch (SQLException exx) {</b></i></span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><i><b><span class="Apple-tab-span" style="white-space: pre;"> </span>exx.printStackTrace();</b></i></span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><i><b>} finally {</b></i></span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><i><b><span class="Apple-tab-span" style="white-space: pre;"> </span>try{</b></i></span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><b><i> if(conn != null) conn.close();</i></b></span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><i><b><span class="Apple-tab-span" style="white-space: pre;"> </span>if(rset != null) rset.close();</b></i></span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><i><b><span class="Apple-tab-span" style="white-space: pre;"> </span>if(pstmt != null) pstmt.close();</b></i></span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><i><b><span class="Apple-tab-span" style="white-space: pre;"> </span>} catch(Exception ex){</b></i></span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><i><b><span class="Apple-tab-span" style="white-space: pre;"> </span>ex.printStackTrace();</b></i></span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><i><b><span class="Apple-tab-span" style="white-space: pre;"> </span>}</b></i></span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><i><b>}</b></i></span></div>Natarajahttp://www.blogger.com/profile/03634459879357983679noreply@blogger.com0tag:blogger.com,1999:blog-6611930403900772136.post-68578375909375417442010-02-04T05:49:00.000-08:002010-04-07T06:58:19.872-07:00Java Servlet API - ServletContextListener Usage Example Code<div><span class="Apple-style-span" style="font-family:'trebuchet ms';">Whenever the web application got instantiated, it initializes the class which implements ServletContextListener, and calls contextDestroyed() method. Also the application context calls contextDestroyed() method when the application getting shutting down. We can use this type of classes to initialize application sepecific processes.The example for ServletContextListener is given below.</span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><br /></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><b>package com.test.listener;</b></i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><b>import javax.servlet.ServletContextEvent;</b></i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><b>import javax.servlet.ServletContextListener;</b></i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><b><br /></b></i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><b>public final class ApplicationListenerContext implements ServletContextListener {</b></i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><b><br /></b></i></span></div><div><span class="Apple-tab-span" style="white-space:pre"><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><b> </b></i></span></span><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><b>public void contextInitialized(ServletContextEvent scEvent) {</b></i></span></div><div><span class="Apple-tab-span" style="white-space:pre"><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><b> </b></i></span></span><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><b>/*</b></i></span></div><div><span class="Apple-tab-span" style="white-space:pre"><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><b> </b></i></span></span><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><b>* This method will get called when the application instance got created.</b></i></span></div><div><span class="Apple-tab-span" style="white-space:pre"><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><b> </b></i></span></span><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><b>* Initialiaze required process here. </b></i></span></div><div><span class="Apple-tab-span" style="white-space:pre"><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><b> </b></i></span></span><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><b>*/</b></i></span></div><div><span class="Apple-tab-span" style="white-space:pre"><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><b> </b></i></span></span><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><b>}</b></i></span></div><div><span class="Apple-tab-span" style="white-space:pre"><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><b> </b></i></span></span><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><b>public void contextDestroyed(ServletContextEvent arg0) {</b></i></span></div><div><span class="Apple-tab-span" style="white-space:pre"><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><b> </b></i></span></span><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><b>/*</b></i></span></div><div><span class="Apple-tab-span" style="white-space:pre"><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><b> </b></i></span></span><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><b>* This method will get called when the application instance is shutting down.</b></i></span></div><div><span class="Apple-tab-span" style="white-space:pre"><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><b> </b></i></span></span><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><b>* Shutdown initialined processes here. </b></i></span></div><div><span class="Apple-tab-span" style="white-space:pre"><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><b> </b></i></span></span><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><b>*/</b></i></span></div><div><span class="Apple-tab-span" style="white-space:pre"><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><b> </b></i></span></span><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><b>}</b></i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><b>}</b></i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><br /></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';">Also add below entry to web.xml to register context listener.</span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><br /></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><b><listener></b></i></span></div><div><span class="Apple-tab-span" style="white-space:pre"><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><b> </b></i></span></span><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><b><listener-class>com.test.listener.ApplicationListenerContext</listener-class></b></i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><b></listener></b></i></span></div>Natarajahttp://www.blogger.com/profile/03634459879357983679noreply@blogger.com0tag:blogger.com,1999:blog-6611930403900772136.post-1904840535162766692010-02-03T05:28:00.000-08:002010-04-07T06:58:42.010-07:00Working with Java Memcached API<div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"></span><br />
<span class="Apple-style-span" style="font-family: 'trebuchet ms';">Memcached is a memory object caching system. It is used to improve the performance of web applications which is dynamic in nature. Memcached server is an in-memory key value store for small amount of data like strings, objects from results of database calls, API calls, or page rendering. It is very simple. Server (daemon) process written in C, providing an in-memory hashmap. Clients can be written in any language.<br />
<br />
The below example gives very basic example to understand memcached server. </span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><br />
</span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';">Set memcached clinet jar file (memcached-client-2.3.jar) in your classpath.</span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><br />
</span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><b>General Usage:</b></span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><br />
</span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';">Use below code to connect with memcache and push object to memcache and retrive object from memcache.</span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><br />
</span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><b><i>MemcachedClient c = null;</i></b></span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><b><i>try{</i></b></span></div><div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><b><i> </i></b></span></span><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><b><i>c = new MemcachedClient(new InetSocketAddress("memcached_host_IP", PORT));</i></b></span></div><div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><b><i> </i></b></span></span><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><b><i>c.set("MyTest", 60*60*24, new Integer(20));</i></b></span></div><div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><b><i> </i></b></span></span><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><b><i>int fromMC = (Integer)c.get("MyTest");</i></b></span></div><div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><b><i> </i></b></span></span><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><b><i>System.out.println(fromMC);</i></b></span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><b><i>} catch (Exception ex){</i></b></span></div><div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><b><i> </i></b></span></span><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><b><i>out.println("Exception raised when getting object from memcached; "+ex.getMessage());</i></b></span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><b><i>} finally{</i></b></span></div><div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><b><i> </i></b></span></span><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><b><i>if(c != null) c.shutdown();</i></b></span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><b><i>}</i></b></span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><br />
</span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';">In the above code below line creates MemcachedClient with memcached ip, and port. It establishes connection to the memcache.</span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><br />
</span></div><div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-family: 'trebuchet ms';"> </span></span><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><i>c = new MemcachedClient(new InetSocketAddress("memcached_host_IP", PORT)); </i> </span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><br />
</span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';">The below line is storing Integer object into the memcache with a key "MyTest". You can get the object using this key. The object will be available in memcache for 24 hours. </span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><br />
</span></div><div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-family: 'trebuchet ms';"> </span></span><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><i>c.set("MyTest", 60*60*24, new Integer(20));</i></span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><br />
</span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';">The below line gets the stored object from memcache by passing key. You need to do proper typecast.</span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><br />
</span></div><div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-family: 'trebuchet ms';"> </span></span><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><i>int fromMC = (Integer)c.get("MyTest");</i></span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><br />
</span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';">The finally block below closes the memcache client connection.</span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><br />
</span></div><div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-family: 'trebuchet ms';"> </span></span><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><i>finally{</i></span></div><div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><i> </i></span></span><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><i>if(c != null) c.shutdown();</i></span></div><div><span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><i> </i></span></span><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><i>}</i></span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><br />
</span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><br />
</span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';">Advaned Usage:</span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><br />
</span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';">MemcachedClient may be processing a great deal of asynchronous messages or possibly dealing with an unreachable memcached, which may delay processing. If a memcached is disabled, for example, MemcachedConnection will continue to attempt to reconnect and replay pending operations until it comes back up. To prevent this from causing your application to hang, you can use one of the asynchronous mechanisms to time out a request and cancel the operation to the server.</span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><br />
</span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';">Get a memcached client connected to several servers, over the binary protocol.</span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><br />
</span></div><div><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><i><b>MemcachedClient c = new MemcachedClient(new BinaryConnectionFactory(), AddrUtil.getAddresses("server1:11211 server2:11211"));</b></i></span></div><div><br />
</div>Natarajahttp://www.blogger.com/profile/03634459879357983679noreply@blogger.com0tag:blogger.com,1999:blog-6611930403900772136.post-54799548537663502372010-01-20T06:50:00.000-08:002010-04-07T06:59:36.979-07:00How to create XML file using JDOM in java<div><span class="Apple-style-span" style="font-family:'trebuchet ms';">This example creates xml file using JDOM apis .</span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><br /></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i>import java.io.FileOutputStream;</i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i>import java.io.OutputStream;</i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i>import org.jdom.Document;</i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i>import org.jdom.Element;</i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i>import org.jdom.output.XMLOutputter;</i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><br /></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i>public class CreateXML {</i></span></div><div> </div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><span class="Apple-tab-span" style="white-space:pre"> </span>public static void main(String a[]){</i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><span class="Apple-tab-span" style="white-space:pre"> </span>OutputStream fos = null;</i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><span class="Apple-tab-span" style="white-space:pre"> </span>Document doc = new Document();</i></span></div><div><span class="Apple-tab-span" style="white-space:pre"><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i> <span class="Apple-tab-span" style="white-space:pre"> </span></i></span></span><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i>Element root = new Element("emps");</i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><span class="Apple-tab-span" style="white-space:pre"> </span>doc.setRootElement(root);</i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><span class="Apple-tab-span" style="white-space:pre"> </span>// create emp element</i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><span class="Apple-tab-span" style="white-space:pre"> </span>Element emp = new Element("emp");</i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><span class="Apple-tab-span" style="white-space:pre"> </span>emp.setAttribute("id", "1016");</i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><span class="Apple-tab-span" style="white-space:pre"> </span>Element name = new Element("name");</i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><span class="Apple-tab-span" style="white-space:pre"> </span>name.setText("Nataraj");</i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><span class="Apple-tab-span" style="white-space:pre"> </span>emp.addContent(name);</i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><span class="Apple-tab-span" style="white-space:pre"> </span>Element dep = new Element("dep");</i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><span class="Apple-tab-span" style="white-space:pre"> </span>dep.setText("development");</i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><span class="Apple-tab-span" style="white-space:pre"> </span>emp.addContent(dep);</i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><span class="Apple-tab-span" style="white-space:pre"> </span>//Add emp element to root element</i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><span class="Apple-tab-span" style="white-space:pre"> </span>root.addContent(emp);</i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><span class="Apple-tab-span" style="white-space:pre"> </span>Element emp1 = new Element("emp");</i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><span class="Apple-tab-span" style="white-space:pre"> </span>emp1.setAttribute("id", "1015");</i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><span class="Apple-tab-span" style="white-space:pre"> </span>Element name1 = new Element("name");</i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><span class="Apple-tab-span" style="white-space:pre"> </span>name1.setText("Nagesh");</i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><span class="Apple-tab-span" style="white-space:pre"> </span>emp1.addContent(name1);</i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><span class="Apple-tab-span" style="white-space:pre"> </span>Element dep1 = new Element("dep");</i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><span class="Apple-tab-span" style="white-space:pre"> </span>dep1.setText("accounts");</i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><span class="Apple-tab-span" style="white-space:pre"> </span>emp1.addContent(dep1);</i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><span class="Apple-tab-span" style="white-space:pre"> </span>//Add emp element to root element</i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><span class="Apple-tab-span" style="white-space:pre"> </span>root.addContent(emp1);</i></span></div><div> </div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><span class="Apple-tab-span" style="white-space:pre"> </span>try{</i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><span class="Apple-tab-span" style="white-space:pre"> </span>fos = new FileOutputStream("c://test.xml");</i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><span class="Apple-tab-span" style="white-space:pre"> </span>new XMLOutputter().output(doc, fos);</i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><span class="Apple-tab-span" style="white-space:pre"> </span>} catch(Exception ex){</i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><span class="Apple-tab-span" style="white-space:pre"> </span>ex.printStackTrace();</i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><span class="Apple-tab-span" style="white-space:pre"> </span>} finally {</i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><span class="Apple-tab-span" style="white-space:pre"> </span>try{</i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><span class="Apple-tab-span" style="white-space:pre"> </span>if(fos != null) fos.close();</i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><span class="Apple-tab-span" style="white-space:pre"> </span>} catch(Exception ex){</i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><span class="Apple-tab-span" style="white-space:pre"> </span>ex.printStackTrace();</i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><span class="Apple-tab-span" style="white-space:pre"> </span>}</i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><span class="Apple-tab-span" style="white-space:pre"> </span>}</i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i><span class="Apple-tab-span" style="white-space:pre"> </span>}</i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i>}</i></span></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><br /></span></div><div><b><span class="Apple-style-span" style="font-family:'trebuchet ms';">OUTPUT:</span></b></div><div><b><span class="Apple-style-span" style="font-family:'trebuchet ms';"><br /></span></b></div><div><span class="Apple-style-span" style="font-size:12px;"><span class="Apple-style-span" style="font-family:'trebuchet ms';"><</span></span><span class="Apple-style-span" style="font-family:'trebuchet ms';">emps</span><span class="Apple-style-span" style="font-size:12px;"><span class="Apple-style-span" style="font-family:'trebuchet ms';">><</span><span class="Apple-style-span" style="font-size:16px;"><span class="Apple-style-span" style="font-family:'trebuchet ms';">emp id="1016"</span><span class="Apple-style-span" style="font-size:12px;"><span class="Apple-style-span" style="font-family:'trebuchet ms';">><</span><span class="Apple-style-span" style="font-size:16px;"><span class="Apple-style-span" style="font-family:'trebuchet ms';">name</span><span class="Apple-style-span" style="font-size:12px;"><span class="Apple-style-span" style="font-family:'trebuchet ms';">></span><span class="Apple-style-span" style="font-size:16px;"><span class="Apple-style-span" style="font-family:'trebuchet ms';">Nataraj</span><span class="Apple-style-span" style="font-size:12px;"><span class="Apple-style-span" style="font-family:'trebuchet ms';"><</span><span class="Apple-style-span" style="font-size:16px;"><span class="Apple-style-span" style="font-family:'trebuchet ms';">/name</span><span class="Apple-style-span" style="font-size:12px;"><span class="Apple-style-span" style="font-family:'trebuchet ms';">><</span><span class="Apple-style-span" style="font-size:16px;"><span class="Apple-style-span" style="font-family:'trebuchet ms';">dep</span><span class="Apple-style-span" style="font-size:12px;"><span class="Apple-style-span" style="font-family:'trebuchet ms';">></span><span class="Apple-style-span" style="font-size:16px;"><span class="Apple-style-span" style="font-family:'trebuchet ms';">development</span><span class="Apple-style-span" style="font-size:12px;"><span class="Apple-style-span" style="font-family:'trebuchet ms';"><</span><span class="Apple-style-span" style="font-size:16px;"><span class="Apple-style-span" style="font-family:'trebuchet ms';">/dep</span><span class="Apple-style-span" style="font-size:12px;"><span class="Apple-style-span" style="font-family:'trebuchet ms';">><</span><span class="Apple-style-span" style="font-size:16px;"><span class="Apple-style-span" style="font-family:'trebuchet ms';">/emp</span><span class="Apple-style-span" style="font-size:12px;"><span class="Apple-style-span" style="font-family:'trebuchet ms';">><</span><span class="Apple-style-span" style="font-size:16px;"><span class="Apple-style-span" style="font-family:'trebuchet ms';">emp id="1015"</span><span class="Apple-style-span" style="font-size:12px;"><span class="Apple-style-span" style="font-family:'trebuchet ms';">><</span><span class="Apple-style-span" style="font-size:16px;"><span class="Apple-style-span" style="font-family:'trebuchet ms';">name</span><span class="Apple-style-span" style="font-size:12px;"><span class="Apple-style-span" style="font-family:'trebuchet ms';">></span><span class="Apple-style-span" style="font-size:16px;"><span class="Apple-style-span" style="font-family:'trebuchet ms';">Nagesh</span><span class="Apple-style-span" style="font-size:12px;"><span class="Apple-style-span" style="font-family:'trebuchet ms';"><</span><span class="Apple-style-span" style="font-size:16px;"><span class="Apple-style-span" style="font-family:'trebuchet ms';">/name</span><span class="Apple-style-span" style="font-size:12px;"><span class="Apple-style-span" style="font-family:'trebuchet ms';">><</span><span class="Apple-style-span" style="font-size:16px;"><span class="Apple-style-span" style="font-family:'trebuchet ms';">dep</span><span class="Apple-style-span" style="font-size:12px;"><span class="Apple-style-span" style="font-family:'trebuchet ms';">></span><span class="Apple-style-span" style="font-size:16px;"><span class="Apple-style-span" style="font-family:'trebuchet ms';">accounts</span><span class="Apple-style-span" style="font-size:12px;"><span class="Apple-style-span" style="font-family:'trebuchet ms';"><</span><span class="Apple-style-span" style="font-size:16px;"><span class="Apple-style-span" style="font-family:'trebuchet ms';">/dep</span><span class="Apple-style-span" style="font-size:12px;"><span class="Apple-style-span" style="font-family:'trebuchet ms';">><</span><span class="Apple-style-span" style="font-size:16px;"><span class="Apple-style-span" style="font-family:'trebuchet ms';">/emp</span><span class="Apple-style-span" style="font-size:12px;"><span class="Apple-style-span" style="font-family:'trebuchet ms';">><</span><span class="Apple-style-span" style="font-size:16px;"><span class="Apple-style-span" style="font-family:'trebuchet ms';">/emps</span><span class="Apple-style-span" style="font-size:12px;"><span class="Apple-style-span" style="font-family:'trebuchet ms';">></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></div><div><b><br /></b></div>Natarajahttp://www.blogger.com/profile/03634459879357983679noreply@blogger.com0tag:blogger.com,1999:blog-6611930403900772136.post-35120403546132739042010-01-19T08:33:00.001-08:002010-04-07T07:00:02.141-07:00How to read XML file using JDOM in java<span class="Apple-style-span" style="font-family:'trebuchet ms';">This article gives you an example to read xml file using JDOM api. We will read xml file and print the values on to the console:<br /><br />Save below xml into emp.xml file.<br /><br /><emps><br /><emp id="1016"><br /><name>Nataraj</name><br /><dep>Accounts</dep><br /></emp><br /><emp id="1015"><br /><name>Nagesh</name><br /><dep>Marketing</dep><br /></emp><br /></emps><br /><br /></span><div><div><b><span class="Apple-style-span" style="font-family:'trebuchet ms';"><br /></span></b><div><b><span class="Apple-style-span" style="font-family:'trebuchet ms';">Java code to read xml file:</span></b></div><div><span class="Apple-style-span" style="font-family:'trebuchet ms';"><br /><i>import java.io.File;<br />import java.io.FileInputStream;<br />import java.io.InputStream;<br />import java.util.List;<br />import org.jdom.Document;<br />import org.jdom.Element;<br />import org.jdom.input.SAXBuilder;<br /><br />public class ParseXml {<br /><br /><span class="Apple-tab-span" style="white-space:pre"> </span>public static void main(String a[]){<br /><br /><span class="Apple-tab-span" style="white-space:pre"> </span>Document xmlDoc = null;<br /><span class="Apple-tab-span" style="white-space:pre"> </span>SAXBuilder builder = new SAXBuilder();<br /><span class="Apple-tab-span" style="white-space:pre"> </span>InputStream is = null;<br /><span class="Apple-tab-span" style="white-space:pre"> </span>Element root = null;<br /><span class="Apple-tab-span" style="white-space:pre"> </span>try {<br /><span class="Apple-tab-span" style="white-space:pre"> </span>is = new FileInputStream(new File("C:\\emp.xml"));<br /><span class="Apple-tab-span" style="white-space:pre"> </span>xmlDoc = builder.build(is);<br /><span class="Apple-tab-span" style="white-space:pre"> </span>if(xmlDoc != null){<br /><span class="Apple-tab-span" style="white-space:pre"> </span>root = xmlDoc.getRootElement();<br /><span class="Apple-tab-span" style="white-space:pre"> </span>System.out.println("Root element name: "+root.getName());<br /><span class="Apple-tab-span" style="white-space:pre"> </span>List</i></span><element><span class="Apple-style-span" style="font-family:'trebuchet ms';"><i> empList = root.getChildren("emp");<br /><span class="Apple-tab-span" style="white-space:pre"> </span>System.out.println("Emp Size: "+empList.size());<br /><span class="Apple-tab-span" style="white-space:pre"> </span>for(Element emp:empList){<br /><span class="Apple-tab-span" style="white-space:pre"> </span>System.out.println("Id: "+emp.getAttributeValue("id"));<br /><span class="Apple-tab-span" style="white-space:pre"> </span>Element name = emp.getChild("name");<br /><span class="Apple-tab-span" style="white-space:pre"> </span>System.out.println("Name: "+name.getText());<br /><span class="Apple-tab-span" style="white-space:pre"> </span>Element dep = emp.getChild("dep");<br /><span class="Apple-tab-span" style="white-space:pre"> </span>System.out.println("Deportment: "+dep.getText());<br /><span class="Apple-tab-span" style="white-space:pre"> </span>System.out.println("<--------------------------------------->");<br /><span class="Apple-tab-span" style="white-space:pre"> </span>}<br /><span class="Apple-tab-span" style="white-space:pre"> </span>}<br /><span class="Apple-tab-span" style="white-space:pre"> </span>} catch (Exception ex) {<br /><span class="Apple-tab-span" style="white-space:pre"> </span>ex.printStackTrace();<br /><span class="Apple-tab-span" style="white-space:pre"> </span>}<br /><span class="Apple-tab-span" style="white-space:pre"> </span>}<br />}<br /></i><br /><br /></span><b><span class="Apple-style-span" style="font-family:'trebuchet ms';">OUTPUT:</span></b><span class="Apple-style-span" style="font-family:'trebuchet ms';"><br />-----------<br />Root element name: emps<br />Emp Size: 2<br />Id: 1016<br />Name: Nataraj<br />Deportment: Accounts<br /><---------------------------------------><br />Id: 1015<br />Name: Nagesh<br />Deportment: Marketing<br /><---------------------------------------></span></element></div></div></div>Natarajahttp://www.blogger.com/profile/03634459879357983679noreply@blogger.com0tag:blogger.com,1999:blog-6611930403900772136.post-69383786065846691992010-01-18T08:50:00.000-08:002010-04-07T07:00:49.010-07:00How to get ClassLoader or Resource in a java static method?<span style="font-family: trebuchet ms;">Follow the below steps to load property file in a java static method.</span><br />
<br />
<span style="font-family: trebuchet ms;">Properties prop = new Properties();</span><br />
<br />
<span style="font-family: trebuchet ms;">prop.load(YourClassName.class.getResourceAsStream("File path"));</span>Natarajahttp://www.blogger.com/profile/03634459879357983679noreply@blogger.com0