File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes How to execute a servlet program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to execute a servlet program" Watch "How to execute a servlet program" New topic

How to execute a servlet program

S Thanigaivel
Ranch Hand

Joined: Oct 06, 2005
Posts: 60
My jdk path
C:\Program Files\Java\jdk1.5.0

My jsdk path

My Tomcat path
D:\Program Files\Apache Software Foundation\Tomcat 5.0

My HtmlFile path
contains: FirstServlet.html

My ServletFile path
contains: and FirstServlet.class

Now how to execute my servlet file?

This is my program

<TITLE> Test to invoke a Servlet </TITLE>
<P> Invoking a Servlet from a Hyperlink </P>
<P> <A Href="http://localhost:8080/servlet/FirstServlet">
Click here to invoke a Servlet </A> </P>

servlet prog

import javax.servlet.*;
import javax.servlet.http.*;

public class FirstServlet extends HttpServlet
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
PrintWriter out;
String title = "This is your first Java Servlet Program";

//Set content type and other response header fields first

//Then write the data of the response
out = response.getWriter();


out.println("<BODY bgcolor=\"#FFFFFF\">");
out.println("<H2>Congratulations Ganesh!<BR>");

//To close the Output stream

plz guide me this is my first servlet program!


Thanks in Advance

[ October 08, 2005: Message edited by: S Thanigaivel ]
Abdulla Mamuwala
Ranch Hand

Joined: Jan 09, 2004
Posts: 225
Hi Thanigaivel

In order to execute your servlet you will have to create a web.xml file, which will contain the details about your FirstServlet.class and the URL used to invoke your servlet( in your case http://localhost:8080/servlet/FirstServlet ). Then you need to place your class files in the YourProject/WEB-INF/classes folder for the container to find them. Apart from that you will have to make sure your tomcat is running fine and class paths are set correctly. I suggest you either follow a book or check out the numerous sites on the web for the same. Check
Marty Hall

Good luck !
MI Mohammed
Ranch Hand

Joined: Feb 16, 2005
Posts: 146
Hi Thanigaivel,

I like your approach towards runing your first servlet. Sure it didn't work. Never mind, most of us had problem runing our first servlet. So you're on course. Let me take you through.

1. Organise the file structure. as follows (using Tomcat);
under webapp; create a folder(call is serv - this is your app root
dirctory) .
2. Create another, dir ( call it WEB-INF - a most, where your web.xml and classes and lib dir resides).

3. Compile your servlet class. (remember to add the servlet-api.jar in the classpath, if not the servlet class won't compile). put the compile servlet class into .../webapp/serv/web-INF/classes.

4. Create web.xml as follows.

<servlet-name>any name for servlet</servlet-name>
<servlet-class>fully qualify name of servlet class
(without class extention)
<servlet-name>same as above</servlet-name>

5. Restart the web container. the hit the page using
the name specified in <url-pattern>

Hope this clears some of the curious part of your first servlet.

SCJP, SCJA, SCWCD( in .....)

SCJA(Beta) SCJP 1.4 SCWCD 1.4 SCBCD 1.3 SCBCD 5.0 beta <br />The more practice we get, the better we are at the exams and in life in general. Pls join me at My DEN.
S Thanigaivel
Ranch Hand

Joined: Oct 06, 2005
Posts: 60
Now i'm able to run servlets!

I Thank again Mr.Abdulla Mamuwala and Mr.mi Mohammed



S Thanigaivel
I agree. Here's the link:
subject: How to execute a servlet program
It's not a secret anymore!