Java Tutorial - Java Script :
Building a Servlet
Now, you are ready to build a servlet in Eclipse. To start, create a new project (an old project can be closed by selecting Project ➪ Close Project). Selecting File ➪ New ➪ Project summons the Project wizard. Notice that a new type of project appears, the Tomcat project as shown in Figure 4.7. A Tomcat project automatically creates the correct directory structure for a Web application. Select the Tomcat Project. On the next screen, enter HelloServlet as the project name, then click next. Accept /HelloServlet as the context name, and click Finish. The package Explorer should now show your new project. If you expand the HelloServlet project, you will see that servlet.jar is included in the project that also includes a WEB-INF subdirectory. With the HelloServlet project root highlighted, right-click it, and select New ➪ Class.Figure 4.6 Eclipse configuration for Tomcat plug-in. Figure 4.7 Eclipse Project wizard.When the New Class wizard starts, use the default package, and then enter HelloServlet as the class name. Set the superclass to javax.servlet.http .HttpServlet. The Browse button can be used to do this as well. Clicking Browse presents the dialog box shown in Figure 4.8. Figure 4.8 Looking up a superclass in the Class wizard.
Entering httpserv and pressing Enter will bring up a list of class names that match the screen. Select the HttpServlet class from the list, uncheck the boxes at the bottom of the dialog box, and click Finish. The editor window should now contain a class called HelloServlet with no code inside. Place the cursor of the editor inside the curly braces, and type doGet. Hold the Control key down, and press the space bar. The editor will fill in the method definition based on the method in the superclass. Change protected to public and change arg0 and arg1 to request and response, respectively.
Also, remove the call to the superclasses doGet, and then edit the doGet() code to look like the following: public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
PrintWriter out = response.getWriter();
out.println(“Hello World”);
out.close();
}
After you type the line: PrintWriter out = response.getWriter();a lightbulb appears on the left margin of the editor. Clicking on the light bulb provides an opportunity for the editor to automatically add the needed import statement at the top of the file as follows:
import java.io.PrintWriter; Save the file (remember that saving also compiles the file), and if there are any errors, correct them. Tomcat can be started by clicking the Start Tomcat button on the toolbar. Starting Tomcat rearranges the windows, showing the Tomcat console at the bottom of the workbench. Test the application by starting a browser and entering the following URL (assuming a standard Tomcat development configuration): http://localhost:8080/HelloServlet/servlet/HelloServlet When you’re finished testing, clicking the Stop Tomcat button on the toolbar stops the Tomcat server. Clicking the button on the left border of the workbench restores the Java perspective.
To export the project to a Web Archive (or .war) file, right click on the project, and select properties. Select the Tomcat node in the navigation pane, and then enter the name of the .war file that will be created. After clicking OK, right-click on the project again and select the Tomcat project, then export to a war file. There are several things that we should note about the Eclipse environment.First is that the plain vanilla environment is very basic and does not support such common tasks as designing a Java GUI or building HTML, XML, or JSP files. The Sysdeo plug-in is just one plug-in that can extend the Eclipse IDE to support J2EE or Web-application programming tasks. Many other plug-ins,both commercial and open source, exist or are being developed for Eclipse. A list of currently available plug-ins can be found at the community plug-ins page at the following URL:
