Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Static method and Servlet

 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a Servlet has a static method and we configure Servlet to be loaded when server starts, Can I directly access the static method of Servlet by servlet.staticMethod() from JSP page?
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.
 
Dave Jose
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess, even if u don't load servlet at startup, and when u try to access static method of servlet (which is nothing but a java class), then it must invoke that method... so, the answer is - Yes.
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Devang Joshi:
I guess, even if u don't load servlet at startup, and when u try to access static method of servlet (which is nothing but a java class), then it must invoke that method... so, the answer is - Yes.


True Devang,

Actully I need to read a file when Servlet is loaded and than want to use static method of Servlet to get those data. That is the reason I need static method in Servlet, otherwise I can create class with static method and use it.
 
Dave Jose
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, see, in init method of ur servlet, if u load a class (the class will be ur filereader class), now ur filereader class will look like...

public class MyServlet {
public void init(ServletConfig config) {
super(config);
Class.forName("MyFileReader"); // to load the file-reader-class...
}
}


public class MyFileReader {
static {
// write file reading code in this static-block...
}
public static String getfileContents() {
// return string or whatever to return contents of the file...
}
}


this will make servlet independent from file reading!

voila!
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Devang Joshi:
hi, see, in init method of ur servlet, if u load a class (the class will be ur filereader class), now ur filereader class will look like...

public class MyServlet {
public void init(ServletConfig config) {
super(config);
Class.forName("MyFileReader"); // to load the file-reader-class...
}
}


public class MyFileReader {
static {
// write file reading code in this static-block...
}
public static String getfileContents() {
// return string or whatever to return contents of the file...
}
}


this will make servlet independent from file reading!

voila!


Thanks Devang!

I have utility that read property file and convert into HashMap. I will read file using this method in init() of Servlet and will make this Servlet loaded when server start.
 
Dave Jose
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
;-) yap, that will do! :-)
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see any reason to make the method static.
In fact, I wouldn't put the method in the servlet at all.
I would put it in a plain old java object (POJO) and simply call it from the servlet's init method. Putting it in a POJO will allow you to test and debug it from the command line.
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ben Souther:
I don't see any reason to make the method static.
In fact, I wouldn't put the method in the servlet at all.
I would put it in a plain old java object (POJO) and simply call it from the servlet's init method. Putting it in a POJO will allow you to test and debug it from the command line.


Ben Souther, nice idea.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chetan Parekh:
Ben Souther, nice idea.


Yes. Moreover, use servlet just to entertain requests. File I/O, DB stuff, etc. should not be done in servlet.
 
We don't have time for this. We've gotta save the moon! Or check this out:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!