Developing the First Servlet - cnx.org · PDF fileDeveloping the First Servlet . Creating a...

17
Developing the First Servlet Creating a Servlet Using the NetBeans IDE The following tutorial consists of steps to create a new Java EE 7 project named FirstServlet. Three files are created for the project: index.html, AddServlet.java, and web.xml. index.html is automatically added to the project and the file will be modified. You may notice that NetBeans and the projects are installed on drive N: (a USB flash drive). However, this is only done for portability and instructor testing. It is strongly recommended that you install NetBeans and all other required software development tools on the local C: drive. The configurations are much easier to manage and troubleshoot when all components are installed on the C: drive. Otherwise, you may encounter configuration issues which are difficult to resolve. Also, due to the permissions managed by UAC (User Account Control) in Windows, I recommend that software development tools not be installed in the Program Files directories. It is important to follow the steps very precisely to help reduce the chances for incorrect configurations and errors. Be patient and take your time. Also, repeating the tutorials in the course for practice will reinforce the concepts, enhance understanding, and improve skills. When working through this or any other software development course it is especially important to get and remain organized. Devote time to planning an organization strategy for your project files and folders. Organization is an essential skill for web developers. Also, regularly backup your work. Modern hard drives are reliable but drive and system failures do occur. Note: Some of the early tutorials in this course (INEW 2338) were constructed prior to the release of NetBeans 8.0 and are shown using NetBeans 7.4. All of the NetBeans 7.4 instructions in the course can be performed with NetBeans 8.0. When the tutorials begin using NetBeans 8.0, instructions will be provided to perform the minor project revisions for the upgrade from 7.4 to 8.0. Steps: Create a new project by clicking the New Project button, making the selections, and then click Next. Page 1 of 17

Transcript of Developing the First Servlet - cnx.org · PDF fileDeveloping the First Servlet . Creating a...

Page 1: Developing the First Servlet - cnx.org · PDF fileDeveloping the First Servlet . Creating a Servlet Using the NetBeans IDE . The following tutorial consists of steps to create a new

Developing the First Servlet

Creating a Servlet Using the NetBeans IDE

The following tutorial consists of steps to create a new Java EE 7 project named FirstServlet. Three files are created for the project: index.html, AddServlet.java, and web.xml. index.html is automatically added to the project and the file will be modified. You may notice that NetBeans and the projects are installed on drive N: (a USB flash drive). However, this is only done for portability and instructor testing. It is strongly recommended that you install NetBeans and all other required software development tools on the local C: drive. The configurations are much easier to manage and troubleshoot when all components are installed on the C: drive. Otherwise, you may encounter configuration issues which are difficult to resolve. Also, due to the permissions managed by UAC (User Account Control) in Windows, I recommend that software development tools not be installed in the Program Files directories.

It is important to follow the steps very precisely to help reduce the chances for incorrect configurations and errors. Be patient and take your time. Also, repeating the tutorials in the course for practice will reinforce the concepts, enhance understanding, and improve skills.

When working through this or any other software development course it is especially important to get and remain organized. Devote time to planning an organization strategy for your project files and folders. Organization is an essential skill for web developers. Also, regularly backup your work. Modern hard drives are reliable but drive and system failures do occur.

Note: Some of the early tutorials in this course (INEW 2338) were constructed prior to the release of NetBeans 8.0 and are shown using NetBeans 7.4. All of the NetBeans 7.4 instructions in the course can be performed with NetBeans 8.0. When the tutorials begin using NetBeans 8.0, instructions will be provided to perform the minor project revisions for the upgrade from 7.4 to 8.0.

Steps:

Create a new project by clicking the New Project button, making the selections, and then click Next.

Page 1 of 17

Page 2: Developing the First Servlet - cnx.org · PDF fileDeveloping the First Servlet . Creating a Servlet Using the NetBeans IDE . The following tutorial consists of steps to create a new

Developing the First Servlet

Make selections like those shown (your location/folder will likely be different) and then click Next.

Page 2 of 17

Page 3: Developing the First Servlet - cnx.org · PDF fileDeveloping the First Servlet . Creating a Servlet Using the NetBeans IDE . The following tutorial consists of steps to create a new

Developing the First Servlet

Make selections like those shown and then click Next. Notice the GlassFish Server and Java EE 7 Web items are selected.

The next dialog box presents an opportunity to add frameworks to the application. No need to add a framework to this project. Use of the JSF (JavaServer Faces) framework will be covered in later tutorials. Click Finish.

Page 3 of 17

Page 4: Developing the First Servlet - cnx.org · PDF fileDeveloping the First Servlet . Creating a Servlet Using the NetBeans IDE . The following tutorial consists of steps to create a new

Developing the First Servlet

The project is created with the configuration shown in the Projects group (highlighted).

Review the file structure NetBeans automatically created for the project by selecting the Files group (highlighted).

Page 4 of 17

Page 5: Developing the First Servlet - cnx.org · PDF fileDeveloping the First Servlet . Creating a Servlet Using the NetBeans IDE . The following tutorial consists of steps to create a new

Developing the First Servlet

The index.html file is shown below with its default (starting) content.

Page 5 of 17

Page 6: Developing the First Servlet - cnx.org · PDF fileDeveloping the First Servlet . Creating a Servlet Using the NetBeans IDE . The following tutorial consists of steps to create a new

Developing the First Servlet

R-click the project name “FirstServlet” in the Project group and select Run to run the project. Glassfish is automatically started if it is not running and the index.html page is displayed in a browser as shown.

Now we are ready to write a servlet and access it from the index.html page which will be modified in a few steps. In the Project group, R-click Source Packages | New | Java Class…

Make the following settings/selections. Any name can be chosen for the Package. It is common practice to name packages using the reverse order of your domain to help reduce the possibility of namespace collisions. Select Finish.

Page 6 of 17

Page 7: Developing the First Servlet - cnx.org · PDF fileDeveloping the First Servlet . Creating a Servlet Using the NetBeans IDE . The following tutorial consists of steps to create a new

Developing the First Servlet

The file and package are created as shown.

Replace the starting content of AddServlet.java with that shown. Java EE web servlets must extend HttpServlet which occurs on line 9. HTTP servlets have been routinely used to process POST and GET requests submitted by a form. In upcoming tutorials, we will use the more powerful technique of binding client HTML controls directly to server

Page 7 of 17

Page 8: Developing the First Servlet - cnx.org · PDF fileDeveloping the First Servlet . Creating a Servlet Using the NetBeans IDE . The following tutorial consists of steps to create a new

Developing the First Servlet

In the AddServlet.java example, POST requests are processed by the doPost method which accepts requests and response objects as parameters. The MIME type of the response is specified on line 15 which will be text/html. On lines 16 and 17 the values of num1 and num2 are extracted from the request object. On line 18, the try with resources syntax is used which was introduced with Java EE 7. HTML output is produced from lines 19 through 35.

Page 8 of 17

Page 9: Developing the First Servlet - cnx.org · PDF fileDeveloping the First Servlet . Creating a Servlet Using the NetBeans IDE . The following tutorial consists of steps to create a new

Developing the First Servlet

Replace the starting content of index.html with that shown.

R-click on the WEB-INF folder in the Projects group and select New | Other.

Page 9 of 17

Page 10: Developing the First Servlet - cnx.org · PDF fileDeveloping the First Servlet . Creating a Servlet Using the NetBeans IDE . The following tutorial consists of steps to create a new

Developing the First Servlet

Select as shown to create the web.xml which is known as the deployment descriptor and then click Next.

Page 10 of 17

Page 11: Developing the First Servlet - cnx.org · PDF fileDeveloping the First Servlet . Creating a Servlet Using the NetBeans IDE . The following tutorial consists of steps to create a new

Developing the First Servlet

Make the appropriate settings (should be prepopulated as the default settings) and click Finish.

Page 11 of 17

Page 12: Developing the First Servlet - cnx.org · PDF fileDeveloping the First Servlet . Creating a Servlet Using the NetBeans IDE . The following tutorial consists of steps to create a new

Developing the First Servlet

Replace the starting contents of web.xml with that shown. The file contains application configuration settings. In particular, notice the servlet and servlet mapping tags that provide settings for the AddServlet.java file in a previous image.

R-click the project name “FirstServlet” in the Project group and select Run to run the project. Glassfish is automatically started if it is not running and the index.html page is shown. Enter numbers to test the application.

Page 12 of 17

Page 13: Developing the First Servlet - cnx.org · PDF fileDeveloping the First Servlet . Creating a Servlet Using the NetBeans IDE . The following tutorial consists of steps to create a new

Developing the First Servlet

Select Send to Servlet on the index.html page to see the AddServlet output as shown.

Thus far, a starting Java EE 7 web application was created which included the files index.html, AddServlet.java, and web.xml. In the next few steps, we will create a copy of the FirstServlet project and name the copy FirstServletAnn. We will make adjustments to use a feature known as annotations which is syntactic metadata. The @Override annotation has already been used on line 11 of AddServlet.java which is used to cause a compilation error if the following method is not found in a parent class or implemented interface. The @Override on line 11 applies to the doPost method which overrides the doPost method of HttpServlet.java. This can be observed by hovering over or clicking on the green dot on line 12.

Page 13 of 17

Page 14: Developing the First Servlet - cnx.org · PDF fileDeveloping the First Servlet . Creating a Servlet Using the NetBeans IDE . The following tutorial consists of steps to create a new

Developing the First Servlet

R-click on the FirstServlet project in the Project group and select Copy… Make the appropriate settings in the Copy Project dialog and select Copy.

Notice that the FirstServletAnn appears in the Project group. R-click FirstServlet to close the previous version of the project. After you close a project, it can be reopened when/if desired.

FirstServletAnn should now be the only project open in the Project group as shown.

Page 14 of 17

Page 15: Developing the First Servlet - cnx.org · PDF fileDeveloping the First Servlet . Creating a Servlet Using the NetBeans IDE . The following tutorial consists of steps to create a new

Developing the First Servlet

R-click AddServlet.java and select Refactor | Rename… Change the name to AddServletAnn and select Refactor. Refactoring helps to ensure the name change is made internally to the class and not just to the file name. To observe where refactoring will occur select Preview. After preview, select Do Refactoring at the bottom of the Preview window. Or, the preview can be skipped and select Refactor in the dialog below.

Update AddServeltAnn.java with the modifications shown. All changes occur prior to line 12. Be very attentive. Naturally, one letter discrepancies can prevent an entire web site from functioning.

Page 15 of 17

Page 16: Developing the First Servlet - cnx.org · PDF fileDeveloping the First Servlet . Creating a Servlet Using the NetBeans IDE . The following tutorial consists of steps to create a new

Developing the First Servlet

The @WebServlet annotation on line 10 provides the naming and mapping that was previously supplied by web.xml in the FirstServlet version of the project. Therefore, web.xml is no longer required and should be deleted. After saving the changes to AddServletAnn.java and deleting web.xml, try running the project by R-clicking FirstServletAnn and selecting Run. You should receive a 404 error like that shown. Why does the error appear? Devote a little time/energy to thinking about the cause of the error before moving on.

Page 16 of 17

Page 17: Developing the First Servlet - cnx.org · PDF fileDeveloping the First Servlet . Creating a Servlet Using the NetBeans IDE . The following tutorial consists of steps to create a new

Developing the First Servlet

To resolve the error, open index.html and modify as shown. There is only one change required. Save the file and go back to index.html in the browser, refresh (F5), and try the page again. It should work with the correction applied.

Page 17 of 17