Page 1 of 1

HelloWorld example using servlets tomcat web server used Rate Topic: -----

#1 bhandari  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 9
  • View blog
  • Posts: 754
  • Joined: 31-January 08

Posted 14 February 2008 - 03:40 AM

Attached File  sample.zip (69.33K)
Number of downloads: 1289Hi All,

Here we will learn how to create a servlet using apache tomcat web server:

Download and then configure tomcat server. See your tomcat server documentation to know how to configure it.

The steps to prepare resources are:

Go to your tomcat installation directory.
Go to webapps directory
Create a folder named “sample” inside webapps
Create a folder named “WEB-INF” inside sample
Create a file named web.xml in WEB-INF folder
Create three folders viz. classes, lib and src under WEB-INF

The structure should now be like:

Tomcat 4.1
|
webapps
|
sample
|
WEB-INF
|
|__classes
|___lib
|___src
|___web.xml


Create a file in src folder named process.java

Copy and paste the following code:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class process extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		PrintWriter out = response.getWriter();
		out.println("Hello World");

	}
}





Copy following lines in web.xml:

<?xml version="1.0" encoding="ISO-8859-1"?> 

<!DOCTYPE web-app
	PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
	"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

	<servlet>
		<servlet-name>process</servlet-name>
		<servlet-class>process</servlet-class>
	</servlet>
	
	<servlet-mapping>
				<servlet-name>process</servlet-name>
				<url-pattern>/process</url-pattern>
	</servlet-mapping>
		 
</web-app>



Copy Servlet.jar in the lib folder. Get it from
/<tomcat installation directory>/common/lib folder

Place the above servlet.jar file into your classpath environment variable.

Compile process.java and place the .class file in classes folder.

Start the tomcat server.

Browse your servlet using the following URL:
http://localhost:<port>/sample/process


Note that there is no need of providing /servlet after /sample because you have provided the web.xml file.

Note: If you don't understand anything then use the zip file attached alongwith this post. Extract the contents of the zip file in the webapps directory of your tomcat server. Start the server. Browse your servlet using the url as given above. (port is usually stored in server.xml file. Check documentation of your web server)

I admit that its confusing for the very beginner. Any assistance required, please do post.

This post has been edited by bhandari: 21 February 2008 - 12:50 AM


Is This A Good Question/Topic? 0
  • +

Page 1 of 1