ServletConfig config object to Servlet's init() method

C

crack_cs

Hi,
I would like to know the use of the 'config' object passed to
the init() method of the Servlet.Can any one mail me the example that
signifies the importance of that 'config' object.
Further, If i'm not wrong the init() method is invoked by the servlet
container ..... can we have program in C/Java to intercept the calls
made by the container to invoke init() method so that I
can understand what object is passed to the init() method.


Thanks in advance
 
T

Thomas Fritsch

crack_cs said:
I would like to know the use of the 'config' object passed to
the init() method of the Servlet.Can any one mail me the example that
signifies the importance of that 'config' object.
It is described in the API doc of Servlet#init and of ServletConfig at
<http://java.sun.com/j2ee/sdk_1.3/te...ervlet.html#init(javax.servlet.ServletConfig)>
Further, If i'm not wrong the init() method is invoked by the servlet
container .....
Yes, you are right.
can we have program in C/Java to intercept the calls
made by the container to invoke init() method so that I
can understand what object is passed to the init() method.
Yes, by implementing a method like the following in your servlet:
public void init(ServletConfig config) {
super.init(config);
//... do anything you like, for example:
System.out.println("init was called");
config.getServletContext().log("init was called");
}
 
C

crack_cs

1. The API doc ,I had read already
2. regarding the method by you ...... I had referred that one too
previously.


but my question is I want to know can we have program in C/JAVA
that intercepts the call made by the container to the init().... so
that I can get the structure of the object that is passed to the init()
method..... we cannot get it directly bcoz ServletConfig is an
interface and has only limited objects and methods.... did you get my
point ?

Thanks and Regards,
Dhairyashil Padalkar
 
T

Thomas Fritsch

crack_cs said:
2. regarding the method by you ...... I had referred that one too
previously.


but my question is I want to know can we have program in C/JAVA
that intercepts the call made by the container to the init().... so
that I can get the structure of the object that is passed to the init()
method..... we cannot get it directly bcoz ServletConfig is an
interface and has only limited objects and methods.... did you get my
point ?
Not yet. I have more questions than answers.

Do you want to intercept the calls made by the container
(1) to init() of *your* servlet only, or
(2) to init() of *all* servlets in the container?

If (1), the technique by overriding your Servlet#init method may
suffice. You can get all init parameters (key=value) with the methods
provided by ServletConfig. You can get further parameters via
getServletContext() and the ServletContext interface.
If (2), there may be some logging options to turn on in your container,
or there may be no way. You have to look up the docs of your special
container (Tomcat or whatever you have).
Or look into the container's source code. Consider running the container
in a debugger, set a breakpoint at init(), and look into the actual
ServletConfig object.

Why do you want to know the internal structure of the ServletConfig
object? What is the goal behind your question?
Your goal contradicts the design goals of the servlet API. ServletConfig
is an interface (i.e. with limited methods), just *because* all
implementation details of the servlet container are hidden behind that
interface.
 
C

crack_cs

Yes I want to about (2)... yes it surely violates the design goals of
the Servlet API but it's just for fun.I'm the kinda guy who want take a
deep dip understanding the overall working inside out..I know it's not
possible but trying to know as much as possible.

1. Is Tomcat Source code available for free ?
2. How to run the container in a debugger mode ..... I'm new to
JAVA/J2EE .... I'll surely make an effort to know about it....
but if u could spare some time to tell me about that I'll be
grateful to u.


Thanks and Regards,
Dhairyashil Padalkar
 
T

Thomas Fritsch

crack_cs said:
Yes I want to about (2)... yes it surely violates the design goals of
the Servlet API but it's just for fun.I'm the kinda guy who want take a
deep dip understanding the overall working inside out..I know it's not
possible but trying to know as much as possible.

1. Is Tomcat Source code available for free ?
Yes, Tomcat is open-source. You can download the sources from
http://tomcat.apache.org/
2. How to run the container in a debugger mode ..... I'm new to
JAVA/J2EE .... I'll surely make an effort to know about it....
but if u could spare some time to tell me about that I'll be
grateful to u.
I recommend you familiarize yourself with remote debugging.

You can start Tomcat (like any other Java application) with a bunch of
cryptic start options
java -Xdebug -Xnoagent -Djava.compiler=NONE
-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=12345 ...
so that it later will accept connections (on port 12345) from a Java
debugger. Look up the docu of the "java" and/or "jdb" executables.
Then you can attach a debugger to this already running Java process. All
modern IDE's support this 'remote debugging'. Just look up your IDE's
docu and start experimenting (may be first with a simpler application
instead of with Tomcat).
I'm sure you'll get much enlightening about what is going on under the hood.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top