1、计算机专业文献翻译用 Tomcat 和 Eclipse开发 Web 应用程序Developing Web applications with Tomcat and EclipseRequired componentsThe Eclipse V3.2 Callisto integrated development environment (IDE) includes tools for doing Web development and integrating with servers. So, aside from the software development kit (SDK), you
2、 need only Eclipse and Apache Tomcat installed. The versions featured in this article are listed below.Table 1. Components and versions usedComponentVersion used in this articleEclipse Platform 3.2.2Java EE 5 SDK 1.5.0_09Apache Tomcat 5.5.20The Eclipse IDE is an open source IDE used as the developme
3、nt environment for your JavaServer Pages (JSPs) and Java files. With the Web and Java 2 Platform, Enterprise Edition (J2EE) Development tools installed, Eclipse is great tool for creating HTML, JSPs, and servlets.You need the Java SDK to run servlets on your machine. Finally, Apache Tomcat is an ope
4、n source Web and servlet container, used in the official reference implementations for Java Servlet and Java Server Pages.InstallationInstallation of Eclipse and Tomcat is similar both are extracted from the archive file format into a folder on your hard disk. If you ever want to uninstall either, i
5、ts as simple as deleting the directory in which you extracted the files.Install EclipseTo install the Eclipse IDE, unpack the compressed (.zip or .tar.gz) file you downloaded from Eclipse, then move the unpacked folder into a convenient location. I try to install it in a location that follows the co
6、nventions of whatever operating system Im using. If Im installing Eclipse on a Macintosh, I put the Eclipse folder in the Applications folder. If Im using Microsoft Windows, I put the unpacked folder into the C:Program Files directory.Once youve downloaded and installed Eclipse, start it up. Before
7、you can create a Web project, you need to install the J2EE and Web Development plug-ins. Fortunately, its easy to install the plug-in using the Callisto Discovery Site.Install the Web toolsWith the Eclipse IDE running, select Help Software Updates Find and Install. This option lets you download and
8、install the Web tools without going to a Web site.Select Search for New Features to Install, as shown below, then click Next.Figure 1. Install/Update windowSelect Callisto Discovery Site, as shown below, then click Next.Figure 2. Select the Callisto Discovery SiteUnder Callisto Discovery Site, check
9、 the Web and J2EE Development box. The wizard immediately warns you that youre missing dependencies. Click Select Required, and these warnings should disappear. Some or all of the components in the Graphical Editors and Frameworks and Models and Model Development categories are selected (as shown in
10、 Figure 3) because theyre required for the installation of the Web and J2EE plug-ins.Figure 3. Select features for installationClick Next to continue, and license agreements appear for each feature youve selected. If you agree with the licenses, accept them and follow the wizard through the rest of
11、the process. When the IDE is finished installing the plug-ins, it asks you to restart Eclipse in order for the changes to take effect. Be sure you restart the IDE before continuing.After you restart the IDE, the new features are available to begin developing dynamic Web projects.Install TomcatTo ins
12、tall Apache Tomcat, extract the files from the downloaded archive and place them into a directory. I put them in my C:apps directory to make them easy to locate later. Thats it for now; use Eclipse as shown later to start Tomcat.Create a new Web projectFrom the Eclipse IDE, select File New Project t
13、o view the project wizards. Type Web in the Wizards box, and the New Project window filters the wizards to show the ones that match. This is an easy way to locate the wizard you want instead of going through each item.Choose Dynamic Web Project from the list. You use the Standard Web Project Wizard
14、to make static HTML Web projects. You can technically use Tomcat to run static Web sites, although a Web server such as Apache Web Server may be a better choice for static content. Web servers built specifically for serving up static pages tend to have less overhead and are tuned for that purpose.Ta
15、rget a runtimeUnder Target Runtime, you see , as shown in Figure 4, because you havent created a runtime yet for Apache Tomcat. Click New to open the New Target Runtime Wizard. Select Apache Tomcat V5.5 from the Apache folder, as shown in Figure 5, then click Next.Figure 4. Create a new dynamic Web
16、projectFigure 5. Create a new server runtimeNow you have the opportunity to name your runtime, although the default name Apache Tomcat V5.5 is fine. Click Browse to locate the base folder for your Apache Tomcat configuration (mine is C:appsapache-tomcat-5.5.20, as shown in Figure 6). I recommend lea
17、ving the IBM Java Runtime Environment (JRE) version the same as the workbench default JRE, unless you have a specific reason to change it. Once youve entered a valid directory, click Finish to create the runtime configuration.Figure 6. Define the server locationYoure asked if you want to switch to t
18、he J2EE Perspective. I always answer yes because that perspective includes views like the Servers view, which help to start and stop Tomcat later. You can always open the Servers view by selecting Window Show View.Add the serverIn the Servers view is a view that contains nothing. This view is for ap
19、plication and Web servers; it lets you control them and monitor their status. To control Tomcat directly from the comfort of the IDE, add a reference to Tomcat by selecting New Server from the context-sensitive menu. The New Server wizard, as shown in Figure 7, will default to the Apache Tomcat V5.5
20、 runtime you just created.Figure 7. Define a new serverClick Next. Youre asked whether you want to add projects to the server. Select the project you created in the previous steps, then click Add.Figure 8. Add the projects to the serverClick Finish to close the wizard. Now the Servers view contains
21、the Tomcat V5.5 Server with the project under it.Figure 9. The Tomcat server in the Servers viewCreate a sample pageThe easiest way to test your new dynamic Web project and Tomcat server integration is to create a simple JSP and deploy it to Tomcat. You can create the new JSP file by selecting File
22、New Other, or you can use the context-sensitive menu by right-clicking the project name in the Project Explorer view and selecting New JSP.Make sure the parent folder is WebContent and name the file index.jsp. Click Finish to create the page using the default template. If you click Next, you can cho
23、ose different templates to use (such as HTML or XHTML) for the new page. The defaults are fine for this test.The page code shown below displays a simple date so you can see it working.Listing 1. Contents of index.jsp My TitleTodays date is and this jsp page worked!Deploy the pageNow that the test pa
24、ge is complete, use Eclipse to redeploy it to Apache Tomcat. In the Servers view, notice that State is set to Republish. That is because changes have been made since the project was last deployed to the server. Right-click the server and select Publish from the menu. The state changes to Synchronize
25、d once the project has been deployed to the server.With the project successfully deployed and the server started, open a browser and navigate to http:/localhost:8080/ProjectName/ (where ProjectName is the name of the dynamic Web project you created). The new Web page you created appears, and the dat
26、e should be current. If you refresh the page, the date changes.Figure 10. Output of index.jsp in a browserHello World servletYou can add Java servlets to your project and deploy them. To create a quick servlet you can use for testing purposes, use the Servlet Wizard by right-clicking your dynamic We
27、b project in the Project Explorer view and selecting New Other. Under the Web folder, select Servlet, then click Next.Add the package name com.example.servlets next to Java Package and add HelloServlet next to Class name. Click Finish. This is an example, so the defaults are fine. The Eclipse IDE ge
28、nerates a servlet class for you. To watch the test servlet in action, add a line in the doGet() method to print a message to the ResponseWriter. The servlet class should look like the one shown below.Listing 2. Contents of HelloServlet.javapackage com.example.servlets;import java.io.IOException;impo
29、rt 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 javax.servlet.Servlet /* (non-Java
30、-doc) * see javax.servlet.http.HttpServlet#HttpServlet() */ public HelloServlet() super(); /* (non-Java-doc) * see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException response.getWriter().write(Hello, world!); /* (non-Java-doc) * see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpS
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1