Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · ASP.NET Newsletter Application · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Newsletter
Enter your email address to receive full length articles at Stardeveloper:


Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (43)
  ADO (16)
  ADO.NET (11)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)

Main Category  Other
  Website Maintenance (3)
Log In
UserName Or Email:

Password:

Auto-Login:

Hosted by Securewebs.com
 
Home : J2EE : Servlets : Introduction to Java Servlets, Developing your first Java Servlet
 
Read full length articles at Stardeveloper using Twitter Follow on Twitter Facebook Facebook fan page Email Get Articles via Email RSS Get Articles via RSS Feed

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
Compiling TestServlet.java class file

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.

Note: %CATALINA_HOME% environment variable was set in the Installing Tomcat 6.0 on Windows, and Developing & Running your first JSP page tutorial to the path of the Tomcat installation folder e.g., C:\apache-tomcat-6.0.14. You must have set this environment variable properly to compile the TestServlet.java source file. Please consult the previous tutorial for details.

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
Starting Tomcat server

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:

Running TestServlet

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.


Previous ( 1 Gone )( No Further Pages )

Related Articles
  1. Examining Java Servlets in detail. Servlet life cycle, HttpServlet class, ServletConfig and ServletContext Objects
  2. Managing Sessions with Java Servlets
  3. Forwarding and Including Response from other Servlets

Comments/Questions ( Threads: 13, Comments: 29 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. Good article
  2. compilation error in Tomcat5.5.17 server
  3. Help,I can't run servlet from Tomcat6.0 ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  4. test ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  5. Compling & Running Servlets ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  6. need help --regarding Servlet compilation and execution ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  7. 10 errors when i compile TestServlet.java ( 2 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  8. The requested resource (/star/servlet/com.stardeveloper.servlets.TestServlet) is not available ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  9. HttpServlet Destroy Method ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  10. Help, the Testservlet class not running in browser with tomcat 4.1.18 ( 2 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  11. Help, the Testservlet class not running in browser with tomcat 4.1.18
  12. HTTP Status 404 ( 2 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  13. the folder to keep Servlet classes ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  14. compile TestServlet errors ( 3 Replies ) This thread contains 2 replies by the Author of this Article. This thread contains 2 replies by Faisal Khan.

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed.

 
© 1999 - 2010 Stardeveloper.com, All Rights Reserved.