JSP Implicit Object config with example - Java @ Desk

Tuesday, May 6, 2014

JSP Implicit Object config with example

JSP Implicit Object config with example

In our last posts, we have learned
JSP implicit objects REQUEST with example,
JSP implicit objects OUT with example ,
JSP Implicit Object APPLICATION with example ,
JSP Implicit object session with example ,
JSP implicit object PageContext with example,
JSP implicit object RESPONSE with example

In JSP, config is an implicit object of class javax.servlet.ServletConfig. Like we can configure parameters for a Servlet in web.xml or deployment descriptor, in the same way we can configure parameters for particular JSP.

As configuration parameters for a Servlet belongs to that Servlet only, configuration parameters for a JSP page belongs to that JSP page only.

web.xml

 <servlet>
  <servlet-name>homeJSP</servlet-name>
  <jsp-file>/homeJSP.jsp</jsp-file>
  <init-param>
   <param-name>firstName</param-name>
   <param-value>John</param-value>
  </init-param>
 </servlet>

 <servlet-mapping>
  <servlet-name>homeJSP</servlet-name>
  <url-pattern>/homeJSP</url-pattern>
 </servlet-mapping>


homeJSP.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%   
String firstName = config.getInitParameter("firstName");  
out.print("First Name is - " + firstName);  
%>  
</body>
</html>


When you hit homeJSP.jsp, the config parameter firstName value will be displayed on the browser.






No comments:

Post a Comment