Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Can't find ServletContext methods

Posted on 2003-11-12
9
Medium Priority
?
1,393 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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
LVL 7

Accepted Solution

by:
grim_toaster earned 100 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 100 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

Major Serverless Shift

Comparison of major players like AWS, Microsoft Azure, IBM Bluemix, and Google Cloud Platform

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
A solution for Fortify Path Manipulation.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.
Suggested Courses

721 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