• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Initializing the jsp query

 
Naresh Chaurasia
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a query regarding Initializing the jsp(P 308).

I have used the following servlet mapping in web.xml


<servlet>
<servlet-name>Tomcat</servlet-name>
<jsp-file>/HFSJ/ch07/TestInit.jsp</jsp-file>
<init-param>
<param-name>name</param-name>
<param-value>chaurasia</param-value>
</init-param>
</servlet>


<servlet-mapping>
<servlet-name>Tomcat</servlet-name>
<url-pattern>/Tomcat/*</url-pattern>
</servlet-mapping>



If i am calling the resource (TestInit.jsp) using http://localhost:8080/scwcd/Tomcat, then i am getting result as expected, but if i am calling the same resource using http://localhost:8080/scwcd/HFSJ/ch07/TestInit.jsp then i am getting the value of name as null.


code for TestInit.jsp

<%!String name;

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
 
Mat Williams
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Hope it help you

Thanks
 
Naresh Chaurasia
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ion Gherasim
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK!!!

Many thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic