View RSS Feed

All Blog Entries

  1. Composition Rather than Inheritance

    by , 11-29-2011 at 02:27 AM
    One of the more abused features of object oriented languages is inheritance. Second on the list of abused features of Java as an outgrowth of inheritance is the use of polymorphism. Developers seem to believe that everything should be inherited, and that classes should use polymorphism for every object irrespective of whether the arguments will be provided from external sources. Doing these sorts of things will make your designs more difficult to understand and lead to complications later when ...
  2. Construction for Your Java Classes with Many Parameters

    by , 11-29-2011 at 02:10 AM
    Traditionally when programmers have to create a constructor that has lots of parameters, some that are required and other that are optional, they often will either build several constructors or use the telescoping constructor pattern. For those of you not aware, the telescoping constructor pattern is where you provide constructors for the class with the required parameters, and then others constructors that cover the other optional parameters. So depending on the number of parameters and whether ...

    Updated 11-30-2011 at 03:06 PM by Design Patterns

    Categories
    Builder Pattern
  3. General Event Handling in Servlets Part 3

    by , 11-29-2011 at 12:35 AM
    In the first two article on general event handling with servlets we have covered how to detect changes in the servlet context and monitoring the creation and destruction of the servlet context. In this last article of the series we will look into how to use listeners with tag libraries. If you have not seen the first two articles of this series, General Event Handling in Servlets, Part 1 and General Event Handling in Servlets, Part 2, then I would recommend that you review them before continuing ...

    Updated 12-09-2011 at 05:16 PM by Servlet

    Categories
    Tutorial , Web , Listener , ServletContextListener
  4. General Event Handling in Servlets Part 2

    by , 11-29-2011 at 12:26 AM
    In the last article, we started to look into the Application Events Framework for servlets and javaserver pages. We listed eight different kinds of events listeners that are related to web application life-cycle events. If you are interested, go review the post, General Event Handling in Servlets Part 1.

    In that article, we looked at the first of the listeners for the initialization and destruction of servlet context and we presented an example of it’s use for a company that is constantly ...

    Updated 12-09-2011 at 05:13 PM by Servlet

    Categories
    ServletContextListener , Tutorial , Web , Listener
  5. General Event Handling in Servlets Part 1

    by , 11-29-2011 at 12:15 AM
    When you are developing applications using servlets or JSP pages, there are a number of tools at your disposal for handling the life cycle of individual servlets or JSP pages. The servlet init method fires when a servlet is first instantiated. With JSP pages it is the jspInit method. Both methods can use initialization parameters that are specified with the init-param subelement of the web.xml servlet element. Requests are handled with service and _jspService, and destruction is handled with destroy ...

    Updated 11-30-2011 at 03:15 PM by Servlet

    Categories
    Tutorial , Web , Listener , ServletContextListener
  6. OMA standard for data synchronisation - SyncML

    by , 11-28-2011 at 08:06 PM (My Java Tips)
    SyncML is the standard for data synchronization and is accepted by Open Mobile Alliance (OMA). All the major players, including Ericsson, Nokia, IBM, Motorola, and Symbian support this protocol.


    The synchronization of data has great importance in today’s world. You carry email applications, contact database, files and other stuff on your handheld devices. It would be great of you can synchronize those with your desktop/notebook. For that there has to be a agreed format.
    ...
    Tags: oma, syncml Add / Edit Tags
    Categories
    Java ME
  7. Using the DocumentBuilderFactory

    by , 11-28-2011 at 08:02 PM (My Java Tips)
    In this post, I will present an example to show how to use the DocumentBuilderFactory.


    First step is to import the required classes.

    Java Code:
    import java.io.File;
    import java.io.IOException;
    import java.io.OutputStreamWriter;
    import java.io.Writer;
    
    // JAXP
    import javax.xml.parsers.FactoryConfigurationError;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.parsers.DocumentBuilderFactory;
    ...
    Categories
    Java EE
  8. Working with the DOM parser

    by , 11-28-2011 at 07:59 PM (My Java Tips)
    You will be introduced to the working of DOM parser in this post.


    We can get a DocumentBuilder instance as soon as we have a DOM factory. The methods available for the DocumentBuilder instance are very similar to those available to its SAX counterpart. But there is a slight difference when we talk about the parse() method. The parse method in case of DOM do not take an instance of the SAX DefaultHandler class. It returns a DOM Document instance representing the XML document that was ...
    Categories
    Java EE
  9. Reading RSS using Informa API

    by , 11-28-2011 at 07:56 PM (My Java Tips)
    RSS (Really Simple Syndication) is a family of Web feed formats used to publish frequently updated content such as blog entries, news headlines, and podcasts. It is actually a specification for XML files to provide syndicated data.


    To read RSS feeds in a Java application is easy. You have to use Informa API which is availabel at:
    http://informa.sourceforge.net/.

    The code snippet given below shows how to read RSS using Informa API.

    Java Code:
    try {
    ...
    Tags: informa, rss Add / Edit Tags
    Categories
    Java EE
  10. Java Compiler API (brief intro)

    by , 11-28-2011 at 07:49 PM (My Java Tips)
    We all know that javac command is used to compile the java classes. Even if we are using some IDE (Eclipse, JBuilder, NetBeans), javac is called at the background for compilation. But with the release of Java 6, it has become possible to compile Java class from a Java class.


    The classes related to Java compiler are packed into javax.tools package.

    There is a class called ToolProvider (javax.tools) whose getSystemJavaCompiler() method returns an instance of some class ...
    Tags: ide, javac Add / Edit Tags
    Categories
    Java SE
  11. Compiling a Java class using JavaCompiler

    by , 11-28-2011 at 07:47 PM (My Java Tips)
    Java 6 introduces a way to compile Java classes from a Java class. In this post, I will present an example to show how this is done.


    I have a Test.java file:

    Java Code:
    public class Test{
    
    public static void main(String[] args) {
            // TODO code application logic here
            System.out.println("Hello World");
    callMe();
    }
        public static void callMe(){
         System.out.println("Hello Babar dost");
    ...
    Categories
    Java SE
  12. Retrieving auto generated key (JDBC 3.0)

    by , 11-28-2011 at 07:41 PM (My Java Tips)
    JDBC 3.0 introduces a lot of interesting and exciting features which makes database programming simpler. In this post, I will address how to retrieve auto generated keys using JDBC 3.0.


    To get the key, simply specify in the statement's execute() method an optional flag denoting that you are interested in the generated value. Flags of your interest are: Statement.RETURN_GENERATED_KEYS or Statement.NO_GENERATED_KEYS. Execute the statement and now you can obtain the values of the generated ...
    Categories
    JDBC
  13. Returning multiple results (JDBC 3.0)

    by , 11-28-2011 at 07:38 PM (My Java Tips)
    If you have worked with JDBC 2, you might be knowing that if your statement is returning multiple results, only one ResultSet can be opened at a time. This is a limitation. Good new is that JDBC 3.0 specification allows the Statement interface to support multiple open ResultSets. Lets see hot this can be done.



    Please note that the execute() method closes any ResultSets that were opened from a previous call. To support multiple open results, the Statement interface adds an ...
    Categories
    JDBC
  14. Using a Collection vs. Using an Iterator

    by , 11-28-2011 at 07:36 PM
    In this tip, we will look at determining the appropriate use of a collection versus using an iterator. One of the main reasons for using an interface is that you can create more generic and flexible code. By writing to an interface rather than an implementation, you can reuse your code for more types of objects.

    The root interface for all sequence containers is Collection. The java.utiLAbstractCollection class provides a default implementation for a Collection, so that you can create ...
  15. Prepared statement pooling (JDBC 3.0)

    by , 11-28-2011 at 07:35 PM (My Java Tips)
    JDBC 3.0 provides improved connection pooling. This post is all about that.



    It is also possible to pool prepared statements. A prepared statement allows us to keep frequently used SQL statement in a pre-compile form, thus improving performance if that statement is executed multiple times. But there is a dark side of this. Creating a PreparedStatement object introduces a certain amount of overhead. There are some developers sometimes change their object models to increase the ...
    Categories
    JDBC
  16. Transaction savepoints (JDBC 3.0)

    by , 11-28-2011 at 07:32 PM (My Java Tips)
    JDBC 2 provides complete transaction rollback control over transaction. But if you want to rollback to a point, you cannot do so. JDBC 3.0 introduces savepoints which makes this possible.


    JDBC 3.0 provides more control over transactions using savepoints. The Savepoint interface allows us to partition a transaction into logical breakpoints. This in turn provides control over how much of the transaction we wish to rolled back.

    Lets see how to do this. Review the following ...
    Tags: jdbc 2, savepoint Add / Edit Tags
    Categories
    JDBC
  17. Reading Request Headers from Servlets

    by , 11-28-2011 at 07:31 PM
    It is relatively easy to read headers. All you need to do is call the getHeader method of HttpServletRequest. If the specified header exists, the servlet returns a String, if not the servlet will return null. Unlike parameter names, header names are not case sensitive. Although getHeader is a general-purpose way to read incoming headers, there are a couple of headers that are so commonly used that they have special access methods in HttpServletRequest. They are listed below.
    • getCookies
    ...
  18. Using the JAXP validation framework

    by , 11-28-2011 at 07:29 PM (My Java Tips)
    While working with XML documents, you need to validate the documents. You may use setValidating() method on a SAX or DOM factory. But Java 5.0 (JAXP 1.3) introduces JAXP validation framework which can also be used for validating XML documents.


    Using the JAXP validation framework is fairly simple and efficient. In JAXP 1.3, the validation is broken out into several classes within the new javax.xml.validation package. Lets go through the steps:

    1. Load the model in to ...
    Tags: dom, java 5.0, jaxp, sax Add / Edit Tags
    Categories
    Java EE , XML
  19. Notes on Form Data for Servlets

    by , 11-28-2011 at 07:25 PM
    When you look at the URL in your web browser if you are using a search engine or on a retail site, you will notice URLs http://host/path?user=James+Rogers&s...rish&sub=music. The part after the question mark (i.e., user=James+Rogers&subj= folk&sub=irish&sub=music) is known as form or query data and is the most common way to get information from a Web page to a server-side program. Form or query data can be attached to the end of the URL after a question mark, for ...
    Categories
    Tutorial , HTTP , URL Mapping , Form
  20. XSLT processing in Java

    by , 11-28-2011 at 07:25 PM (My Java Tips)
    XSLT (Extensible Stylesheet Language Transformations) is used to transform XML files into other formats like HTML format. There are many XSLT processors (libraries) available to be used in Java for XSLT transformation. These libraries can be used from a Java application like JSP/Servlet to read a XML file and to transform it into a HTML.


    An XSLT processor takes two inputs: an XML file and an XSLT stylesheet.

    For this post, I have chosen Xalan-Java library for the transformation. ...
    Tags: html, xalan-java, xml, xslt Add / Edit Tags
    Categories
    XML
  21. Handling Form Data from Servlets

    by , 11-28-2011 at 07:23 PM
    A feature which is of immense benefits to all programmers working with servlets is that all form data parsing is handled automatically. Rather than needing to build your own called for parsing form data, you only need to call the getParameter method of the Http- ServletRequest, providing the relevant parameter name as an argument. You use getParameter in the same way whether data is sent by GET or by POST. The servlet knows which request method was used and calls the proper method to handle the ...
    Categories
    Form , Tutorial
  22. JAXP SAXParser class

    by , 11-28-2011 at 07:19 PM (My Java Tips)
    You can doa lot if interesting stuff once you have the instance of SAXParser class. I will introduce a code snippet that will show you what you can do with an instance of SAXParser.


    JAXP provides methods to determine the parser's settings. For instance:

    isValidating()
    use this method to see if the parser will perform validation or not

    isNamespaceAware()
    use this method to determine if the parser can process namespaces in an XML document ...
    Categories
    Java EE
  23. SAXParserFactory Example

    by , 11-28-2011 at 07:17 PM (My Java Tips)
    To change the parser implementations, JAXP provides a class called SAXParserFactory. I will present an example that will show how to use this class.


    First thing is to create an instance of SAXParserFactory. After creating the new instance, we have to get he SAX-capable parser. For thos, factory provides a method. Good thing is that the JAXP implementation takes care of the vendor-dependent code and thus keeps the code clean. This factory has some other nice features, as well. Do explore ...
    Categories
    Java EE
  24. Introduction to JAXP

    by , 11-28-2011 at 07:14 PM (My Java Tips)
    If you want to process XML data using applications written in the Java programming language, then JAXP is the best choice. JAXP stands for Java API for XML Processing. I will introduce JAXP in this post.


    JAXP can be called an abstraction layer since it does not introduces any new way to parse XML documents nor it provides different way of handling XML documents. Actually it makes using DOM and SAX easier. Using JAXP, you may habdle vendor-specific tasks that may arise when dealing ...
    Tags: dom, jaxp, sax Add / Edit Tags
    Categories
    Java EE
  25. Java Web Start (advantages)

    by , 11-28-2011 at 07:12 PM (My Java Tips)
    Ever thought of launching full-featured Java applications with a single click? Java Web Start provided with Java Standard Edition (J2SE™), version 5.0 helps making this possible. In this post, I will introduce you to this new and exciting feature.


    Java Web Start simplifies a lot of things. For instance a complete spreadsheet program or an Internet chat client normally involves going through complicated installation procedures. But with Java Web Start, you can download and launch applications ...
    Categories
    Java Web Start
  26. Creating the JNLP File (Java Web Start)

    by , 11-28-2011 at 07:08 PM (My Java Tips)
    If you plan to run an application with Java Web Start, you have to create JNLP (Java Network Launching Protocol) file. In this post, I will briefly explain how to do that.


    The JNLP file is an XML file and it contains elements and attributes that tell Java Web Start how to run the application.

    Presented below is a JNLP file for the Notepad application:

    <!--?xml version="1.0" encoding="utf-8"?-->
    Java Code:
    <jnlp spec="1.0">codebase="URL
    ...
    Tags: jnlp file, xml Add / Edit Tags
    Categories
    Java Web Start
  27. How to Debug Servlets

    by , 11-28-2011 at 07:07 PM
    Servlets can be difficult to debug because it is not possible to execute them directly. Instead, you trigger their execution by means of an HTTP request, and they are executed by the Web server. For remote execution, inserting break points or reading debugging messages and the stack traces becomes extremely difficult. Here I will outline some strategies to servlet debugging:
    1. Check the HTML source. If it doesn’t look correct, use the “View Source” command in order to check the HTML. If
    ...
    Categories
    Tutorial , HTTP
  28. Accessing Resources in a JAR File

    by , 11-28-2011 at 07:05 PM (My Java Tips)
    JAR files are used to deploy applications. They comprise of java classes and other resources like images etc. In this post, I will explain how to access resources packed in a JAR file.



    To access resources in a JAR file, we use getResource method. Lets do this practically. The code snippet provided shows how to retrieve images from a JAR file.

    Java Code:
    // Get current classloader
    ClassLoader cl = this.getClass().getClassLoader();
    // Create icons
    ...
    Categories
    Java SE
  29. Know the Servlet Life Cycle

    by , 11-28-2011 at 07:04 PM
    If you are programming in java on the back end, one of the key enabling technologies that you must know is servlets. Basically a servlet is a class that handles HTTP responses and makes HTTP Requests. I’m going to assume that you know enough about servlets to be able to override doGet and doPost in order to do some useful work (generate HTML) with your servlets, but what you might not know is the servlet lifecycle. I outline this below in order to improve your understanding how servlets work and ...
    Categories
    Tutorial , HTTP
  30. Creating JAR files

    by , 11-28-2011 at 07:02 PM (My Java Tips)
    This post is all about learning how to create JAR files. After going through this, you will be able to easily create JAR files.


    The basic JAR command syntax is as follows:

    jar cf myjar input-file(s)

    Let me explain the command;

    - c option indicates that I want to create a JAR file
    - f option indicates that I want the output to go to a file rather than to stdout
    - myjar is the name that I want the JAR file to have. It can be ...
    Categories
    Java SE
Page 31 of 48 FirstFirst ... 21293031323341 ... LastLast