Tuesday, November 20, 2012

How to prepare your Eclipse IDE for web development

Objective

Once you're going to start developing some java-based web-applications or just static web-sites you need to perform some simple steps to configure your development environment. Basically you need three things to start developing:
  1. Eclise IDE (J2EE edition)
  2. Tomcat server (Servlet container and servlet specification implementation)
  3. JDK to support basic java features along with runtime environment
Lets go step-by-step here. At the moment of this post publishing the latest version of Eclipse IDE was Juno release.  Download it from here so that you've got the version suitable for your operating system. I'm using Windows 7 64bit so all further statements will be related to it. 
That will be great to create some sandbox so that you can use it as isolated disk space to have some practice. After you're pretty much familiar with the configuration you can repeat the steps with your own paths. Say, create the following folder where all the magic is going to happen: c:/sandbox. Create Projects subfolder to keep Eclipse project there.

Configure your Eclipse and Tomcat instances

Extract the eclipse archive to c:/sandbox. So you now have your IDE under c:/sandbox/Eclipse. Start it up (lets not talk about performance tuning of your IDE - this is not our topic here). Set up the workspace to Now you have IDE working. Unfortunately that's not enough to start coding. Close welcome screen.
You won't be able to code a piece of java code until you have JDK installed. Follow this link to download J2SE 6 for your operating system (make sure you're registered as oracle user as you will have to input your oracle credentials).

Install the distribution to C:\sandbox\JDK_SE6. Switch to Eclipse.
  • Go to Window\Preferences
  • In the preferences tree go to Java\Installed JREs
  • Click [Add] button. Choose Standard VM in new dialog and click Next
  • Specify folder C:\sandbox\JDK_SE6 for JRE home. Once you do that all other fields are pre-filled automatically
  • Now you may remove all other VMs from the list if they are.
  • Click OK to close preferences dialog
You now can write and execute Java code. However we're going to create web application, aren't we? Hence we need to install servlet implementation. That's also good to have a web server. Fortunately all those stuff is combined under Apache Tomcat server. Lets use the latest version at the moment. You may take it from here. Unpack the content of the archive to our sandbox so that we have Apache Tomcat server under C:\sandbox\apache-tomcat-7.0.32.

Lets bind eclipse to that apache instance.
  • Go to Window\Preferences again. In preferences tree go to Server\Runtime Environments.
  • Click [Add...] button
  • In "New Server Runtime Environment" dialog choose Apache Tomcat v7.0. Check "Create a new local server" cehck-box. Click Next
  • Click [Browse...] button and choose the folder C:\sandbox\apache-tomcat-7.0.32
  • Under JRE you may choose either default JRE of workbench or JDK_SE6. They are all the same. However lets choose the second option to be more defined.
  • Click Finish and then OK to close preferences window.

Creating a template project.

Lets now create some project to get aware how to create the project each time you want to start trying new create something new.
  • In Eclipse press Ctrl+N so that "New" dialog is opened. 
  • Choose "Dynamic Web Project" and click Next
  • There type some project name (note that the project path is attached automatically to our sandbox)
  • Choose Apache Tomcat v7.0 as target runtime
  • Set dynamic web module version to 3.0
  • Click Next until it gets disabled :)
  • There check the check-box "Generate web.xml deployment descriptor"
  • Finally click [Finish]
So.. Congratulations! You've now got web project configured. To make sure it's working lets do the following.
  • In your Project Explorer expand WebContent folder
  • Right-click the folder and choose New->Html file
  • Name it index.html and click okay
  • Within <body>...</body> enclosure  add piece of html-code like <h1>It really works!</h1>. Save the changes
  • Right click the project root in project explorer. Select "Run as -> Run on Server"
  • You should now see your html page which was deployed to the web-server and now loaded from it
That's all