I'm creating a web application with servlets and I need a thread changing a number constantly and when a client do a request it gets the current number.
I tried creating a class with the main method and from there start the thread, then from the servlet get the instance where the thread is running (can this be possible?), but the application never enters to the main method.
Any suggestions?
Thanks
Servlets run in a web container and the web container's main method is out of your control.
If you want to perform any startup operations, then the servlet framework provides context listeners that can be registered with the framework. These listeners are called when your web application starts.
Alternatively, if you want to perform some operation on each incoming request or outgoing response, then you can use servlet filters
You should have a scheduled task that runs on your webserver, that updates that number. There is no "main" method (as in application entry point) in web applications, since each servlet is an independent entry point.
Related
A web container will typically create a thread to handle each request. To ensure that a servlet instance handles only one request at a time, a servlet can implement the SingleThreadModel interface. If a servlet implements this interface, no two threads will execute concurrently in the servlet’s service method.A web container can implement this guarantee by synchronizing access to a single instance of the servlet or by maintaining a pool of web component instances and dispatching each new request to a free instance.
Could someone clarify the bolded part of the above paragraph ?
Doesn't each servlet would have only one instance of it in a web container?
Doesn't each servlet would have only one instance of it in a web container?
No, that is not always true. The web container manages the lifecycle of servlets (it is responsible for creating and managing instances of servlets). The Java EE specification does not guarantee that there will be only one instance of your servlet class, so you should not write your servlets in a way that depend on this.
As the description explains, the web container might make multiple instances of your servlet class to efficiently and concurrently handle requests if your servlet implements SingleThreadModel.
It is better to write your servlet in such a way that it does not need to implement SingleThreadModel - write your servlet code in a thread-safe way.
I have a following scenario.
suppose in web application there is folder called dump in which data logs files are created. i am restarting my web server (apache tomcat). so before my web application start or ready to serve the request. it should execute some program and fetches the data from logs file and need to insert into table.
I know we can do something like that using Interceptors in struts2 /spring mvc or using Servlet making load-on-startup. but it will execute when the server is ready to request. i want to execute above program while web server is going to initialised and before web application is started.
You could use the Context Listener, provided for by the JavaEE spec: http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContextListener.html#contextInitialized(javax.servlet.ServletContextEvent)
contextInitialized(), specifically will be an event that you want to operate on.
However, you may want to be sure about what resources are initialized/ready for you to use at this phase.
I am writing a code for a web application using Java and Apache Tomcat. The web application involves an authentication system. My question: is there any way that I can execute a certain code every time my web application receives a request. So instead of adding the code to check if the user is logged in every page, the code gets called automatically when the application receives a request.
Thanks!
You have at least a couple of options
make all your servlets extend the same base class and put the shared code in the service method
use a J2EE filter to intercept the message and put the shared code there
I'd go for the second of these options, providing that it's not too much of a code restructure. Authentication is a cross-cutting concern and is exactly what the Filter framework was designed to handle.
If I understand you correctly you should take a look on HTTP Filter.
You should implement interface javax.servlet.Filter, register you filter in web.xml using <filter> tag and your filter will be called on each call of URL you mapped to this filter.
You could use filter to control the event call.
You also can use tomcat container's background thread follow the event every 5 min.
Form-based authentication is for you.
I have a client application which accesses a web service api using apache axis.
My client is a service which calls the web service randomly. So I do not know when exactly the interaction happens.
I want to keep the session active betweeen the web service calls.
I am thinking of having a seperate thread which periodically calls a dummy method so that the session never times out? is this a right idea or does it have any draw backs?
Another way is to relogin when ever a "session timed out" exception is thrown while accessing the webservice. But since many objects can interact with the web service this will create new problems like synchronization to be handled.
Can i create and start a thread from inside a web service operation of a Java Web Application normally?If yes should that thread class be in the classes of the Web Application or the Java Application that consumes the service?
The server JVM will certainly need to load the classes used by your new thread, so they will need to be somewhere on the server classpath. Unless you also need to use some or all of them on the client application there will be no need for them to be on the client classpath. How you name and package them is up to you but if they are only used inside the server app it would make sense to use similar names or the same parent packages.
You should consider using something like Quartz to manage your threads and depending on what you want this web service to do, consider using JMS/MDB instead.