Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to execute a servlet program  RSS feed

Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .....)
S Thanigaivel
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now i'm able to run servlets!

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



S Thanigaivel
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!