Sunteți pe pagina 1din 34

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 Examples
Step 1. Install Java 2 Platform, Standard Edition (J2SE) Step 6  Create and execute a Java Applet
Step 2. Install Eclipse IDE Step 7. Create a new Tomcat Project
Step 3. Install Jakarta Tomcat Step 8. Create and execute a JSP
Step 4. Install Sysdeo Eclipse Tomcat Launcher plugin Step 9. Create and execute a Servlet
Step 5. Configure Eclipse

      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 IDE


1. Download the file here
2. Double click eclipse-SDK-3.0-win32.zip
3. Extract to C:\eclipse

      STEP 3: Install Jakarta Tomcat


1. Download the file here
2. Double click jakarta-tomcat-5.0.19.zip
3. Extract to C:\jakarta-tomcat-5.0.19

      STEP 4: Install Install Sysdeo Eclipse Tomcat Launcher


plugin
1. Download the file here
2. Double click tomcatPluginV3.zip
3. 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 Eclipse


1. 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
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".
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.
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).
7. Select JVM Settings from the left tree menu, add tools.jar to Classpath.Then click
OK.
 

 
      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.
3. Keep it as default and click Finish button.
4. Select AppletDemo (project name) in the workspace and right click on it, then
select New > File.
5. In the pop up window New File, input the name of applet, here "SimpleApplet.java",
then click Finish button.
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);
    }
}

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.


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).
10. Click the html file and the applet will show as below.
      STEP 7: Create a new Tomcat Project
 

1. Select File > New > Project and select a new entry  Tomcat Project , then
press Next.
2.Give project a name such as TomcatProject and press Next.
3.Keep it as default and press Finish.
4.It will pop up a window, Click Yes.

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


      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.
 
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 -->


<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.
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.
 

      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.
 

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


 

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

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

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)
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.
 

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

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>
<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.
 

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:

S-ar putea să vă placă și