Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling method from Servlet

 
Andrew Babaian
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Andrew Babaian
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic