Solved

Can't find ServletContext methods

Posted on 2003-11-12
9
1,318 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
  • 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
 
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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:

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now