• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Having Problem Compiling Servlet Code

 
Vishnu Bhardwaj
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I was trying to run my first "hello servlet" and tool complains about "javax.servlet.*" not found. I cut and pasted the example from "Marcus Greens" web book and wanted to compile it in TextPad. Infact its not finding any "javax.*.*" stuff.

a) Currently I have Java SE 1.5. Do I need to down-load servlet "jar" file and place it in ../jre/lib/ext directory??? If so where can I find it on sun-web site? When I went into the "../lib/ext/" directory I coulnd't find a jar file with "servlet.jar" name so how will I know whether the SE version I have downloaded has servlet package in the right place?

b) I am not sure whether servlets jar comes with "Java SE" pacakge and hence down-loaded "Java EE" but still has similar problem. BTW, I dont have problem running "non-servlet" related java code".

c) I have also downloaded tomcat 5.0 and its working fine.

I am doing some simple thing wrong here. Please suggest.

thanks
--Vishnu

Here is that hello world template: [FROM MARCUS GREEN's WEB COURSE]

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorldServlet extends HttpServlet{

public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
/* The heart of the servlet */
PrintWriter pw = response.getWriter();
pw.println("<html>");
pw.println("<h1> Hello World </h1>");
pw.println("</html>");
}

public static void main( String [] args ) {

} //public static void main()
}

Some of the Compilation Errors :
==================================
HelloWorldServlet.java:4: package javax.servlet does not exist
import javax.servlet.*;
HelloWorldServlet.java:5: package javax.servlet.http does not exist
import javax.servlet.http.*;

[ January 19, 2008: Message edited by: Vishnu Bhardwaj ]
[ January 19, 2008: Message edited by: Vishnu Bhardwaj ]
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The servlet-api.jar file is part of Java EE, so if you have that (installed, or put in your classpath) you should be all set. But if you have a servlet container installed then that also contains it. E.g., for Tomcat look in the TOMCAT_HOME/common/lib directory; for other servers, have a look around their installation directories to find it.
 
Collins Mbianda
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do I need to down-load servlet "jar" file and place it in ../jre/lib/ext directory???


No, you don't have to put any thing in that directory.
You just have to add the "servlet-api.jar" to your classpath.

If your are using tomcat you can compile your servlet with the command line:

javac -classpath "CATALINA_HOME\common\lib\servlet-api.jar" MyServlet.java

Where CATALINA_HOME is the directory where you install Tomcat.

Hope it help
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vishnu,

I hope now you would have the answers to your queries from the ranchers.

Kindly make use of the Code Tags hereafter to paste the java codes so that it will look uncluttered.

To your question, Yes, Servlet is a part of Java EE and NOT of Java SE.
 
Vishnu Bhardwaj
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Collins et al.

It worked by setting the path to the TOMCAT server directory's jar file: servlet-api.jar.

--Vishnu
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic