• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

compilation error while running simple servlet code

 
Jenny raj
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
package hall;

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

public class HelloWorldServlet extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
{
PrintWriter out = response.getWriter();
out.println("Hello World");
}
}


i tried executing the above servlet after installing j2ee 1.4,

while compiling the above program i am getting the follwing error message


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.*;
^
HelloWorldServlet.java:7: cannot resolve symbol
symbol : class HttpServlet
location: class hall.HelloWorldServlet
public class HelloWorldServlet extends HttpServlet
^
HelloWorldServlet.java:9: cannot resolve symbol
symbol : class HttpServletRequest
location: class hall.HelloWorldServlet
public void doGet(HttpServletRequest request,HttpServletResponse response) thr
ows ServletException, IOException
^
HelloWorldServlet.java:9: cannot resolve symbol
symbol : class HttpServletResponse
location: class hall.HelloWorldServlet
public void doGet(HttpServletRequest request,HttpServletResponse response) thr
ows ServletException, IOException
^
HelloWorldServlet.java:9: cannot resolve symbol
symbol : class ServletException
location: class hall.HelloWorldServlet
public void doGet(HttpServletRequest request,HttpServletResponse response) thr
ows ServletException, IOException

^
6 errors



what could be the problem?where i am going wrong ?
 
Scott Dunbar
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will have to include the J2EE jars in your classpath somehow. You can start on the command line with something like:

javac -classpath ,;/path/to/j2ee/jar/file.jar HelloWorldServlet.java

but soon you'll want more and more on the classpath. If you're just experimenting then you can continue with the command line. Otherwise you may want to consider a build tool such as ant to help you manage your build environment easier.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Our servlets faq has a section on compiling servlets (with links to the JIG beginner faq on classpaths).
http://faq.javaranch.com/view?ServletsFaq

If you're using Tomcat, all the dependencies needed for compilation can be found in the {tomcat install}/common/lib/servlet-api.jar file.
 
Jenny raj
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have no idea abt tomcat,
do i have to install it to run a servlet?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have installed the J2EE SDK then you don't need to download and install Tomcat - you can just deploy and run your servlet in the J2EE reference implementation application server.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For aun's j2ee sdk this will probably be j2ee.jar.
If you only want to practice servlets, you could just use Tomcat instead of the full j2ee.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic