O
O
oberontrik2017-09-22 19:07:38
JavaScript
oberontrik, 2017-09-22 19:07:38

Lifecycle of servlets in Tomcat?

I would like to ask if I understand a number of things correctly, and ask a couple of questions:

1. Tomcat has a set of threads that it manages and in which it launches servlets.
At the same time, two or more instances of the same (class) servlet can be executed in parallel.

  • Are the fields of the class available to all instances of this servlet (working in parallel) in this case?
  • If yes - it turns out, at the class (not methods) level, do I need to use thread-safe collections?


2. When loading and unloading a servlet from memory, the init () and destroy () methods are called (respectively), which allow you to load resources when the servlet is first started and save some data when the servlet is unloaded from memory.
If everything is clear with the moment of loading the servlet (If a request came to the servlet, but it is not in memory, then it is loaded),
then with the moment of unloading from memory, not everything is transparent - Under what situations can a servlet be unloaded from memory? (I read somewhere that it is unloaded only when the tomcata / application is stopped, but can't a servlet be unloaded when it hasn't been working for a long time or tomcat doesn't have enough resources and wants to load some other servlet instead?)

3.Let's imagine that I wanted to "hack" user authorization at the servlet level myself. Each servlet, before executing a user request, checks for authorization. The authorization check logic is moved to a separate class (not servlet).
And here the question arises: If I need to initialize some common data before starting work of all servlets, for example, a connection to the database or something like that, and then enable servlets to use this, how best to do this? (It only occurred to me to make everything common static and call it directly, example: ClassName.getConnect ()) And did I decide correctly, taking into account the needs of different servlets to go to the database in parallel, to organize access? (Make getting java.sql.Connection into some common class and then give it to servlet'
And what pitfalls can there be, in what would take out the authorization process in a separate class and then use it in different servlets? (And how to pass it to them later without making its methods static?)

4.At some point in the life of my program (usually at the initial one), a connection to the database is created and resources common to all servlets are loaded; accordingly there are no servlets. At such a moment, I would like to either turn off tomcat (from java code), or return a static error page. (How to do both correctly? I have thoughts about returning an error page - you can change the nginx config from java and force it to reread, and redirect all requests to tomcat to the page with an error message in the config.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Anton Neverov, 2019-06-09
@Denisneedyourhelp

https://jsfiddle.net/wuopahjd/
1) Use let instead of var . This does not affect functionality. This is the standard.
2) Why are you passing the string argument to the function? I removed 3 in my example
) Everything is correct for you, only you do not return anything from the function and do not display the result inside it. return - displays the result of the function, returns values ​​that can already be displayed in the console.
https://developer.mozilla.org/en/docs/Web/JavaScript...

S
Sergey Gornostaev, 2017-09-22
@oberontrik

  1. Yes, available. Yes, you must use thread-safe collections.
    and then in code
    Socket socket = new Socket("localhost", 8005); 
    if (socket.isConnected()) { 
        PrintWriter pw = new PrintWriter(socket.getOutputStream(), true); 
        pw.println("someLongAndSecretString");
        pw.close(); 
        socket.close(); 
    }

K
karthickvarunan, 2018-03-01
@karthickvarunan

A servlet life cycle can be defined as the entire process from its creation till the destruction. The following are the paths followed by a servlet.
The servlet is initialized by calling the init() method.
The servlet calls service() method to process a client's request.
The servlet is terminated by calling the destroy() method.
Finally, servlet is garbage collected by the garbage collector of the JVM.
for servlet tutorial

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question