Solved

Can't find ServletContext methods

Posted on 2003-11-12
9
1,360 Views
Last Modified: 2013-11-24
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
Comment
Question by:benfield1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
9 Comments
 
LVL 4

Expert Comment

by:vk33
ID: 9730482
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
 
LVL 7

Expert Comment

by:grim_toaster
ID: 9730497
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
 

Author Comment

by:benfield1
ID: 9730725
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!

 
LVL 7

Accepted Solution

by:
grim_toaster earned 25 total points
ID: 9731016
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
 
LVL 4

Expert Comment

by:vk33
ID: 9731436
>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
 

Author Comment

by:benfield1
ID: 9732053
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
 
LVL 7

Expert Comment

by:grim_toaster
ID: 9737819
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
 
LVL 4

Assisted Solution

by:vk33
vk33 earned 25 total points
ID: 9738300
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
 

Author Comment

by:benfield1
ID: 9747153
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

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question