• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

access the servlet init config parameters using jspInit()

 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have overridden jspInit() to access the servlet init config parameters of the Jsp's generated servlet
<%!
public void jspInit() {
System.out.println("Inside jspInit()");
ServletConfig config = getServletConfig();
ServletContext context = getServletContext();
context.log("Start: jspInit() ");
context.log("JSP:"+config.getInitParameter("JSP_SERVLET_CONFIG_PARAM"));
System.out.println("JSP:"+config.getInitParameter("JSP_SERVLET_CONFIG_PARAM"));
context.log("End: jspInit() ");
}
%>

and the DD:
<servlet>
<servlet-name>HobbyFriends</servlet-name>
<jsp-file>/web/hobbyFriends.jsp</jsp-file>
<init-param>
<param-name>JSP_SERVLET_CONFIG_PARAM</param-name>
<param-value>JSP_SERVLET_CONFIG_VALUE</param-value>
</init-param>
</servlet>

Still the output is JSP:null . Everythign is fine, there are no exceptions anywhere @ anytime jst the output is null.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
depak.. one thing here.. to get that servletcontext dont we need to use like. because you already got the config object there.

actually if we directly user getservletcontext .. this will give the generic context isn't it?..
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friend, It is not possible to use servlet init parameters until the servlet is initialized. As because init or jspInit method is part of initialization we can not use init parameters. Please correct me if i am wrong.

Thanks
Afzal
 
Sarat Koduri
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It is not possible to use servlet init parameters until the servlet is initialized


afzal..i am afraid you should put it in the other way.. i mean,

Init parameters are meant to be used during initialization of the servlet and ofcourse we can use the init parameters inside the init method.

for example i want to connect to database and i wanted it to be done before the initialisation of the servlet then commonly we put those db parameters as servlet init parameters and override the init method of the servlet and do the connection here.. hope you got it...
 
Afzal Hossain
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sarat Koduri , yes you are right. Thanks for the explanation.
 
Afzal Hossain
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi sarat, I have run your code ... the problem is you did not use any mapping for the hobbyFriend.jsp file. It only works if you use any mapping instead of directly calling the jsp or servlet name. I hope you understand the point.
Thanks
Afzal
 
Sarat Koduri
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
afzal.. its deepak who wrote the code .. and i didn't .!! any way i want to highlight one more point here, whether we call that jsp directly or not the jsp init method got overriden so i guess it should be able to access the initialisation parameters. what do you say..!!!
[ September 18, 2008: Message edited by: Sarat Koduri ]
 
Afzal Hossain
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Deepak, As because you didn't use mapping, container creates 2 different instances of the generated servlet class one for accessing it as a named servlet and one for accesing it as a JSP page and will pass each servlet instance a different ServletConfig object. In order to be able to use the same instance and hence same configuration we have to explicitly map the JSP page's url as follows:
<servlet>
<servlet-name>HobbyFriends</servlet-name>
<jsp-file>/web/hobbyFriends.jsp</jsp-file>
<init-param>
<param-name>JSP_SERVLET_CONFIG_PARAM</param-name>
<param-value>JSP_SERVLET_CONFIG_VALUE</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>HobbyFriends</servlet-name>
<url-pattern>/web/hobbyFriends.jsp</url-pattern>
</servlet-mapping>

Now container will create only one instance of the generated servlet class and request for both the URLS will be served by the same instance, thus generating the same configuration.( i am quoting from Hanumant's book).

Hope this info will help you deepak.

Sorry sarat, it was for deepak.. And yes jspInit method is overriden.
Thanks
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic