campusflava

Thursday, May 16, 2013

How to Build a JSP Development Environment Using Eclipse, Tomcat and MySQL

For those who want to develop and test web-based java code, JSP’s and Servlets, the combination of Eclipse, Tomcat and MySQL is a recommendable desktop environment, because all of them are either free or open source and available for downloading on the internet. Eclipse is an integrated java development tool, which can be used for coding and compiling. Tomcat is an open source software for implementation the Java Web Pages. MySQL is an open source database software which can be used to store and retrieve data for the Web Applications. The following instruction covers the basic steps of installation and configuration, especially how to configure them together to make sure they co-operate smoothly.

N.B Visit www.campusflava.com for other updates
 
 Steps
  1. 1
    Download appropriate version of JDK according to your operating system from Oracle. Before downloading, you must select “Accept License Agreement”.
  2. 2
    Once the download is completed, double click the file to install JDK. After the installation is done, You need to create two environment variables (JAVA_HOME and CLASSPATH) in your operating system,so go to Start -> Settings -> Control Panel -> System -> Advanced -> Environment Variables -> System variables -> New.
    • JAVA_HOME points to JDK installation path, which specifies the location of the java run time environment. CLASSPATH points to a list of file paths of directories or jar files, which would be used by applications for searching Java programs and libraries:
      •  %JAVA_HOME%\lib;%JAVA_HOME%\lib\tools.jar
      • Then, add the following path to the PATH environment variable in your operating system:
        • %JAVA_HOME%\bin;%JAVA_HOME%\jre\bin.
  3. 3
    Download appropriate version of Tomcat from http://tomcat.apache.org/download-70.cgi, and follow the steps of “Apache Tomcat Setup Wizard” to install it.
  4. 4
    Create an environment variable, CATALINA_HOME. The value is the installation path of Tomcat you just installed.
    Add the following path to the CLASSPATH environment variable:
    ;%CATALINA_HOME%\common\lib.
    Add the following path to PATH environment variable:
    ;%CATALINA_HOME%\bin.
  5. 5
    Start the Tomcat server from the bin folder of Tomcat installation directory by double clicking the “startup.bat” file. Now open web browser and type URL http://127.0.0.1:8080/ in the navigation bar to test the server. If your browser displays a page just like the following image, it means your installation of Tomcat is successful. To stop the Tomcat server, you can double clicking the “shutdown.bat” file or just press the “Ctrl + c” keys.
  6. 6
    Download appropriate version of Eclipse from http://www.eclipse.org/downloads/packages/eclipse-ide-java-developers/indigosr2. Once you have downloaded the Eclipse archive you will need to decompress the zip file, which will create the unzipped Eclipse folder. You may want to extract the archive to the root of C:\ drive, thus creating the folder “C:\eclipse”, or just moved the extracted eclipse folder to the root of C:\ drive if you extracted it already. Since Eclipse does not have any installer, there will be a file inside the Eclipse folder named eclipse.exe. You can double click on the file to run Eclipse.
  7. 7
    Now you need to install “Sysdeo Tomcat plug-in” for the integration of Tomcat with Eclipse. This can be downloaded from http://www.eclipsetotale.com/tomcatPlugin.html. Unzip this plug-in in your eclipse “plugins” directory. When you restart Eclipse, Tomcat options (start, stop, restart) will appear in the Menu.
  8. 8
    Select a JDK as default JRE for Eclipse in its preference window: Windows -> Preferences -> Java -> Installed JREs. The selected JRE must be a JDK, because Tomcat requires JDK to compile files instead of JRE.
    Configure Tomcat for Eclipse also in its preference window: Windows -> Preferences -> Tomcat.
  9. 9
    By testing a simple JSP program to verify the correctness of previous installations and configurations. Run Eclipse. Select File -> New -> Project and select “Tomcat Project” under “Java”, then press “Next”

    Give a project name:
    Select the “context” and finish it. Now create a new file for this project by selecting File -> New -> File and give it a name like “Test.jsp” for example. Edit this file and save it.
    Then, start tomcat from menu Tomcat -> Start. You will see the starting information in Eclipse console.
    Open a web browser and input the URL (http://127.0.0.1:8080/ProjectName/JSPfileName) in the navigation bar.
  10. 10
    Download MySQL Installer for Windows from http://dev.mysql.com/downloads/
    Run the downloaded msi file, and then follow the steps of MySQL installer wizard to install it.
  11. 11
    After installation finished, you need to visit http://dev.mysql.com/downloads/connector/j/ to download a mysql-connector for establishing the connection between Tomcat and MySQL database. Decompress the zip file and move the “*.jar” file (eg. mysql-connector-java-5.1.18-bin.jar) to the “\common\lib” subfolder under Tomcat installation directory (eg. C:\old\d\Tomcat 5.0\common\lib).
  12. 12
    Test the connection to MySQL database. At first, you need to create a test table in your MySQL database, and insert several rows of data. Then, write a simple JSP program to retrieve the data from the test table and display.

    If you can see the data of test table from a web browser, all done. Congratulations! Now you can start your Servlet or JSP programming.

Tips
  • The standard Eclipse you previously installed does not include WTP(Web Tools Platform). You may install it by the following steps: help -> install new software -> select “The Eclipse Web Tools Platform…” from “Work with” address bar -> check a version of “Web Tools Platform (WTP)” in the multiple-choice list -> Next -> Finish.
  • There are many of plug-ins that could be installed on Eclipse to make your programming more convenient. Lomboz is one of them, and it is open-source. It provides a JSP Editor that can be used to edit HTML, Custom Tags, and JSPs. The editor has a parser based on Jasper from Tomcat, which can handle syntactically incorrect programs and can pre-compile programs to point syntax errors with each save. It also provides Java Servlet preview mode. You can download it from http://forge.ow2.org/projects/lomboz.
  • If you want to develop Servles applications, you need to add the file “servlet-api.jar” to Eclipse lib by the following method: windows -> preferences -> Java -> Installed JREs -> select jdk and “Edit” -> Add External JARs (locate the file “servlet-api.jar” from Tomcat’s common\lib subdirectory) -> Finish.

Warnings
  • For each of the software you need to download the appropriate version for your operating system, XP, 32bit or 64 bit. This instruction based on Window XP, but there is no big difference from other operating systems.

No comments: