Reputation: 11183
I need to invoke a Servlet on application startup since it contains some application initialization logic.
I know I can set load-on-startup configuration, but this will only invoke Servlet’s init method. I need to invoke a doGet method and pass some Url parameters to it. Servlet doGet method expects ServletRequest and ServletResponse objects.
Also, since this is clustered application, I need to know exactly what node I am accessing (since one option is just to open a socket and invoke a Servlet).
What is the best option to perform this?
EDIT: As a clarification, Servlet already exist and can not be modified. Until now, someone would manually invoke the Servlet from the browser. I need to automatize this.
Upvotes: 3
Views: 6634
Reputation: 308
The best way to do this by using java.lang.Runtime. Its worked for me perfectly. You can override init method in which servlet(here my servlet name is BackEndServlet) you have to call either doGet or doPost method.
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
String url = "http://localhost:8080"+config.getServletContext().getContextPath()+"/BackEndServlet";
System.out.println(url);
String os = System.getProperty("os.name").toLowerCase();
Runtime rt = Runtime.getRuntime();
try{
if (os.indexOf( "win" ) >= 0) {
// this doesn't support showing urls in the form of "page.html#nameLink"
rt.exec( "rundll32 url.dll,FileProtocolHandler " + url);
} else if (os.indexOf( "mac" ) >= 0) {
rt.exec( "open " + url);
} else if (os.indexOf( "nix") >=0 || os.indexOf( "nux") >=0) {
// Do a best guess on unix until we get a platform independent way
// Build a list of browsers to try, in this order.
String[] browsers = {"epiphany", "firefox", "mozilla", "konqueror",
"netscape","opera","links","lynx"};
// Build a command string which looks like "browser1 "url" || browser2 "url" ||..."
StringBuffer cmd = new StringBuffer();
for (int i=0; i<browsers.length; i++)
cmd.append( (i==0 ? "" : " || " ) + browsers[i] +" \"" + url + "\" ");
rt.exec(new String[] { "sh", "-c", cmd.toString() });
} else {
return;
}
}catch (Exception e){
return;
}
return;
}
Upvotes: 0
Reputation: 1109735
This is in one word terrible, but you could use java.net.URL
/java.net.URLConnection
for this.
new URL("http://localhost/yourservlet").openStream();
Upvotes: 0
Reputation: 28905
The best option is to refactor whatever logic you have in the doGet
method into a separate method that can be invoked both from init
and doGet
.
If you really can't refactor the logic (which really is the only good option), you can use some mock library. Google says Spring's mock objects are popular.
Having a usable implementation of HttpServletRequest
and HttpServletResponse
, make a servlet loaded with load-on-startup
, and from its init
method, locate the relevant servlet from the current ServletContext
, and invoke doGet
with the appropriate request and response objects. (Yes, it's a pretty bad kludge, but you'll have to do something like this.)
Edit: If you don't want to hack the WAR file, maybe you should check if your servlet container has the possibility to run some kind of hooks after you re/deploy a web app.
Upvotes: 2
Reputation: 20627
Normally, bootstrup initialization / shutdown cleanup is achieved with ServletContextListener - did you consider this option?
Alternatively, as an ugly hack, you can implement a servlet superclass with the initializtion logics, that will be called just once.
Upvotes: 3