Thursday, May 29, 2014

How to set up eclipse with Tomcat7 with Ubuntu to do Java Servlet programming

This article will help you to set up eclipse with tomcat to do Java Servlet programming. 


Prerequisite : You should have Java installed in your machine
  1. Install Eclipse and Tomcat 7 by running in a terminal:
    sudo apt-get install eclipse
    sudo apt-get install tomcat7
    Note that tomcat will automatically start at boot. If you want to prevent this run the command:
    sudo update-rc.d tomcat7 disable
  2. Start eclipse and create a new workspace. To install WTP (Web Tools Platform)
    • From the Help menu select Install New Software
    • Next to Work with: open the drop down menu and select the update       site of your Eclipse version. 
    • Select Web, XML, Java EE Development and OSGi Enterprise       Development
    • Click Next and complete the installation
    • Restart eclipse
  3. Before setting up eclipse, we need to create some links & set permissions. To do so run this command:
    cd /usr/share/tomcat7
    sudo ln -s /var/lib/tomcat7/conf conf
    sudo ln -s /etc/tomcat7/policy.d/03catalina.policy conf/catalina.policy
    sudo ln -s /var/log/tomcat7 log
    sudo chmod -R 777 /usr/share/tomcat7/conf
  4. Next, you shall configure WTP to use Tomcat 7:
    • select Windows -> Preferences -> Server -> Runtime Environments
    • Click Add…; 
    • select “Apache Tomcat v7.0″; 
    • enter “/usr/share/tomcat7″ into the “Tomcat installation directory” field;
    • press Ok;
    If you come across any error like “'Cannot create a server using the selected type' or if you are not able to select your server, run the following:
    cd ~/workspace/.metadata/.plugins/org.eclipse.core.runtime/.settings/
    rm org.eclipse.jst.server.tomcat.core.prefs
    rm org.eclipse.wst.server.core.prefs
  5. Now, we are ready to create our first project. To add a Servlet to the project,
      • Right-click on “HelloWorld” in the project explorer,
      •  choose New > Servlet. 
      • Enter the Class name “HelloServlet” and package as “org.chintech” and click Next 
      • You may add any initialization parameters if required. 
      • By default the servlet is mapped to “/HelloServlet” URL pattern. 
      • You may edit this or add a new one or leave it as it is. 
      • Click Finish, to complete.
  6. To edit the new Servlet, from the project Explorer, Open HelloWorld>Java Resources>src>HelloServlet.java and add some flesh to the doGet() or doPost() method
  7. Next, we shall deploy it on the server. To view the available servers, open the Servers view:
  • Go to Window->Show View->Other…;
  • Choose the Servers under the Server category; 
  • This will display the server tab
  1. To add our tomcat server, click on new server wizard; or right-click in the server tab and choose New>Server
  2. To add our tomcat server, click on new server wizard; or right-click in the server tab and choose New>Server
  • choose Apache / Tomcat v7.0 Server and press Next
  • enter “/usr/share/tomcat7″ into the “Tomcat installation directory” field;
  • press Next;
  •  select your project on the left pane under “Available” and press Add> to move it to the right pane under “Configured”;
  • Press “Finish”;
  1. Finally, we are ready to take off. Right-click on the “HelloWorld” project and choose run on server. If everything goes right, you can see your servlet output in the web browser at “http://localhost:8080/HelloWorld/HelloServlet”