Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

compilation error while running simple servlet code

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have no idea abt tomcat,
do i have to install it to run a servlet?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic