JSP - SPLessons

Chapter 16

JSP Config Object

JSP Config Object

The JSP Config Object is created by the JSP container while translating the JSP page to Servlet source to help JSP programmers. This JSP config object is used to get an information from the web.xml for the particular JSP page. The JSP Config Object has some methods which are used in Servlet. They are
Following is the difference between Config and Application.

  • Config is for a specific Servlet/JSP, the config parameter are unknown to other Servlet/JSP and it has Session Scope.
  • application( ServletContext ) parameter are specified for the entire application. These variables are access across the application.


Following is an example.

jspconfig.html

<html>
   <body>
<h2><a href="./welcome">click here</a></h2>
</body>
</html>

Here just created the button Click here, the URL name should be match with web.xml file URL.

web.xml

<web-app>  
  
   <servlet>  
       <servlet-name>Config</servlet-name>  
       <jsp-file>/configObject.jsp</jsp-file>  
  
         <init-param>  
            <param-name>user_name</param-name>  
            <param-value>SPlessons</param-value>  
         </init-param>  
   </servlet>  
  
   <servlet-mapping>  
      <servlet-name>Config</servlet-name>  
      <url-pattern>/welcome</url-pattern>  
   </servlet-mapping>  
  
</web-app>  

Here make sure that servlet name should be same that is Config, URL should be match with HTML form, parametes are passing from the web.xml file by using the tag .
configObject.jsp

<h2>
<% String userName = config.getInitParameter("user_name"); 
out.println("your name get from web.xml:"+userName); %>
</h2>

Here name will be retrieved from the web.xml page that is SPlessons and that will be printed using out.println.

Output

Output will be as follows, where click on the click here button that was created in main page jspconfig.html .

After clicking on the button output will be as follows with the name that was mentioned in the web.xml .

Summary

  • It is an implicit object, utilized for getting setup data for a specific JSP page.
  • JSP Config Object is an object of javax.servlet.ServletConfig.
  • web.xml is used to access the init parameters of the servlet.