36
Tutorial on Integrating Eclipse and Tomcat This tutorial will guide you through the process of installation of Eclipse, Jakarta Tomcat, and an Eclipse Tomcat launcher plug-in that will integrate Eclipse and Tomcat. It will also guide you through a few examples that are designed to make sure the software has been installed correctly. Software Step 1. Install Java 2 Platform, Standard Edition (J2SE) Step 2. Install Eclipse IDE Step 3. Install Jakarta Tomcat Step 4. Install Sysdeo Eclipse Tomcat Launcher plugin Step 5. Configure Eclipse Examples Step 6 Create and execute a J Step 7. Create a new Tomcat Pr Step 8. Create and execute a J Step 9. Create and execute a S STEP 1: Install Java 2 Platform, Standard Edition (J2SE) Click on the "Watch a Viewlet" button in Step 1 of Gel Tutorial to view this step 1. Download the file here .

Tutorial on Integrating Eclipse and Tomcat

Embed Size (px)

Citation preview

Page 1: Tutorial on Integrating Eclipse and Tomcat

Tutorial on Integrating Eclipse and Tomcat

This tutorial will guide you through the process of installation of Eclipse, Jakarta Tomcat, and an Eclipse Tomcat launcher plug-in that will integrate Eclipse and Tomcat.

It will also guide you through a few examples that are designed to make sure the software has been installed correctly.

 

Software

Step 1. Install Java 2 Platform, Standard Edition (J2SE)

Step 2. Install Eclipse IDE

Step 3. Install Jakarta Tomcat

Step 4. Install Sysdeo Eclipse Tomcat Launcher plugin

Step 5. Configure Eclipse

Examples

Step 6  Create and execute a Java Applet

Step 7. Create a new Tomcat Project

Step 8. Create and execute a JSP

Step 9. Create and execute a Servlet

 

 

      STEP 1: Install Java 2 Platform, Standard Edition (J2SE)

Click on the "Watch a Viewlet" button in Step 1 of Gel Tutorial to view this step

1. Download the file here.2. Run the file j2sdk-1_4_2_05-windows-i586-p.exe.3. Install into default directory C:\j2sdk1.4.2_05 and accept default settings.

 

      STEP 2: Install Eclipse IDE1. Download the file here2. Double click eclipse-SDK-3.0-win32.zip3. Extract to C:\eclipse

      STEP 3: Install Jakarta Tomcat

Page 2: Tutorial on Integrating Eclipse and Tomcat

1. Download the file here2. Double click jakarta-tomcat-5.0.19.zip3. Extract to C:\jakarta-tomcat-5.0.19

      STEP 4: Install Install Sysdeo Eclipse Tomcat Launcher plugin

1. Download the file here2. Double click tomcatPluginV3.zip3. Extract zip file to a temporary directory and copy the directory

com.sysdeo.eclipse.tomcat_3.0.0 to C:\eclipse\plugins or directly extract zip file to C:\eclipse\plugins

      STEP 5: Configure Eclipse1. Start Eclipse by clicking eclipse.exe in the dir C:\eclipse, it will pop up a window

(See picture 1),select directory for putting your project and then click ok. 

2. Select Window<Preferences

Page 3: Tutorial on Integrating Eclipse and Tomcat

3. Select the Java option from the left hand tree view. Expand the Java element and select Installed JRE's.

4. Click "Add" and navigate to the JRE directory of the SDK (C:\j2sdk1.4.2_05\jre) .Input JRE name and click "OK".

Page 4: Tutorial on Integrating Eclipse and Tomcat

5.Check the check box beside the JRE that you just added from the SDK you installed, as shown below. This sets this JRE as the default JRE to be used by Eclipse. You must do this for the Tomcat plugin to work. The Tomcat plugin requires that an SDK be the default JRE selected in these Eclipse preferences.

Page 5: Tutorial on Integrating Eclipse and Tomcat

6. Set the Tomcat plugin preferences. First, select Tomcat from the left tree menu, then check Version 5.x, then click Browse to select Tomcat home (C:\jakarta-tomcat-5.0.28).

Page 6: Tutorial on Integrating Eclipse and Tomcat

7. Select JVM Settings from the left tree menu, add tools.jar to Classpath.Then click OK.

Page 7: Tutorial on Integrating Eclipse and Tomcat

 

 

 

 

 

 

 

Page 8: Tutorial on Integrating Eclipse and Tomcat

      STEP 6: Create and Execute a Java Applet

1. Select File > New > Project and a NewProject window will pop up, select Java Project, then click Next button.

2. New Java Project window will pop up, input the name of project, here "AppletDemo", then click Next button.

Page 9: Tutorial on Integrating Eclipse and Tomcat

3. Keep it as default and click Finish button.

Page 10: Tutorial on Integrating Eclipse and Tomcat

4. Select AppletDemo (project name) in the workspace and right click on it, then select New > File.

Page 11: Tutorial on Integrating Eclipse and Tomcat

5. In the pop up window New File, input the name of applet, here "SimpleApplet.java", then click Finish button.

Page 12: Tutorial on Integrating Eclipse and Tomcat

6. Add the following code to the file SimpleApplet.java and save it.

import java.awt.*; import java.applet.*;

public class SimpleApplet extends Applet {     public void paint(Graphics g)     {        g.setColor (new Color(0,0,255));       g.drawString("This is a simple applet Demo", 50,50);     }

Page 13: Tutorial on Integrating Eclipse and Tomcat

}

7.Select SimpleApplet.java in the workspace and right click it, then select Run>Java Applet.

8. A AppletViewer window will pop up showing the running result.

Page 14: Tutorial on Integrating Eclipse and Tomcat

9. Applet can also be viewed through a browser, to do this, you need edit a html file that contains the applet tag and applet class name (the applet class should be in the same directory, or else path should be included). Here is a example (in FrontPage editor).

Page 15: Tutorial on Integrating Eclipse and Tomcat

10. Click the html file and the applet will show as below.

Page 16: Tutorial on Integrating Eclipse and Tomcat

      STEP 7: Create a new Tomcat Project

 

1. Select File > New > Project and select a new entry  Tomcat Project , then press Next.

Page 17: Tutorial on Integrating Eclipse and Tomcat

2.Give project a name such as TomcatProject and press Next.

Page 18: Tutorial on Integrating Eclipse and Tomcat

3.Keep it as default and press Finish.

Page 19: Tutorial on Integrating Eclipse and Tomcat

4.It will pop up a window, Click Yes.

5.A new project with a WAR structure is created in the workspace.

Page 20: Tutorial on Integrating Eclipse and Tomcat

      STEP 8: Create and execute a JSP

 

1. Select TomcatProject (created in step 6) in the workspace and right click on it, then select New > File. Give the file name JSPDemo.jsp and click Finish. 

Page 21: Tutorial on Integrating Eclipse and Tomcat

2.Add the following code to the file JSPDemo.jsp and save it.

<html><title>JSP Demo</title><body bgcolor="blue" text="white">

<!-- DIRECTIVES --><%@ page language = "java" %><%@ page import = "java.util.*" %><%@ page contentType = "TEXT/HTML" %>

<!-- SCRIPLETS -->

Page 22: Tutorial on Integrating Eclipse and Tomcat

<H1><% if (Calendar.getInstance().get(Calendar.AM_PM) == Calendar.AM){ %>Good Morning ! <% } else { %>Good Afternoon! <% } %></H1>

<H2> If you're seeing this page via a web browser, it means you've integrated Tomcat and Eclipse successfully. Congratulations!</H2>

<H3> The current time is: <%out.println(new java.util.Date()); %> </H3>

</body></html>

3.Start Tomcat by pressing Start Tomcat button in the toolbar.

4.The log of startup of Tomcat will appear in the Eclipse console view.

Page 23: Tutorial on Integrating Eclipse and Tomcat

5.Open a web browser and go to the URL http://localhost:8080/TomcatProject/JSPDemo.jsp . A page will load and you will get the following figure if followed all the above steps.

Page 24: Tutorial on Integrating Eclipse and Tomcat

 

      STEP 9: Create and execute a Servlet

1. Select File > New > Project. Then New Project window will pop up, select Tomcat Project then click Next button.

2. In the New Tomcat Project window, input the project name, here use "ServletDemo",then click Next button.

Page 25: Tutorial on Integrating Eclipse and Tomcat

3. To make it simple, keep this as default and click Finish button.

Page 26: Tutorial on Integrating Eclipse and Tomcat

4.Select WEB-INF/src in workspace and right click it and select  New > File.

Page 27: Tutorial on Integrating Eclipse and Tomcat

5.In the pop up window New File, input file name "SimpleServlet.java" and click Finish button.

Page 28: Tutorial on Integrating Eclipse and Tomcat

6. Input the following code in to the file SimpleServlet.java and click Save button in toolbar.

import java.io.PrintWriter; //import javax.servlet.*;

public class SimpleServlet extends javax.servlet.http.HttpServlet {

public void doGet(javax.servlet.http.HttpServletRequest request,      javax.servlet.http.HttpServletResponse response)

Page 29: Tutorial on Integrating Eclipse and Tomcat

throws javax.servlet.ServletException, java.io.IOException {

performTask(request, response);

} public void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException { performTask(request, response);

} public void performTask(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) { try { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("This is a Simple Servlet Demo");

} catch(Throwable theException) { //theException.printStackTrace(); } } }

7. Select WEB-INF/ in workspace and right click it and select  New > File.

Page 30: Tutorial on Integrating Eclipse and Tomcat

8. A New File window will pop up and input the file name " web.xml" and click Finish button.

Page 31: Tutorial on Integrating Eclipse and Tomcat

9. Input the following content that in the table

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

<web-app>

<servlet>

Page 32: Tutorial on Integrating Eclipse and Tomcat

<servlet-name>SimpleServlet</servlet-name>

<servlet-class>SimpleServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>SimpleServlet</servlet-name>

<url-pattern>/SimpleServlet</url-pattern>

</servlet-mapping>

</web-app>

into the file web.xml, for this machine it pop up a Notepad window when Finish button clicked in step 7. So copy the content in above table into this Notepad window, then save it.

Page 33: Tutorial on Integrating Eclipse and Tomcat

10. Press the Start Tomcat button in tool bar, after tomcat starts, open a browser and input the URL: http://localhost:8080/ServletDemo/SimpleServlet . The result will show as below:

Page 34: Tutorial on Integrating Eclipse and Tomcat