Developing WEB applications with Tomcat and Eclipse

Source: Internet
Author: User
Tags configuration settings apache tomcat ibm developerworks

Developing WEB applications with Tomcat and Eclipse

Apache Tomcat & Eclipse Platform is an outstanding WEB development platform

If you integrate Eclipse directly with Apache Tomcat, it will be easier to use it as a WEB development platform. In this article, learn how to set up Eclipse and Apache Tomcat V5.5 to develop the classic Hello world servlet.

4 Reviews:

Nathan A. Good, writer and software engineer, Freelance Developer

May 25, 2007

    • Content

edited Note : This article is a follow-up to the article "using Eclipse as a development environment for Jakarta Tomcat" published in June 2004 by Geoffrey Duck.

Required Components

The Eclipse V3.2 Callisto integrated Development Environment (IDE) includes tools for WEB development and integration with servers. So, in addition to the Software Development Toolkit (SDK), just install Eclipse and Apache Tomcat. The components and versions used in this article are as follows:

Table 1. Components and versions to use
Components The version used in this article
Eclipse Platform 3.2.2
Java EE 5 SDK 1.5.0_09
Apache Tomcat 5.5.20

The Eclipse IDE is an open source IDE that serves as the development environment for Java™server Pages (JSP) and Java files. If you install both the Web and Java 2 platform,enterprise Edition (EE) development tools, Eclipse will be great for creating HTML, JSPs, and Servlets.

The Java SDK is required to run the servlet. Finally, Apache Tomcat is also used in the formal reference implementation of Java Servlet and Java Server Page, an open source Web and Servlet container.

Installation

The installation of Eclipse and Tomcat is very similar-you need to unzip the archive file to a folder on your local hard disk. If you want to uninstall any of them, simply delete the folder that contains the extracted files.

Install Eclipse

To install the Eclipse IDE, unzip the compressed file (. zip or. tar.gz) downloaded from the Eclipse Web site, and then put the compressed folder in the appropriate location. I choose the location of the installation based on the conventions of the operating system you are using. If you install Eclipse on the Macintosh, I'll place the Eclipse folder inside the Applications folder. If you are using microsoft™windows™, I will put the unpacked folder into the C:\Program file directory.

Once you've downloaded and installed Eclipse, you're ready to start it. Before you start creating a Web project, you need to install the Java EE and Web development plugins. Fortunately, these plugins are easy to install with the Callisto Discovery site.

Install Web Tools

After the Eclipse IDE is working, select Help > Software Updates > Find and Install. This option allows you to download and install Web tools without having to go to the Web site.

Select Search for New Features to Install, as shown below, and then click Next.

Figure 1. Install/Update window

Select Callisto Discovery Site, as shown below, and then click Next.

Figure 2. Select Callisto Discovery Site

Under Callisto Discovery Site , select the Web and Java EE development check box. The wizard warns you that a dependency is missing. When you click Select Required, the warning disappears. Some or all of the components in the graphical Editors and frameworks and Models and Model development categories are selected (shown in 3), which are required to install the WEB and Java EE plug-ins.

Figure 3. Select the features to install

Click Next to continue, and the certificate protocol for each of the selected attributes will also appear. If you agree to these certificates, select Accept to continue the wizard's follow-up process. When the IDE finishes installing the plug-in, you will be asked to restart Eclipse so that the changes will take effect. Be sure to restart the IDE before continuing.

When the IDE is restarted, the new features required to begin developing a dynamic WEB project are ready to be completed.

Installing Tomcat

To install Apache Tomcat, unzip the downloaded archive and put it in the directory, and I put it in my C:\apps directory for later search. This is enough for the time being to start Tomcat using the Eclipse shown later.

Back to top of page

To create a new WEB project

From the Eclipse IDE, choose File > New > Project to view the Projects Wizard. In the Wizards box, enter Web , and the New Project window Filters The wizard to show only those items that match. This approach makes it easy to find wizards without having to traverse each item.

Select Dynamic Web Projectfrom the list. We're going to use the standard WEB project Wizard to build a static HTML Web project. Although you can also technically use Tomcat to run a static Web site, Web servers, such as Apache Web server, are more suitable for static content. WEB servers that are designed to serve static pages have less overhead and are specifically tuned for this purpose.

Target the run-the-Library

Under Target runtime , you can see <NONE>, 4, as you have not yet created the runtime for Apache Tomcat, so you need to click New to open new Target Runti Me Wizard. Select Apache Tomcat V5.5 from the Apache folder, shown in 5, and click Next.

Figure 4. Create a new dynamic Web project Figure 5. Create a new server runtime

You can now name this runtime, and of course preserve the default name of Apache Tomcat V5.5. Click Browse to locate the basic folder for Apache Tomcat configuration (My is C:\apps\apache-tomcat-5.5.20, 6). For no particular reason, I recommend that you do not change, leaving the Ibm®java Runtime Environment (JRE) version as the default JRE for the workbench. After entering a valid directory, click Finish to create the runtime configuration.

Figure 6. Define the server location runtime configuration

If you find yourself needing to change your runtime configuration in the future, you can do this by choosing Window > Preferences . In the Filter box, type the Runtime list to display the installed run-Library configuration settings under the Server category. Select your Apache Tomcat runtime and click Edit to change the name, directory location, or JRE version.

You also need to choose whether you want to convert to the Java EE perspective. I always choose Yes, because this perspective contains some very useful views, such as the Servers view, that can help you to start and stop Tomcat in the future. You can open the Servers view by selecting Window > Show view.

Add Server

The Servers view does not currently contain any content. This view is for applications and WEB servers, which let you control them and monitor their status. To control tomcat directly from the IDE, you can add a reference to Tomcat by selecting New > Server from the context sensitive menu. The New Server Wizard, shown in Figure 7, will default to the Apache Tomcat V5.5 runtime that you just created.

Figure 7. Define a new server

Click Next. The system asks if you want to add items to the server. Select the project that you created in the previous steps, and then click Add.

Figure 8. Add a project to the server

Click Finish to close the wizard. The Servers view now contains the Tomcat V5.5 Server, and it also contains the project.

Figure 9. The Tomcat server in the Servers view creates a sample page

The easiest way to test the new dynamic Web project and the Tomcat server integration is to create a simple JSP and deploy it to Tomcat. You can create a new JSP file by selecting file > New > Other, or you can use context sensitive menus by right-clicking the project name in Project Explorer view and choosing New > JS P.

Make sure that the parent directory is webcontent and name the file index.jsp. Click Finish to create the page using the default template. If you click Next, you can also select the template you want to use for the new page (such as HTML or XHTML). For this test, you can leave the default options.

The page code shown below shows a simple date, which indicates that the code works correctly.

Listing 1. Index.jsp's Content
<%@ page language= "java" contenttype= "text/html; Charset=iso-8859-1 "    pageencoding=" Iso-8859-1 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en"     "Http://www.w3.org/TR/html4/loose.dtd" >
Deploy this page

After the test page is complete, you can use Eclipse to redeploy it to Apache Tomcat. In the Servers view, notice that the state has been set to republish. This is because many changes have occurred since the project was last deployed to the server. Right-click the server and select Publishfrom the menu. Once the project is deployed to this server, the status becomes Synchronized.

Having a problem?

Sometimes things go wrong. To see the errors generated from Tomcat, you can use the Console view. The Console captures the output from the server that you set up in the Servers view.

After the project is successfully deployed and the server starts successfully, you can open the browser and navigate to http://localhost:8080/[projectname]/(where [ProjectName] is the name of the dynamic Web project you created). The new Web page that you created appears, and the date should be the current date. If you refresh the page, the date will change as well.

Figure 10. index.jsp output in the browser Hello World servlet

You can add Java Servlets to your project and deploy it. To quickly create a servlet for testing purposes, you can use the servlet Wizard by right-clicking the dynamic WEB project in Project Explorer View and choosing New >Other. Under Web folders, select the Servlet, and then click Next.

Add the package name com.example.servlets to the Java packages next to the HelloServlet Class name . Click Finish. As an example, you can leave the default values. The Eclipse IDE generates a servlet class. To see this test servlet in the app, you can doGet() add a line of code to the method to print the message to the Responsewriter. The servlet class should resemble this as shown below.

Listing 2. Helloservlet.java's Content
Package Com.example.servlets;import Java.io.ioexception;import Javax.servlet.servletexception;import Javax.servlet.http.httpservletrequest;import javax.servlet.http.httpservletresponse;/** * servlet implementation Class for Servlet:helloservlet * * */public class HelloServlet extends Javax.servlet.http.HttpServlet implements Java X.servlet.servlet {/* (non-java-doc) * @see javax.servlet.http.httpservlet#httpservlet () */public HelloServlet () {Supe   R ();} /* (non-java-doc) * @see Javax.servlet.http.httpservlet#doget (httpservletrequest request, HttpServletResponse respons e) */protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, Ioexcepti on {response.getwriter (). Write ("Hello, world!");}/* (non-java-doc) * @see javax.servlet.http.httpservlet#dopost (httpservletrequest request, HttpServletResponse Res ponse) */protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, Ioex ception {//TODO auto-generated method stub}}

After you add this servlet, the Eclipse IDE prompts you to change the project. Look again at the Servers view; Restart now appears in the Status column next to the TOMCAT server. To restart this server, right-click the server in the Servers view and select Restart > Start. Open the browser and navigate to Http://localhost:8080/[projectname]/helloservlet (where [ProjectName] is the name of the dynamic Web project). The text "Hello, world!" will appear in the browser window. You can add dates, add similar code in the index.jsp file to the doGet() method, and redeploy the servlet using the Eclipse IDE.

Back to top of page

Conclusion

You can start, stop, and deploy to the server without leaving the IDE, which is a very efficient way to develop JSPs and Servlets. The combination of Eclipse and Apache Tomcat provides a simple integration environment that allows you to accelerate your development.

Reference Learning
    • You can refer to this article on the DeveloperWorks Global site   English original  .
    • This article is a follow-up to the article "using Eclipse as a development environment for Jakarta Tomcat" published by Geoffrey Duck in June 2004.
    • Get more information about  java servlet technology   to start building powerful dynamic WEB applications.
    • Visit Sun Microsystems for information about  javaserver Pages technology   to build JSPs for your dynamic Web site.
    • See  eclipse Web Tools Platform (WTP) Project For more information about the plug-ins needed to build WEB applications using Eclipse.
    • See the Eclipse recommended reading list.
    • Browse all eclipse  articles   and   tutorials on developerWorks. New to
    • Eclipse should first see   now start learning Eclipse.
    • Expand your Eclipse skills by viewing the  eclipse project resources for IBM DeveloperWorks  .
    • to hear interesting interviews and discussions for software developers, refer to  developerworks podcasts.
    • for an introduction to eclipse platform, see "Getting Started with Eclipse platform."
    • keep an eye on developerWorks   technical events and webcasts.
    • Check out recent conferences, exhibitions, webcasts, and other   events for IBM Open source developers that will be held globally.
    • Access DeveloperWorks's   open source software technology zone for extensive how-to information, tools, and project updates to help you develop with open source technology and use it with IBM products.
Access to products and technologies
    • Download Eclipse from the project's official Web site, which is powerful (and free ) and can be used in Java, JSP, and more.
    • Download Apache Tomcat and use it to run the Web application.
    • Download the Java EE 5 SDK from Sun Microsystems.
    • Refer to the latest Eclipse technology downloads on IBM Alphaworks.
    • Download an evaluation version of the IBM product to actually experience these application development tools and middleware products from db2®, lotus®, Rational®, Tivoli®, and WebSphere®.
    • Use IBM trial software to improve your next open source development project, which can be downloaded or obtained via DVD.
Discuss
    • The Eclipse Platform newsgroup should be the first stop to discuss issues related to eclipse (selecting it will launch the default Usenet reader application and open eclipse.platform).
    • Eclipse newsgroups contain many resources for users who are interested in using and extending Eclipse.
    • Join the DeveloperWorks community by participating in the DeveloperWorks blog.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.