9.6 Using the Sysdeo Tomcat Plug-in

     

The Sysdeo plug-in lets you start and stop Tomcat from inside Eclipse, and we'll take a look at using that plug-in here. You can download this plug-in for free from http://www.sysdeo.com/eclipse/tomcatPlugin.html. After expanding it in the plugins directory, activate it by selecting Window Customize Perspective, opening the Other node, and selecting the Tomcat item, as you see in Figure 9-9.

Figure 9-9. Activating the Tomcat plug-in
figs/ecps_0909.gif

This adds a Tomcat menu to Eclipse (shown in Figure 9-10) and adds three Tomcat buttons to the Eclipse toolbar that you can see under the Navigate menu; these buttons start, stop, and restart Tomcat.

Figure 9-10. New Tomcat buttons in the toolbar
figs/ecps_0910.gif

To connect the Tomcat plug-in to the version of Tomcat you're using, select Window Preferences and click the Tomcat node, as you see in Figure 9-11.

Figure 9-11. Connecting the Tomcat plug-in to Tomcat
figs/ecps_0911.gif

You use this dialog to connect the Tomcat plug-in to Tomcat itself. At this time, the Sysdeo plug-in for Eclipse 2.1.1 is in beta and does not yet work smoothly with the most recent version of Tomcat, Version 4.1.29, so for the next two examples, we'll use an earlier version of Tomcat (you can get earlier versions of Tomcat from the Apache archives at http://archive.apache.org/dist/jakarta/; the archives for all Tomcat 4+ versions are at http://archive.apache.org/dist/jakarta/tomcat-4/archive/). By the time you read this, the Tomcat plug-in should be working with the current version of Tomcat; if not, download and use an earlier version of Tomcat from the Tomcat site if you want to use the plug-in.

In this dialog, you set the Tomcat home location, which is the parent directory of the Tomcat bin directory (equivalent to the CATALINA_HOME environment variable, detailed earlier). To set that location, click the Browse button next to the Tomcat home box, browse to the Tomcat installation directory, and click OK (this also fills in the Configuration file box automatically). Click OK to close the Preferences dialog.

9.6.1 Writing JSP with the Sysdeo Tomcat Plug-in

We'll start using the Tomcat plug-in with a JSP project. To create a new Tomcat project, select File New Project, select Java in the left box of the New Project dialog, select Tomcat Project in the right, and click Next. Give this new project the name Ch09_05 in the New Tomcat Project dialog and click OK. This creates a new Tomcat project with a number of folders already built-in, as you can see in the Package Explorer in Figure 9-12.

Figure 9-12. Creating a new Tomcat project
figs/ecps_0912.gif

To create a new JSP project, right-click the project and select New File, calling the new file Ch09_05.jsp . Enter the JSP you see in Example 9-8. Then simply save the file, and it's ready to runno file copying needed.

Example 9-8. Creating a JSP with the Sysdeo plug-in
 <HTML>     <HEAD>         <TITLE>A Web Page</TITLE>     </HEAD>     <BODY>         <H1>Using JSP</H1>         <% out.println("Using a Tomcat plug-in..."); %>     </BODY> </HTML> 

The Tomcat plug-in lets you edit files locally and sets a Tomcat context for them by editing the Tomcat server.xml file in the Tomcat conf directory. In our example, here's what the plug-in adds to server.xml so Tomcat knows where to find our JSP file:

 <Context path="/Ch09_05" reloadable="true" docBase="D:\eclipse211\eclipse\workspace\Ch09_05" workDir="D:\eclipse211\eclipse\workspace\Ch09_05\work\org\apache\jsp" /> 

This change to server.xml means that Tomcat can find our JSP, so start Tomcat by clicking the Start Tomcat button and navigate to http://localhost:8080/Ch09_05/Ch09_05.jsp in a browser (the URL here includes the project name, Ch09_05 , not just the Ch09 directory as the earlier non-plug-in examples did). You should see the results shown in Figure 9-13.

Figure 9-13. Creating a new JSP using the Sysdeo plug-in
figs/ecps_0913.gif

The Debug perspective will appear when you run Tomcat; when you're done with this example, terminate the Debug session and click the Remove All Terminated Launches button in the Debug view.


That's itthis JSP is running. As you can see, the Tomcat plug-in lets you develop code inside Eclipse, and it lets you start and stop Tomcat inside Eclipse as well.

9.6.2 Writing Servlets with the Sysdeo Tomcat Plug-in

The Sysdeo plug-in also helps write servlets, such as the one in Example 9-9. Up to this point, we've had to set the output directory to store servlets in the Tomcat directories, but the Tomcat plug-in can handle the details automatically.

Example 9-9. Creating a servlet with the Sysdeo plug-in
 import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class Ch09_06 extends HttpServlet {                 public void doGet(HttpServletRequest request,                         HttpServletResponse response)                         throws IOException, ServletException                 {                         response.setContentType("text/html");                         PrintWriter out = response.getWriter( );                         out.println("<HTML>");                         out.println("<HEAD>");                         out.println("<TITLE>");                         out.println("A Web Page");                         out.println("</TITLE>");                         out.println("</HEAD>");                         out.println("<BODY>");                         out.println("<H1>");                         out.println("Using Servlets");                         out.println("</H1>");                         out.println("Using a Tomcat plug-in...");                         out.println("</BODY>");                         out.println("</HTML>");                 } } 

To see this at work, create a new Tomcat project, Ch09_06 . Add a new class, Ch09_06 , to the project, putting that class in the org.eclipsebook.ch09 package. The new class is automatically stored in the WEB-INF/src directory, as you see in the Package Explorer in Figure 9-14.

Figure 9-14. A Servlet project using the Tomcat plug-in
figs/ecps_0914.gif

As before, the Tomcat plug-in edits the Tomcat server.xml document to let Tomcat know where to look for your files:

 <Context path="/Ch09_06" reloadable="true"  docBase="D:\eclipse211\eclipse\workspace\Ch09_06"  workDir="D:\eclipse211\eclipse\workspace\Ch09_06\work\org\apache\jsp" /> 

There's one more thing to do before running this servlet. The Tomcat plug-in doesn't create a local web.xml file to tell Tomcat that Ch09_06.class is a servlet, as we have done in the earlier examples in this chapter. To let Tomcat run anonymous servlet classes that have not been defined in a web.xml file, you can enable the Tomcat "invoker" servlet by removing the comments around the servlet-mapping element in the web.xml file in the Tomcat conf directory:

 <!-- The mapping for the invoker servlet --> <!--   <servlet-mapping>     <servlet-name>invoker</servlet-name>     <url-pattern>/servlet/*</url-pattern>   </servlet-mapping> --> 

When you edit web.xml in the Tomcat conf directory, the servlet is ready to go; after saving Ch09_06.java and building the project, start Tomcat with the Tomcat plug-in's buttons and navigate to http://localhost:8080/Ch09_06/servlet/org.eclipsebook.ch09.Ch09_06 . You should see the results that appear in Figure 9-15. Congratulationsnow you're creating servlets with the Sysdeo Tomcat plug-in.

Figure 9-15. Running a servlet with the Tomcat plug-in
figs/ecps_0915.gif

Alternatively, if you don't want to edit web.xml in the Tomcat conf directory to enable anonymous servlets, you can create a local web.xml for each project you create with the Tomcat plug-in. To do this, right-click the WEB-INF folder in the Package Explorer, select New File, and enter the XML you see in Example 9-10.

Example 9-10. A web.xml file for use with the Sysdeo plug-in
 <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http:// java.sun.com/dtd/web-app_2_3.dtd"> <web-app>   <display-name>Example Applications</display-name>   <servlet>     <servlet-name>Ch09_06</servlet-name>     <servlet-class>org.eclipsebook.ch09.Ch09_06</servlet-class>   </servlet>   <servlet-mapping>     <servlet-name>Ch09_06</servlet-name>     <url-pattern>/org.eclipsebook.ch09.Ch09_06</url-pattern>   </servlet-mapping> </web-app> 

If you create a local web.xml file like this on a project-by-project basis, you don't have to use the word "servlet" in the servlet's URLin this case, you'll be able to navigate to the servlet with the URL http://localhost:8080/Ch09_06/org.eclipsebook.ch09.Ch09_06 .

9.6.3 Debugging Web Projects

The Tomcat plug-in also lets you debug servlet code interactively. For example, say you put a breakpoint in the code for the Ch09_06 servlet we just developed at this line in the code:

 out.println("<HTML>"); out.println("<HEAD>"); out.println("<TITLE>"); out.println("A Web Page"); out.println("</TITLE>"); out.println("</HEAD>"); out.println("<BODY>"); out.println("<H1>"); out.println("Using Servlets"); out.println("</H1>"); out.println("Using a Tomcat plug-in...");  out.println("</BODY>");  out.println("</HTML>"); 

When you start Tomcat and navigate to this servlet in a browser, execution will halt when the breakpoint is reached, and the Eclipse debugger will come up, as you see in Figure 9-16. You're free to single-step through the servlet's code at this point and debug what's going on.

Figure 9-16. Debugging a servlet interactively
figs/ecps_0916.gif

You can also debug JSPs, but you have to work with the servlet code that the JSP is translated into. For example, your JSP Ch09_04.jsp in the webapps/Ch09 directory is translated into the servlet code Ch09_04_jsp.java (this name can vary by Tomcat version) in the work/localhost/ch09 directory before it's run (in the most recent versions of Tomcat, that's become the work/Standalone/localhost/ch09 directory). The servlet file is the file that you actually debug.



Eclipse
Eclipse
ISBN: 0596006411
EAN: 2147483647
Year: 2006
Pages: 114
Authors: Steve Holzner

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net