|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SYS-CON.TV |
TOP THREE LINKS YOU MUST CLICK ON Eclipse Creating Web Applications with the Eclipse Web Tools Project
Using open source to develop Web applications, EJBs, and Web services
By: Boris Minkin
Nov. 22, 2005 01:00 PM
Creating Servlets and JSPs
Web Tools provides comprehensive wizards and editors for creating servlets and JSPs. We'll start by creating a package called "servlet" in our Eclipse project where all our servlets will reside. Next, we'll launch the wizard for creating a CreateCustomerServlet by selecting File-New-Other, and then Web-Servlet (see Figure 4). On the screen in Figure 4, we can specify the Web Project and package where servlet should reside, and the next screen will look like Figure 5. On this one, we can specify the optional description for the servlet, initialization parameters (that can be retrieved by the servlet in the init() method from its ServletConfig object), and URL mapping. Figure 6 shows the final screen for defining servlet. At this point, we are presented with the capability to specify the interfaces that this servlet will implement (by default, all the servlets implement the javax.servlet.Servlet interface), and the methods that should be automatically generated when the servlet is created. Just click on Finish and the servlet is generated. This also creates the following entry in the web.xml file: Another thing that must be added to web.xml is the resource reference to the datasource that has been configured in DBTest.xml: This XML is necessary for the CommandExecutor datasource lookup code to work properly. In the init() method of ListCustomers-Servlet, we will get the instance of the CommandExecutor (see Listing 8), which will retrieve the data source and cache it for future use to supply database connections. It's a good practice to cache a reference to JNDI data source, because retrieving it is a time-consuming operation. In the doGet() method (see Listing 9), we will execute the database command and forward the list of registered customers to the JSP to display them. In this doGet method, request attribute "customers" is set to the generic ArrayList and then RequestDispatcher object is used to forward a response to customers.jsp (see Listing 10). Using some of the new features of J2SE 5.0 such as generics and enhanced "for" loop improves the JSP code by reducing the amount of Java code in it. Creating and editing JSP using WTP is simple. Select the Web content folder of your Web project, right-click, and select New - Other from the menu. From the dialog that appears, select Web - JSP, then specify the name of the JSP you want to create. WTP also allows you to select an existing template for your JSP to be based on. Three built-in templates are provided based on HTML, XHTML, and XML. In a similar way, we can create other servlets and JSP pages in our application (see the source code), which is easy to import into Eclipse by using its File-Import menu. Figure 7 shows the Web site map of our simple Web application. As you can see, users initially get to index.html page from which they can perform all the other tasks, such as creating customers and orders, and listing existing customers and their orders.
Debugging Application in Eclipse with WPT The server currently running in the debug mode will be automatically selected. After a couple of clicks, the embedded browser page should appear. From now on you can debug the Web application the same way as a regular Java program.
Deploying the Application to Tomcat On the popup window you can check "Export source files" and "Overwrite existing file" if you wish to have your source contained within your WAR file (it may not be a good idea in production) and want to suppress a warning if the file with the same name (perhaps a previously exported version) already exists in the directory and you want to simply overwrite it. Once the WAR file is exported, deploying it to Tomcat can be done through the Tomcat 5.0 administrative console, which in our case is located at http://localhost:8080/manager/html. The console displays currently deployed applications and allows you to deploy new, undeploy, and reload existing applications. Once deployed, we need to make sure that our DBTest.xml file is still there under $TOMCAT\conf\catalina\localhost with all the correct datasource definitions. If it's not, replace it with the correct one that have been saved by you before. Now, our application is ready to run, so point your browser at http://localhost:8080/DBTest/. Figure 9 shows what the list of orders for a registered customer will look like.
Summary YOUR FEEDBACK
LATEST JAVA STORIES & POSTS
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK SPONSORED BY INFRAGISTICS
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||