Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,492 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,719 people online right now. Registration is fast and FREE... Join Now!




HelloWorld example using servlets

 
Reply to this topicStart new topic

> HelloWorld example using servlets, tomcat web server used

bhandari
Group Icon



post 14 Feb, 2008 - 02:40 AM
Post #1


Attached File  sample.zip ( 69.33k ) Number of downloads: 163
Hi 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:

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:

CODE
<?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:
CODE
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: 20 Feb, 2008 - 11:50 PM
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 12/2/08 07:43PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month