Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1412
  • Last Modified:

Can't find ServletContext methods

I am trying to build the following simple Java program:

import javax.servlet.*;
import java.util.Enumeration;
import java.io.IOException;

public class ContextDemoServlet implements Servlet {
           ServletConfig servletConfig;
                  
      public void init(ServletConfig config) throws ServletException {
               servletConfig = config;
      }
      
      public void service(ServletRequest request ,
                          ServletResponse response)
                          throws ServletException , IOException {
            
            ServletContext servletContext = servletConfig.getServletContext();
            Enumeration attributes = servletContext.getAttributeNames();
            while (attributes.hasMoreElements()) {
                  String attribute = (String) attributes.nextElement();
                  System.out.println("Attribute name : " + attribute);
                  System.out.println("Attribute value : " +
                  servletContext.getAttribute(attribute));
            }
            System.out.println("Version :" + servletContext.getMajorVersion()
                   +"."+ servletContext.getMinorVersion());
            System.out.println("Server info :" + servletContext.getServerInfo());
      
      }
      
      public void destroy() {
             
      }
      public String getServletInfo() {
            return null;
      }
      public ServletConfig getServletConfig() {
            return null;
      }
}

I get:

--------------------Configuration: j2sdk1.4.1_02 <Default>--------------------
C:\jakarta-tomcat-4.0\webapps\myApp\WEB-INF\classes\ContextDemoServlet.java:17: cannot resolve symbol
symbol  : method getAttributeNames ()
location: interface javax.servlet.ServletContext
            Enumeration attributes = servletContext.getAttributeNames();
                                                       ^
C:\jakarta-tomcat-4.0\webapps\myApp\WEB-INF\classes\ContextDemoServlet.java:24: cannot resolve symbol
symbol  : method getMajorVersion ()
location: interface javax.servlet.ServletContext
            System.out.println("Version :" + servletContext.getMajorVersion()
                                                               ^
C:\jakarta-tomcat-4.0\webapps\myApp\WEB-INF\classes\ContextDemoServlet.java:25: cannot resolve symbol
symbol  : method getMinorVersion ()
location: interface javax.servlet.ServletContext
                   +"."+ servletContext.getMinorVersion());
                                             ^
3 errors

Process completed.

Presumably this means that the compiler can't find the methods in servlet.jar.  But according to the documentation they should be there.

My question is: (1) What am I doing wrong?
and
(2) more generally, is there a way of checking for methods in a class or jar file ?
                          
0
benfield1
Asked:
benfield1
  • 3
  • 3
  • 3
2 Solutions
 
vk33Commented:
Hi!

1. Is ServletContext interface found? What is your tomcat version?
2. You can see the jar-file contents:

jar -tf project.jar

If you have no source code provided, you still can use reflection to see the methods available:
http://java.sun.com/docs/books/tutorial/reflect/

For Servlet API see the following:
http://jakarta.apache.org/tomcat/tomcat-5.0-doc/servletapi/index.html

Regards!
0
 
grim_toasterCommented:
Have you included the required jar files in your compiler classpath?  As in:

 javac -cp .... ContextDemoServlet.java

Where the .... represents the jar file/s required.
0
 
benfield1Author Commented:
VK
1. Is ServletContext interface found?   How do I tell that? Looks like it from the listing below
What is your tomcat version?             4.0 (I couldn't get 4.1 or 5 to work)


GT
This is the console equivalent listing specifying the classpath

C:\jakarta-tomcat-4.0\webapps\myApp\WEB-INF\classes>javac -classpath c:\jakarta-
tomcat-4.0\common\lib\servlet.jar ContextDemoServlet.java

ContextDemoServlet.java:17: cannot resolve symbol
symbol  : method getAttributeNames ()
location: interface javax.servlet.ServletContext
                Enumeration attributes = servletContext.getAttributeNames();

etc    

Any thoughts?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
grim_toasterCommented:
Okay, the path looks alright.  What do you mean by console equivalent?  Are you using some other IDE?  I had a problem similar to this a while ago using Oracle's JDeveloper, where they were including other jar files in the classpath with older versions of the javax.servlet API's in it.
0
 
vk33Commented:
>How do I tell that?
Do you get anything like
ContextDemoServlet.java:16: cannot resolve symbol
symbol  : interface ServletContext

If not, then your interface is found and classpath is ok.

Try to specify relative path rather than the absolute one in your classpath:
C:\jakarta-tomcat-4.0\webapps\myApp\WEB-INF\classes>javac -classpath ../../../../common/lib/servlet.jar ContextDemoServlet.java

I don't like the ":" symbol in "c:\", it's used as classpath separator... :)

Hope this helps!
0
 
benfield1Author Commented:
OK, I was using JCreator to start with, but this is typed straight into a dos session using the relative path.  I have also deleted the Classpath environment variable so that can't be implicated.

C:\jakarta-tomcat-4.0\webapps\myApp\WEB-INF\classes>javac -classpath \..\..\..\.
.\common\lib\servlet.jar ContextDemoServlet.java
ContextDemoServlet.java:17: cannot resolve symbol
symbol  : method getAttributeNames ()
location: interface javax.servlet.ServletContext
                Enumeration attributes = servletContext.getAttributeNames();
VK, you see it finds the interface ServletContext, but not the method.

Any more thoughts?
0
 
grim_toasterCommented:
Try unzipping the servlet.jar file, and putting the ServletContext class through a decompiler (such as JAD), and see if it contains the required methods (or for that matter that the servlet.jar file contains the correct class).  Alternatively, depending on your version of JCreator, you should have pre-emptive typing, so see if that would bring up the methods.

Unfortunately I can't seem to find Tomcat version 4.0 for download, so I can't check it out.  But I would suggest trying to get version 4.1.29 (the most recent stable release), what problems were you having?
0
 
vk33Commented:
Hi!

Well, with my Tomcat 4.1.18 I have no problems compiling your code:

# $JAVA_HOME/bin/javac -classpath .:/usr/local/jakarta-tomcat-4.1.18/common/lib/servlet.jar ContextDemoServlet.java

Probably you could try installing a newer version of Tomcat.
Btw, I'm running Linux. :)

Regards!
0
 
benfield1Author Commented:
OK guys I've finally solved it by downloading a new versson of the ServletContext class from the jwsp-1.3 package.

To get the compiler to take this I've had to remove the J2SDK files from j2sdk1.4.1_02/lib and j2sdk1.4.1_02/jre/ext/lib - they must have been older versions.

All is sweet now and I've split the points between you for your help.

Regards

Andy Smith
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now