Thursday, 13 November 2014

jsp config implicit object

config is an implicit object of type ServletConfig.
This object is also per jsp page.
This object is used to get the initialization parameters declared in the web.xml file(Deployment Descriptor).
Example
index.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 user=config.getInitParameter("User"); 
out.print("Hello "+user); 
%> 
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>Config Example</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
  <servlet-name>config</servlet-name>
  <jsp-file>/index.jsp</jsp-file>
  <init-param>
    <param-name>User</param-name>
    <param-value>Prince</param-value>
  </init-param>
  </servlet>
  <servlet-mapping>
  <servlet-name>config</servlet-name>
  <url-pattern>/config.do</url-pattern>
  <url-pattern>/config.jsp</url-pattern>
  </servlet-mapping>
</web-app>
Output