Recipe 11.4 Creating a Servlet


11.4.1 Problem

You want to use Eclipse to develop servlets.

11.4.2 Solution

Develop the servlet's Java code using Eclipse, and add servlet.jar or servlet-api.jar (depending on your version of Tomcat) as external .jar files. After compiling the servlet, copy it over to your Tomcat installation.

11.4.3 Discussion

JSP files such as that developed in the previous recipe were introduced to make online Java programming easier. JSPs actually are compiled into servlet code, which is pure Java code, with no HTML mixed in. Developing servlets in Eclipse is like developing other Java code: Eclipse can detect problems even before you compile.

You can see the Java code for a servlet in Example 11-2. Servlet code such as this extends the HttpServlet class, and you have to include a .jar file in the build path to get that support. In Tomcat 4.x, that .jar file is servlet.jar ; in Tomcat 5.x, it's servlet-api.jar .

Example 11-2. A sample servlet
 package org.cookbook.ch11; import*; import javax.servlet.*; import javax.servlet.http.*; public class ServletClass 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("Servlet Sample");         out.println("</TITLE>");         out.println("</HEAD>");         out.println("<BODY>");         out.println("<H1>");         out.println("Servlet Sample");         out.println("</H1>");         out.println("This servlet is functional.");         out.println("</BODY>");         out.println("</HTML>");     } } 

In this example, the code displays the text:

 This servlet is functional. 

To make this happen, create a new project, and call it Servlet . Create a new class in this project, ServletClass , in the package org.cookbook.ch11 , and add the code from Example 11-2 to it.

To satisfy the imports, include servlet-api.jar , which comes with Tomcat 5.x ( servlet.jar if you're using Tomcat 4.x) in the build path. You can find servlet-api.jar at jakarta-tomcat-5.0.19\common\lib\servlet-api.jar ; right-click the Ch11_02 project in the Package Explorer, select Properties Java Build Path Add External JARs, and add servlet-api.jar to the build path.

When you've added servlet-api.jar to the build path, you should be able to build the servlet's .class file. Select Project Build Project, creating ServletClass.class .

11.4.4 See Also

Recipe 11.5 on installing a servlet in Tomcat; Chapter 2 in Java Servlet Programming (O'Reilly); Chapter 2 in JavaServer Pages (O'Reilly); Chapter 1 in the Java Servlet and JSP Cookbook (O'Reilly).

Eclipse Cookbook
Inside XML (Inside (New Riders))
ISBN: 596007108
EAN: 2147483647
Year: 2006
Pages: 232
Authors: Steve Holzner

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: