File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Calling method from Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calling method from Servlet" Watch "Calling method from Servlet" New topic
Author

Calling method from Servlet

Andrew Babaian
Greenhorn

Joined: May 16, 2002
Posts: 25
I'm a novice programmer and I'm having trouble calling a static method that I created from a servlet. When I call from my servlet:
String date = (String) request.getParameter("txtDate");
String saturdayDate = SaturdayDate.getSatDate(date);
I get the error:
C:\Tomcat\webapps\rsb\WEB-INF\classes\TimeEntryServlet.java:29: non-static method getSatDate(java.lang.String) cannot be referenced from a static context
String test = SaturdayDate.getSatDate(date);
I wrote my method as SaturdayDate.java and it compiles in the same classes folder as my servlet. What am I doing wrong? I thought I declared the method as static correctly.
Thanks in advance,
Andrew
Here's the code:
import java.util.*;
import java.text.*;
public class SaturdayDate
{
public static String getSatDate(String date)
{
String strSatDate = "";
Date dateToday;
DateFormat fmt = DateFormat.getDateInstance(DateFormat.SHORT,Locale.US);
GregorianCalendar cal = new GregorianCalendar();
try{
dateToday = fmt.parse(date);
cal.setTime(dateToday);
int delta = 7 - (cal.get(cal.DAY_OF_WEEK));
cal.add(cal.DAY_OF_WEEK, delta);
dateToday = cal.getTime();
strSatDate = fmt.format(dateToday);
}
catch(ParseException e)
{
System.out.println(e);
}
return strSatDate;
}
}
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Welcome to JavaRanch!
This code looks correct. Perhaps what's happening is that the code you're showing us is not the code that's executing; the code installed in Tomcat is an earlier version of SaturdayDate in which getSatDate() was not marked as static. Make sure you completely uninstall your servlet, delete your .class files, compile them again, and reinstall them.


[Jess in Action][AskingGoodQuestions]
Andrew Babaian
Greenhorn

Joined: May 16, 2002
Posts: 25
I stopped tomcat, removed the java and class files from my classes folder, uninstalled the servlet by putting them in another folder and attempted to recompile. The servlet still has the same error.
by writing the handle, SaturdayDate, does this assume SaturdayDate is an object or a reference to the class or both?
String test = SaturdayDate.getSatDate(date);
thanks
Andrew
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

First of all, I don't know what I was thinking, as you're showing me a compiler error, not a runtime error; of course reinstalling could have no effect. I'm sorry, I must've been dozing off.
When you call
SaturdayDate.getSatDate()
that's supposed to be the class name.
Anyway, I think my advice basically still stands, just without the Tomcat part. Make sure you haven't got an old version of SaturdayDate.class lying about in which getSatDate() is nonstatic; I can't think of any other explanation for this error.
Andrew Babaian
Greenhorn

Joined: May 16, 2002
Posts: 25
I'll look for any other old class files. I wouldn't know where to look though. Is there a cache of some sort?
Thank you for your help.
Andrew
 
 
subject: Calling method from Servlet