Implicit Objects in JSP, application implicit object in JSP, How to get Value from the XML configuration file


Implicit Objects in JSP, application implicit object in JSP, How to get Value from the XML configuration file


application is an object of ServletContext class in Java Servlet, It is used to get the configuration information from an xml configuration file to all JSP pages. for example if we have an XML configuration file in our application and we have define the database connection string into the <context-param> tag and then we want to access the value of the variables which are define into the <context-param> then we are used the config.getInitParameter(String param) method of the application object.

Example:-

web.xml

<web-app>
<servlet>
<servlet-name>talibhassan</servlet-name>
<jsp-file>/hello.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>talibhassan</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>

<context-param>
<param-name>user</param-name>
<param-value>Talib Hassan</param-value>
</context-param>
</web-app>

hello.jsp

<html>
<head>
<title>Hello User</title>
</head>
<body>
<%
String user=application.getInitParameter("user");
%>
<h1>Welcome <%=user%></h1>
</body>
</html>
</html>


In above example we are using the <context-param> variable value into single JSP page but the real use of this object is to define the global values for entire application.

{ 0 comments... read them below or add one }

Post a Comment