Solved

Can't find ServletContext methods

Posted on 2003-11-12
9
1,341 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

740 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