Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Wednesday, December 9, 2009

Tomcat with Eclipse

This is a short tutorial to learn how to run Apache Tomcat server using Eclipse. Lets do it step by step.

  • Install Apache Tomcat 6.0 from here.
  • Unzip the zip file onto your local machine.
  • Now to run the Tomcat server, you need to go to command prompt. So type cmd.exe in Run. Am using the Windows platform. In the command prompt, go to the Apache folder/bin folder
  • Type in SET JAVA_HOME= The location where JDK is installed. In my case, C:\jdk1.6.0.
  • Now run startup.bat
  • This is the way to run the server for a single instance. The best way to avoid typing this command everytime you need to start server, is to modify startup.bat and shutdown.bat. So edit these 2 files by any editor like Notepad, EditPad or Wordpad and include the command SET JAVA_HOME=C:\jdk1.6.0
  • When you run startup.bat, you see another window of command prompt opening, detailing the initializtion and starting of the server. (For instance, typically the message ends with the line - INFO: Server startup in xxxx ms, where xxxx = the time taken to startup like 4318ms or 5124ms... anything).
  • To check if the server has started, go to your web browser and type in http://localhost:8080/ in the address bar. You should see a congratulations message. On the left panel of the page you can manage the server using administration tools.
  • In order to manage the server, you need to have the managing rights for access. So go the installed directory of Apache tomcat/conf folder. Edit the tomcat-users.xml file and add the following command:  
  • So far we learnt how to install the tomcat server on the local machine. Lets now see how to run it using Eclipse. There are many plugins for Eclipse. The most preferred one is the Sysdeo tomcat plugin. Download it here.
  • After you download the zip file of the plugin (latest version is the 3.2.1), unzip it and cut/paste in the "plugins" folder of Eclipse's installed location. 
  • Now start Eclipse.

  • Go to the Window menu and click Preferences.

  • In the preferences, you need to make changes for Tomcat. So choose the appropriate Tomcat version you're using. Also specify the Tomcat home i.e the exact location of where Tomcat is installed. Apply the settings.

  • So this is how you can start and stop Tomcat server using Eclipse. See the following change on the menu bar of Eclipse.

Stumble Upon Toolbar

Wednesday, November 25, 2009

Key Java Concepts IV - Overridden Methods Basics

Today, we'll learn more about overridden methods. We can override a method when a subclass inherits a method from its superclass. The main advantage of overriding is that we can define the behavior that's specific to a particular class. We need to observe certain important rules while overriding methods.

  • The overriding method needs to have the exact argument list as the overridden method. By not doing so, we could get an overloaded method instead. 
  • The overridden method should have the same return type as the overridden method.

  • "final" methods cannot be overridden.

  • The access level of overriding method CANNOT be more restrictive than the overridden method.

  • The access level CAN be lesser restrictive than its overridden method.
  • We cannot override "static" methods.
  • The overriding method CAN throw any number of unchecked runtime exceptions, even if it isn't declared in the overridden method.
  • The overriding method CANNOT throw the checked exceptions which haven't been declared in the overridden method.
  • However, the overriding method CAN throw lesser or narrower checked exceptions than those declared in the overridden method.
  • A subclass in a different package can override methods marked public or protected. But make sure the methods are not marked final or static.

  • The overriding method CANNOT have a more restrictive access modifier than the overridden method. i.e you cannot override a public method and mark it protected.
  • The overriding method must not declare a exception it will never throw.

  • A method can be overridden only if it can be inherited.
Illustration:

public class Car {
         public Tyre() {
                  System.out.println("Common Car Tyre");
        }
}

public class Camaro extends Car {
        public Tyre() {
                System.out.println("Special Camaro tyres");
       }

}


Stumble Upon Toolbar

Thursday, November 19, 2009

Key Java Concepts II - Constructors

This post will focus on the concepts surrounding Constructors in Java. So, here we go...

  • Constructors MUST have the same name as class.
  • A constructor is invoked when an object is created.
  • Constructors don't have a return type.
  • Interfaces never have constructors.
  • Constructors cannot be inherited, thus they cannot be overridden.
  • Constructors can use any access modifier.
  • The compiler will create a default constructor, even if you don't create one yourself.
  • Every class has a constructor. Even an abstract class has one.
  • The default constructor is a no-arg constructor with a call to super().
  • The constructor can be directly invoked using another constructor only i.e using a call to super() or this().
  • Calls to this() or super() cannot be in the same constructor. Either one can exist, not both.
  • this() may appear as only the first statement in a constructor.
  • Constructors can call other constructors and they can call constructors again, however it's advisable to use a call to super() as early as possible to avoid the explosion of stack.
  • The call to super() can be a no-arg call or may have arguments passed to the super constructor.
  •  Its perfectly legal to have methods and constructor with the same name and in the same class. However it's not considered to be a good programming style.
 Illustration:

class Foo {
    Foo() {} // this is the constructor.
}

In this case, when the compiler compiles it, it generates a default constructor. So, for the compiler, the above code will look like:

class Foo {
    Foo {
         super();
    }
}



Stumble Upon Toolbar

Key Java Concepts I - Interfaces

    This is a part of a series of key important Java concepts to remember. Much of the fundamental points are based from a variety of books, forums, websites etc. The objective is to make these fundas handy at any moment. This post will cover the concept of Interfaces.
    •  Interfaces are like 100-percent abstract classes.
    • Interfaces methods are by default public and abstract - however the explicit declaration is optional.  
    • Interfaces can be implemented by any class.
    • Interfaces can have constants.
    • Interface constants are implicitly public, static and final.
    • An interface can have only abstract methods - no concrete methods are allowed.
    • Interfaces cannot extend a class or implement any other interface.
    • However, Interfaces can extend as many interfaces as possible.
    • As you know, there is no multiple inheritance allowed in Java i.e a class cannot extend more than one class BUT a class can implement multiple interfaces.
    • The class implementing an interface can itself be abstract.
    • An abstract implementing class need not have to implement the interface methods, however the non-abstract (concrete) class MUST implement the methods if it implements an interface.
    Illustration:

    public abstract Sizeable{  // use of public modifier is optional, if you don't want default access
        public abstract  toDo();  // Note the abstract methods always end with a ; instead of {}
        public abstract doThis();
    }

    class Baloon implements Sizeable {}

    public class Tyre mplements Sizeable {}


      Stumble Upon Toolbar
      Related Posts with Thumbnails