aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Initializing the jsp  query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Initializing the jsp  query" Watch "Initializing the jsp  query" New topic
Author

Initializing the jsp query

Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 356
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


SCJP 1.4, SCWCD1.4, OCA(1Z0-007)
Mat Williams
Ranch Hand

Joined: Jul 20, 2005
Posts: 215
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
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


Narendra Dhande
SCJP 1.4,SCWCD 1.4, SCBCD 5.0, SCDJWS 5.0, SCEA 5.0
Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 356
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
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
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
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
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.


SCJP 1.4<br />SCWCD 1.4
Ion Gherasim
Greenhorn

Joined: Jul 23, 2003
Posts: 25
OK!!!

Many thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Initializing the jsp query