How to call a function while running a Java web application? - java

I have a web application in Java (Netbeans). And I have a function that should be called exactly while running the web application, without putting it into the static method main.
I really don't have any idea about how to do.
Thank you in advance.

Create a class that implements ServletConextListener :
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class ListenToMeFirst implements ServletContextListener {
#Override
public void contextDestroyed(ServletContextEvent arg0) {
}
#Override
public void contextInitialized(ServletContextEvent arg0) {
// Run me First while deploying!!!
}
}
Don't forget to put it in your web.xml file :
<listener>
<listener-class>path.to.yourListenerClass.ListenToMeFirst</listener-class>
</listener>

Related

How to create a Java Class while Tomcat startup?

I have a main Java application running in a tomcat environment.
Now i have written a java class, put it into a JAR file and in the TCs lib folder. I can access that class now in the main app by importing the class and calling the constructer.
is there a way to create that class once at TCs startup. so i can access the classes variables?
Thanks!
e.
//EDIT 1
here is my example:
Beach.java
public class Beach {
public static void main(String []args) {
System.out.println("***********************");
}
}
MyAppServletContextListener.java
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class MyAppServletContextListener implements ServletContextListener{
#Override
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("**************** ServletContextListener started");
Beach x = new Beach();
}
#Override
public void contextDestroyed(ServletContextEvent arg0) {
}
}
all this goes into a jar file and into :
…/WEB-INF/lib/beach.jar
and this is my addition to Web.xml:
<web-app>
<listener>
<listener-class>
MyAppServletContextListener
</listener-class>
</listener>
</web-app>
and this is the server.log error:
10:42:26,440 | ERROR | [[/APP]] | Error configuring application listener of class MyAppServletContextListener
java.lang.ClassNotFoundException: MyAppServletContextListener
You can create a class implementing ServletContextListener
Once registered this class will allow you to invoke the desired constructor.
public class MyAppServletContextListener implements ServletContextListener{
#Override
public void contextInitialized(ServletContextEvent arg0) {
YourClass x = new YourClass();
}
#Override
public void contextDestroyed(ServletContextEvent arg0) {
}
}
You need to register this class into the web.xml:
<web-app ...>
<listener>
<listener-class>
com.yourpackage.MyAppServletContextListener
</listener-class>
</listener>
</web-app>

Execute servlet on startup of the application

I build a web application with JSPs and in my servlet I have:
public class MyServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
init();
HttpSession session = request.getSession(true);
//more code...
}
}
Till now my serlvet is called, when the JSP page calls it like <a href="MyServlet..">. What I want is whenever the application starts, the servlet to be executed as well. I could have a button in my 1st page like "START" and there to call the servlet.. But, can I avoid this?
Whatever you want done on startup should be done by a class implementing ServletContextListener, so you should write such a class, for example:
public class MyContextListener
implements ServletContextListener{
#Override
public void contextDestroyed(ServletContextEvent arg0) {
//do stuff
}
#Override
public void contextInitialized(ServletContextEvent arg0) {
//do stuff before web application is started
}
}
Then you should declare it in web.xml:
<listener>
<listener-class>
com.whatever.MyContextListener
</listener-class>
</listener>
You can configure it in Tomcat's web.xml (or corresponding configuration files in similar servers), like below using the tag <load-on-startup> :
<servlet>
<servlet-name>MyOwnServlet</servlet-name>
<servlet-class>MyServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
In my point of view, a good way is to implement a Servlet Context Listener. It listens to application startup and shutdown.
public class YourListener implements javax.servlet.ServletContextListener {
public void contextInitialized(ServletContextEvent sce) {
}
public void contextDestroyed(ServletContextEvent sce) {
}
}
And then, you configure the listener in your web.xml () or with the #WebServletContextListener annotation.

How to run the server class in gwt automatically at the time of apache starts

How to write and implement simple cronjob class in GWT?
It doesn't have any error but I need to know how to run the server class at time of apache starts.
Simply use ServletContextListener that will be called on the context initialization single time at time of server starts.
Put your logic inside contextInitialized() method.
web.xml
<listener>
<listener-class>com.x.y.z.AppServletContextListener</listener-class>
</listener>
Listener
public class AppServletContextListener implements ServletContextListener {
#Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
...
}
#Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
...
}
}

Trigger a function upon Weblogic termination

Is there anyway to capture weblogic termination event and trigger a Java function?
My weblogic version is V10.0
Thank you,
You could imlement a javax.servlet.ServletContextListener
public class MyServletContextListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent e) {
}
public void contextDestroyed(ServletContextEvent e) {
}
}
and add it to the web.xml.
<listener>
<listener-class>MyServletContextListener</listener-class>
</listener>
There you can handle the shutdwn of the weblogic container.

Instantiate value only once in a Bean

I'm new to using Servlets so please forgive me if I use incorrect terminology. I have an Object called "Provider" in JSF Bean Class "Detector" which needs to be instantiated once and then can be used for all other requests. I've done some searching and found the ServletContextListener interface which seems to do what I need. Ive mentioned it in my web.xml file like so:
<listener>
<listener-class>
p1.ContextListener
</listener-class>
</listener>
and the class looks like this:
package p1;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class ContextListener implements ServletContextListener{
#Override
public void contextInitialized(ServletContextEvent sce) {
Detector.startProvider();
}
#Override
public void contextDestroyed(ServletContextEvent sce) {
Provider.dispose();
}
}
And here is my Detector Class:
package p1;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
#ManagedBean
public class Detector{
private static Provider p;
FacesContext context;
String userAgent;
public Detector() {
context = FacesContext.getCurrentInstance();
}
public String getValue() {
return p.getValue();
}
public String getUserAgent() {
return ((HttpServletRequest) context.getExternalContext().getRequest()).getHeader("User-Agent");
}
public static void startProvider(){
p = Creater.create();
}
}
My code all works, but the only way that seems right to me is to have the Provider Object as a static but that seems like a bad idea in an Bean that will be used for different requests. My question is whether it is right to have the Provider Object as a static?
Using "static" is a bad idea. If you want an object in your servlet to be shared between all the HTTP requests processed by this servlet then simply made it a field of your servlet class. The best place for initialization of that field variable is init() method.
public class MyServlet extends HttpServlet {
private MyProdiver provider;
public void init() throws ServletException {
this.provider = new MyProdiver();
// do init
}
}
Unless your servlet class implements SingleThreadModel there is only one servlet instance per servlet declaration in your deployment descriptor (web.xml)
I found the answer I need on this question JSF initialize application-scope bean when context initialized. I set the Provider Object as an attribute of the ServletContextEvent in my "ContextListener" and retrieved it in my Detector class from my FacesContext Object "context". (This is shown in more detail in the accepted answer of the link provided)

Categories