8

I got this big newbie question. When i try the following; the 'servlet' turns red and indicates 'Cannot resolve symbol 'servlet'.

import javax.servlet.http.*;
import javax.servlet.ServletException;

I got apache tomcat running. I am a very big java newbie. Can anyone help me where to find a servlet library or something ? I googled but got no clear explanation of how to make this work.

This is the content of my web.xml file;

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">

    <display-name>
        HelloWorld
    </display-name>
    <description>
        This is my first webapp
    </description>

    <servlet>
        <servlet-name>Hello world!</servlet-name>
        <description>This is a hello world servlet</description>
        <servlet-class>servlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>HelloWorldServlet</servlet-name>
        <url-pattern>HelloWorldServlet</url-pattern>
    </servlet-mapping>

</web-app>

EDIT: I use the IntelliJ IDEA IDE. And I am using Maven.

Ben
  • 5,584
  • 4
  • 25
  • 36

5 Answers5

14

The servlet jar needs to be in your build path.

If you are using maven you could do this :

<dependency>
 <groupId>org.apache.tomcat</groupId>
 <artifactId>tomcat-servlet-api</artifactId>
 <version>7.0.21</version>
 <scope>provided</scope>
</dependency>

Or use one of the providers listed here, Such as the below which is not dependent on a specific container :

<dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>javax.servlet-api</artifactId>
   <version>3.0.1</version>
   <scope>provided</scope>
</dependency>
Community
  • 1
  • 1
NimChimpsky
  • 43,542
  • 55
  • 186
  • 295
3

Sounds like you're missing a classpath entry for servlet.jar. You haven't told us how you're building this, but basically you need to compile against servlet.jar. You shouldn't need to explicitly put it anywhere for execution time, as Tomcat should take care of that.

Jon Skeet
  • 1,261,211
  • 792
  • 8,724
  • 8,929
  • @BWestra: If you're 100% new to Java, then I'd suggest starting off with simpler programs instead of servlets. Learn the basics through simple console applications. How you add a library to your classpath depends on how you're developing - you haven't told us anything about whether you're using an IDE, compiling from the command line, using Ant, Maven, whatever. – Jon Skeet Feb 28 '12 at 09:23
  • @BWestra: Well if you're working with Maven, you should know how to work with dependencies in general - and you need the servlet dependencies, basically. I don't use Maven myself, but NimChimpsky's answer looks like it's along the right lines. I'd suggest trying to use a dependency which *isn't* Tomcat-specific though. – Jon Skeet Feb 28 '12 at 09:28
2

You are missing Servlet jar in your classpath.add the same jar in your classpath.

If you are using Eclipse ,right click on the project.

-->Properties --->Java build path --->select Libraries

add jar there.

Balaswamy Vaddeman
  • 7,634
  • 3
  • 28
  • 40
1

The first time used IntelliJ IDEA I have the same issue, but I think the principle should be the as Eclipse, just configure our needed jar file to the external library.

Go to "File" ----> "project structure" ----> "Library",then click the button and add the needed jar.

Can not find Servlet seems you lost the server-api.jar file,just put it into your library. And if you want to build a j2ee project,simple that file shall make no sense.

So just put the whole local tomcat/lib jar file to your project and the function is the same as eclipse(configure build path--> runtime server...).

aircraft
  • 16,211
  • 16
  • 74
  • 135
Crabime
  • 552
  • 7
  • 23
0
<servlet>
    <servlet-name>Hello world!</servlet-name>
    <description>This is a hello world servlet</description>
    <servlet-class>servlet</servlet-class> <--here is full name of your servlet class.
</servlet>

<servlet-mapping>
    <servlet-name>HelloWorldServlet</servlet-name><--here must be match <servlet-name>
    <url-pattern>HelloWorldServlet</url-pattern>
</servlet-mapping>
xuanyuanzhiyuan
  • 3,253
  • 1
  • 12
  • 6