How To Get Servlet Init Parameters In JSP

How To Get Servlet Init Parameters In JSP

We know that we can declare init parameters for JSP file in web.xml file as we do in the case of servlet.The entry in the web.xml file will be as follows:

<servlet>
    <servlet-name>myJSP</servlet-name>
    <jsp-file>/MyJsp.jsp</jsp-file>
    <init-param>
            <param-name>paramName</param-name>
            <param-value>paramValue</param-value>
     </init-param>
</servlet>
<servlet-mapping>
     <servlet-name>myJSP</servlet-name>
      <url-pattern>/MyJsp.jsp</url-pattern>
</servlet-mapping>

Now comes the question that how can be access or retrieve those initialization parameters in the JSP page.Among the life cycle methods of a JSP page jspInit() method is one of the methods which can be overridden.The jspInit() method is called by the init() method of the servlet which is the result of the translation of the JSP page.To access the initialization parameters is one of the cases where we override the jspInit() method.When we need to declare a method we use the declarative tag as follows :

<%!
 public void jspInit()
 {
   ServletConfig sConfig = getServletCOnfig();
   String str= sConfig.getInitParameter(“paramName”);
   getSerrvletContext().setAttribute(“name”,str);
 }
%>

Here we have overridden the jspInit() method in the declarative tag.Since this method is called by the init method of the translated servlet code we can access the servlet config object with the help of getServletConfig() method as it is passed by the server to the servlet through init() method. This method (jspInit()) will appear as an overridden method in the translated servlet.The point should be noted here that init() method in the servlet calls this jpInit() life cycle method of the  jsp page so it can access the servlet config object.The life cycle method _jspService() is not permitted to be overridden .Initialization parameters for the the JSP page can be understood as the configuration parameters to be accessed by the translated servlet.After all a JSP page is a servlet at the end of the day and it is the translated servlet only which processes the requests from the clients.

 

 

 

Number of View :1297

Tags: , , , , , , , , , , ,

  1. No comments yet.

SetPageWidth
Content Protected Using Blog Protector By: PcDrome.

Mahol Dot Org is Stephen Fry proof thanks to caching by WP Super Cache