Writing Web-based Applications in Eclipse
Laurie Williams, Ben Smith and Sarah Smith [Contact Authors] |
| 1.0 | What is a web-based application? |
| 2.0 | Apache Tomcat |
| 3.0 | The Sysdeo Plugin |
| 4.0 | MySQL |
| 5.0 | Resources |
A web-based application, or web application is is an application that's "accessed with a web browser over a network such as the Internet or an intranet" (Wikipedia). Most webapps are deployed using a web application framework, and frequently entail the use of a database engine. This tutorial focuses on the Java application framework within Eclipse. Using the Java SDK, Tomcat, MySQL and the Sysdeo Plugin, we can deploy an application which:
This tutorial will detail the basic steps to getting the software packages mentioned installed and configured on your home machine. |
Apache Tomcat (download) is a program which acts as a container for Java Server pages and provides for their cooperation with a webserver. By following the download link above, you can select an installer or distribution package appropriate for your OS. IMPORTANT: Before installing Apache Tomcat on your machine, please ensure that you have the JDK installed. The JDK is different than the JRE; the JDK provides the executables required to compile Java applications from source, whereas the JRE only contains the files needed to run Java bytecode. 2.1 Running Tomcat/Starting the Catalina Service
2.2 Setting up Administrator Access You will need to have administrator access to the server. This is either accomplished through a Setup Wizard or by editing the XML configuration files directly. Be sure to note any administrator login pair you create for the server if you are using a wizard. If you need to add administrator access manually, complete the following steps:
2.3 Configuration Manager Access Either when you are installing Tomcat (through a wizard) or by configuring it manually, you need to add manager access for the Sysdeo plugin. If you are installing via a wizard, just be sure to select the option to create a manager account and remember the username/password combination you create. For adding a new manager user, execute the following steps if you have admin access:
2.4 Using the Tomcat Manager After configuring a user with manager status for the deployment of applications, you can access the manager application to deploy applications if you wish. If you are trying to use the Sysdeo plugin, skip this step. To configure an application manually:
2.5 WINDOWS USERS READ THIS For some indeterminate reason, Tomcat cannot compile JSPs on a Windows machine without a key JAR file. Before attempting to run your first Tomcat application, copy The rest of this tutorial focuses on deploying Tomcat applications from within the Sysdeo plugin (below). For more information on how to configure Tomcat, please refer to the online documentation. |
The Sysdeo Plugin is an Eclipse plugin that runs Apache Tomcat from within a user's workspace. We use this plugin because it allows error messages created by the JSPs to be printed to the console (as opposed to a log file), because it automatically deploys projects to the local Tomcat server and just generally facilitates integration of Eclipse and Tomcat. The Sysdeo Plugin will have to be manually installed. For more information on manually installing plugins, please refer to the Installing Eclipse Plugins tutorial. You will know you have the plugin correctly loaded into the Eclipse Environment when the Tomcat buttons appear next to the run button. They look like this: ![]() ![]()
IMPORTANT: Though it may be tempting, DO NOT click start button until you have properly configured the Sysdeo plugin. Also do not attempt to start Tomcat from within Eclipse while the Catalina service is already running. Check the status of your Catalina service and then proceed.
3.1 Configuring the Sysdeo Plugin to work with the Eclipse IDE ![]() Figure 3.1 After the Sysdeo Plugin is installed, you will need to initialize it by telling it where Tomcat is, etc, by taking the following steps:
The Sysdeo Tomcat plugin is now installed, however your project is not deployed yet. To create and deploy your first project, see the next section. DO NOT start the Tomcat plugin yet!
3.2 Converting your Project to a Web Application ![]() Figure 3.2a There are several different ways you could set up a project to deploy it as a web application, but the following is the most straightforward and easiest. There are certain folders and files that you will need inside the project for it to function correctly as a webapp, especially for your JSPs to have access to any Java classes you may design on your own. You will start with a structure similar to that in Figure 3.2a. To prepare your project's structure for deployment:
![]() Figure 3.2b 3.3 Deploying a Tomcat Project ![]() Figure 3.3a Now that you have Tomcat and Sysdeo installed, you can easily configure web applications from within Eclipse. Assuming you have a project created, Sysdeo inserts the proper configuration statements into the XML of the configuration file and the application is deployed. To deploy your application on the local Tomcat machine:
![]() Figure 3.3b For more information on JSPs and Servlets, check out the The JSP Module on Open Seminar. 3.4 Debugging Your Webapplication Any time that Tomcat is running using the Sysdeo plugin and your application is running through Eclipse, you can debug your application just as you would any other Java Application. The sourecode of the classes you create can be marked with a breakpoint, and monitored in depth from the Debug Perspective. For more information on loading the Debug Perspective, see the Eclipse Tutorial, Section 5. |
4.1 Installing MySQL The MySQL database server is available for download for free from MySQL AB, the program's provider. The entire list of package downloads is available here. In order to begin creating databases and executing SQL queries, you will need to provide yourself root access to the MySQL server. You will want to create a user with minimal privilages (insert, update, select, delete) priviledges on the database and/or tables that your web application will be using. This is to reduce the chance of getting hacked. The Reference Manual, Chapter 2 has more information regarding platform-specific installation and configuration options. Windows users be sure to start the service for the MySQL daemon before attempting to configure it. 4.2 Setting up Databases with MySQL You will not want to hardcode the SQL queries to create a database into your Java programs; the database should be predetermined and hardcoded, to prevent database hacks such as a SQL Injection (Wikipedia). To create a database on your server, after it is configured and you have root access, see the MySQL tutorial on database use. You may also want to install MySQL Administrator, a GUI configuration tool for your server. 4.3 Installing the MySQL Connector The MySQL connector (download) is a Java library that allows a set of classes to quickly execute and manage MySQL queries. To use it with your project, follow these steps:
4.4 Using the MySQL Connector The MySQL Connector library creates a huge set of Java classes for everything in the SQL environment. For more information on using the MySQL connector in depth, please see The Java Tutorial on this subject. The main classes that we are concerned with for rudimentary input and output on the database are provided with the Java libraries in the SQL package:
Since the MySQL Connector is meant to be extremely flexible and extensible, understanding each class can be daunting. SQLConnection.java contains methods which perform the tasks of creating and working on a JDBC connection. We have also included the following example, which when run will print the first user listed in the user's table of your MySQL server. This example makes use of the SQLConnection.java class.
import java.sql.*;
try
{
SQLConnection cn = new SQLConnection();
Statement stmt = cn.getStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM `user`;");
if (rs.next())
System.out.println(rs.getString("User"));
cn.closeConn();
}
catch (Exception E)
{
System.out.println(E.toString());
}
|