Sie sind auf Seite 1von 10

Installing Apache Tomcat 6 and using it with Eclipse to create and run a java servlet 

By Doug Twitchell (See license information at end of document) 
1. Download Tomcat 6 from http://tomcat.apache.org/download‐60.cgi 
2. Download the Windows Service Installer 

 
3. Run the Windows Service Installer  
4. Remember this directory: 

 
 
5. Input an admin  password and REMEMBER it 
6. If you get this warning: 

 
You likely have another Tomcat installation.  Remove it.  If you don’t want to, use this command 
to remove the service so the new service can be installed: 
 
ServiceEnum.exe /deleteService <name> 
 
7. Don’t start the Tomcat Service.  Eclipse will do this later for us. 
8. Ensure that you have the Web Tools Platform plugin installed in eclipse.  
a. Go to File Æ new…  
b. If you have web tools already installed, you should find the web folder as shown below: 

 
 
c. If they are not there, you need to install them.  To install do the following: 
i. Go to Help Æ Software Updates Æ Find and Install…  
ii. Choose “Search for new features to install” 
 
 
iii. From the list of update sites, choose the “Europa Discovery Site” 

 
 
iv. After clicking Finish, it will ask you to choose from some mirrors.  Georgia Tech 
is a good one to choose.  From there choose  “Web and J2EE development”. 
 
 
v. It will say that some other packages are required.  Choose “Select Required“  to 
have eclipse automatically select what is needed. 
 
 
vi. Click Next, accept the license, and continue clicking Next until only Finish is 
available and click it to start the download.   
vii. When the download is finished click Install All to start the installation.   
viii. Confirm that you want to restart eclipse 
 
9. Once you have the Web Tools Platform installed, in eclipse, create a new project.  The Project 
type should be “Dynamic Web Project” which is one of the choices shown in the graphic in step 
7b above. 
 
10. Give the project a name.  We also need to give the project a “Target Runtime.” This is the web 
server that we will be publishing our web application to, which, in our case, is Tomcat 6.0.  Click 
“new..” to create a new target runtime (see screenshot below).  On subsequent projects, you’ll 
be able to choose the target runtime we create in this step from the dropdown box. 
 
 
11. Choose Apache Tomcat v6.0 and indicate that you would like to create a new local server. 

 
12. Use the Browse button to choose the installation directory of Tomcat and click Finish. 
 
 
13. Back at the New Dynamic Web Project dialog, click finish to create the project with everything 
else as default.  You should see the following in your project explorer: 

 
 
14. Right‐click on your web project, choose new, and choose to create a new Servlet: 
 
 
15. Give it a name and hit finish 

 
 
16. Create your servlet with the code below: 
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class for Servlet: HelloWorld
*
*/
public class HelloWorld extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {

//ignore this constant, but keep it in


static final long serialVersionUID = 1L;

/**
* Simple default constructor
*/
public HelloWorld() {
super();
}

/**
* The init method is called when the servlet is first created
* on the web server. You can use this method to connect to databases,
* open files, or other one-time tasks that will be open during the life
* of the servlet
*/
@Override
public void init() throws ServletException {
//this happens once in when the servlet is first loaded
super.init();
}

/*
* This is where most of the action happens. GET is the command used by web browsers
* to access pages on a web server.
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
//First we get a writer we can use to write to the stream that will be
//shown in the user's browser
PrintWriter out = response.getWriter();

//we need to tell the browser what kind of content we are sending
//in this case html
response.setContentType("text/html");

//Another way to tell the browser what we are sending, but


//this is more specific on the kind of html being used \
String docType =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n";
//print it to the browser!
out.println(docType);
//open our html document give it a title, which is in the header
//also open the body, which is what the user actually sees
out.println("<HTML>" +
"<HEAD>" +
"<TITLE>A Page</TITLE>" +
"</HEAD>" +
"<BODY>");
//our web page content!
out.println("Hello World");
//close the body and the html page
out.println("</BODY>" +
"</HTML>");
}

/* Use this method if you want to do something different when an


* http POST is sent to the server. This is often done when using
* html forms.
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
}
 
 
 
17. Run your servlet by choosing Run Æ Run as Æ Run on server  
a. Leave the defaults and click Finish to run it 

 
 
18. Note:  Tomcat must not be running outside of eclipse when you do this.  Eclipse will start the 
server itself.  If the server is running, stop it using the icon in the system tray. 
19. You’ll see your web page displayed in Eclipse’s internal web browser.  To see it in another 
browser, copy the URL from the eclipse browser to the browser of your choice.  You should see 
the same output there. 
20. From now on, as you make changes, when you save those changes, they will be automatically 
updated on the server.  Sometimes, however, the automatic update doesn’t work.  If you don’t 
think it did, go to the server tab at in the bottom part of the eclipse workspace, right‐click on the 
server and choose Restart. 

 
 
21. Done 

 
This work is licensed under the Creative Commons Attribution‐Share Alike 3.0 United States License. To 
view a copy of this license, visit http://creativecommons.org/licenses/by‐sa/3.0/us/ or send a letter to 
Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA. 
 

Das könnte Ihnen auch gefallen