Creating Your First Web Application - JSP

Locate the folder called <tomcat-home>webapps, and create a new folder within it called chapter01.You should now have the following folder structure on your machine:

The webapps folder within Tomcat is where all the Web applications reside. Each folder represents an individual Web application.You have just created a new folder to hold your Web application.

Note

If you would rather not create all the various examples shown in this chapter, you can download the appropriate war file from the book Web site. In this case, it is called chapter01.war. This can then be placed in the webapps folder in Tomcat, or extracted using a tool such as WinZip into the chapter01 folder you have just created.

If you look again at Figure , you can see the various deployment descriptors mentioned for each of the different parts of J2EE applications.You are creating a Web application; therefore, you will need to create a file called web.xml.

Create a new folder within the chapter01 folder called WEB-INF, all uppercase. Then, within this folder, create a new file called web.xml.

An example web.xml for the BasicServlet class, can be seen in Listing 1.3:

The root element of the web.xml deployment descriptor is always web-app. Note that it contains a reference to an XML Schema which defines the elements and attributes used within the XML document.To deploy a servlet, there are two child elements that need to be specified.The <servlet> element defines the name you want to give your servlet with its associated class.The <servlet-mapping> element defines the URL that will be used to invoke the servlet.

The name of your servlet is set to be the same as the class name in Listing 1.3.The URL for the servlet named BasicServlet is then set to be /myServlet. The last thing you will need to do to complete the Web application is to put the compiled servlet class into the folder structure.There is a specific place within a Web application where you are meant to place class files.Within the WEB-INF folder, you should create a new folder called classes, and place BasicServlet.class in there. Your completed Web application should now have the following form:

you should see an output like Figure:

Creating Your First Web Application

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

JSP Topics