|
Compiling this Servlet
Now compile this Java Servlet using the following command at the DOS prompt :
C:\apache-tomcat-6.0.14\webapps\star\WEB-INF\classes\com\stardeveloper\servlets>
javac -cp %CATALINA_HOME%\lib\servlet-api.jar TestServlet.java
Note: Substitute the D: in above image with C: (or the drive where you installed Tomcat 6.0 server and are keeping the web application files for this (and previous) tutorials.
Above picture shows all the DOS commands (cd) that you will need to change directories and get into the C:\apache-tomcat-6.0.14\webapps\star\WEB-INF\classes\com\stardeveloper\servlets folder. Once there, you will type a javac (to compile Java source file) command to compile the TestServlet.java source file. You will also provide classpath (-cp) argument with the path to servlet-api.jar file in the \lib folder of Tomcat.
If all goes well a 'TestServlet.class' file will be created in that folder.
What is web.xml file?
web.xml file, also known as "Web Deployment Descriptor" allows us to configure our web application inside the Servlet Container. Here we can specify the name of our web application, define our Java Servlets, specify initialization parameters for Servlets, define tag libraries, and a whole lot more. This file is placed inside the /WEB-INF folder.
For our simple TestServlet we use two of the features of 'web.xml' file; name of Servlet and the Servlet mapping to a URL. This will allow us to access our Servlet using a URL like /TestServlet. Now copy and paste the following text into a new file 'web.xml' and save it in the '/WEB-INF' folder :
<?xml version="1.0"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>com.stardeveloper.servlets.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/TestServlet</url-pattern>
</servlet-mapping>
</web-app>
The tags for the 'web.xml' file above are pretty simple to understand. First we define
a <servlet> tag within <web-app></web-app> tags. In <servlet-name>
we provide name of our Servlet and in <servlet-class>, the complete package and class name of our TestServlet class. The <servlet-mapping> tag allows us to specify a a URL pattern which when requested by the client browser, will result in the delegation of the response generation by the Servlet Container to our TestServlet.
This is all for 'web.xml' file. We have given our TestServlet a name ("TestServlet") and
provided a URL pattern of /TestServlet to access it.
Running this Servlet
Now we need to start Tomcat server. Type the following command at DOS prompt and hit Enter to start the Tomcat server:
D:\apache-tomcat-6.0.14\bin>startup
Now open your browser and point to this address: http://localhost:8080/star/TestServlet. You should a get response like following image in your browser window:
Summary
We began with the introduction to Java Servlets and learned that Serlvets are simple Java classes that implement javax.servlet.Servlet interface. In practice, we will most probably do that by extending javax.servlet.GenericServlet or javax.servlet.http.HttpServlet classes. We learned that Servlets are fast and efficient because they are only created once by the Servlet Container and then kept in memory.
Finally we developed a Java Servlet source code file. Then we compiled it. Next, we created the web.xml file to deploy this Java Servlet in our web application. Finally, we ran our Servlet in the Tomcat server and saw it's response.
Compiling this Servlet
Now compile this Java Servlet using the following command at the DOS prompt :
C:\apache-tomcat-6.0.14\webapps\star\WEB-INF\classes\com\stardeveloper\servlets>
javac -cp %CATALINA_HOME%\lib\servlet-api.jar TestServlet.java
Note: Substitute the D: in above image with C: (or the drive where you installed Tomcat 6.0 server and are keeping the web application files for this (and previous) tutorials.
Above picture shows all the DOS commands (cd) that you will need to change directories and get into the C:\apache-tomcat-6.0.14\webapps\star\WEB-INF\classes\com\stardeveloper\servlets folder. Once there, you will type a javac (to compile Java source file) command to compile the TestServlet.java source file. You will also provide classpath (-cp) argument with the path to servlet-api.jar file in the \lib folder of Tomcat.
If all goes well a 'TestServlet.class' file will be created in that folder.
What is web.xml file?
web.xml file, also known as "Web Deployment Descriptor" allows us to configure our web application inside the Servlet Container. Here we can specify the name of our web application, define our Java Servlets, specify initialization parameters for Servlets, define tag libraries, and a whole lot more. This file is placed inside the /WEB-INF folder.
For our simple TestServlet we use two of the features of 'web.xml' file; name of Servlet and the Servlet mapping to a URL. This will allow us to access our Servlet using a URL like /TestServlet. Now copy and paste the following text into a new file 'web.xml' and save it in the '/WEB-INF' folder :
<?xml version="1.0"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>com.stardeveloper.servlets.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/TestServlet</url-pattern>
</servlet-mapping>
</web-app>
The tags for the 'web.xml' file above are pretty simple to understand. First we define
a <servlet> tag within <web-app></web-app> tags. In <servlet-name>
we provide name of our Servlet and in <servlet-class>, the complete package and class name of our TestServlet class. The <servlet-mapping> tag allows us to specify a a URL pattern which when requested by the client browser, will result in the delegation of the response generation by the Servlet Container to our TestServlet.
This is all for 'web.xml' file. We have given our TestServlet a name ("TestServlet") and
provided a URL pattern of /TestServlet to access it.
Running this Servlet
Now we need to start Tomcat server. Type the following command at DOS prompt and hit Enter to start the Tomcat server:
D:\apache-tomcat-6.0.14\bin>startup
Now open your browser and point to this address: http://localhost:8080/star/TestServlet. You should a get response like following image in your browser window:
Summary
We began with the introduction to Java Servlets and learned that Serlvets are simple Java classes that implement javax.servlet.Servlet interface. In practice, we will most probably do that by extending javax.servlet.GenericServlet or javax.servlet.http.HttpServlet classes. We learned that Servlets are fast and efficient because they are only created once by the Servlet Container and then kept in memory.
Finally we developed a Java Servlet source code file. Then we compiled it. Next, we created the web.xml file to deploy this Java Servlet in our web application. Finally, we ran our Servlet in the Tomcat server and saw it's response.
|