wood burning stoves 2.0*
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to execute a servlet program" Watch "How to execute a servlet program" New topic
Author

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
D:\JSDK2.0

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

My HtmlFile path
E:\Programs\JSDK\DocRoot
contains: FirstServlet.html

My ServletFile path
E:\Programs\JSDK\Servlets
contains: FirstServlet.java and FirstServlet.class

Now how to execute my servlet file?

This is my program

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


servlet prog

import java.io.*;
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
response.setContentType("text/html");

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

out.println("HTML");
out.println("<HEAD><TITLE>");
out.println(title);
out.println("</TITLE></HEAD>");

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

//To close the Output stream
out.close();
}
}

plz guide me this is my first servlet program!

------------------------------------------

Thanks in Advance

Regards
Thanigaivel
[ 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.

<web-app>
<servlet>
<servlet-name>any name for servlet</servlet-name>
<servlet-class>fully qualify name of servlet class
(without class extention)
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>same as above</servlet-name>
<url-pattern>/serv.do</url-pattern>
</servlet-mapping>
</web-app>

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

---------------------------

Regards,

S Thanigaivel
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How to execute a servlet program
 
Similar Threads
First Servlet in Tomcat
Doubts regarding request and response objects.
New to servlets
Doubts regarding request and response objects.
How to test my servlet