public void jspInit(){ ServletConfig config = getServletConfig(); ServletContext context = getServletContext(); name = config.getInitParameter("name"); } %>
<%=name%>
can someone tell me why is that the two results are different.
Thnx in advace
SCJP 1.4, SCWCD1.4, OCA(1Z0-007)
Mat Williams
Ranch Hand
Joined: Jul 20, 2005
Posts: 215
posted
0
Hi,
I am not 100% on this so maybe someone else can comment.
However if you use the URL in example 1 http://localhost:8080/scwcd/Tomcat then you are forcing the container to go to the DD and see where it should direct the request to.
If on the other hand (and this is where I am not 100% sure) you type in the url as per example 2 http://localhost:8080/scwcd/HFSJ/ch07/TestInit.jsp it is up to the container, and in this case the container thinks it has enough information to direct you query straight to the requested resource 'TestInit.jsp' and therefore doesn't need to go to the DD thus the init-param name/value pair is not given to the jsp after it is compliled.
I have the feeling that I have read somewhere this is what happens.
Just out of curiosity if you go to the url in example 1 and then without restarting Tomcat (I assume that Tomcat is the container you are using) go to the second url you typed in - what happens?
Hope this helps.
Matt
Narendra Dhande
Ranch Hand
Joined: Dec 04, 2004
Posts: 950
posted
0
Hi,
Both the URLs are handled by diff. Servlet instance. When you call the JSP with Tomcat it served by the servlet instance which have a init parametres as per DD. When you call the JSP with original name, it will serve by diif. instance which do not have init parameters.
Hi Narendra, I know that a jsp gets translated into servlet, but i am not aware of the different instance of a jsp trying to serve the request. I have compared the java code generated by tomcat in the case of url1 and url2 and the codes are same.
If two different instance are serving the request, how they are different. can you tell more on this !
N C
Narendra Dhande
Ranch Hand
Joined: Dec 04, 2004
Posts: 950
posted
0
Hi,
The code for the class are not different. The generated servlet class is same, but the container create multiple instances for the same class. In your case one is for the <servlet> defination and another for implicit .jsp defination.
Thanks
Ion Gherasim
Greenhorn
Joined: Jul 23, 2003
Posts: 25
posted
0
Hello, It is very nice. I have one question: what is on RequestDispatcher's forward? Is it used the DD?
Thanks, Ion
Narendra Dhande
Ranch Hand
Joined: Dec 04, 2004
Posts: 950
posted
0
Hi,
It id sepend on how the RequestDispatcher object is constructed. If you are using the URI mapped as definded in DD, it will use the Servlet instance with init param. If you are using URI using directly JSP file it will use the instance without init params.
Basically two (not considering distributed / SingleThread models) instances are craeted as you have to pass two different ServletConfig objects to init method.One having the init parameters and one without it.
Hope it help you.
Thanks
Osama Hasan
Ranch Hand
Joined: Sep 30, 2002
Posts: 53
posted
0
Hi all! I think that the servlet container cannot create multiple instances of a servlet but it can run multiple threads on the same instance. Please correct me if I am wrong.