Saturday 14 March 2015

jsp Context parameters


Just like initialization parameters, you can specify context parameters in web.xml. Only difference is initialization parameters are specific to particular Servlet/jsp, where as Context parameters are available to all the servlets/jsps in your web application.

GetData.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Servlet Config</title>
    </head>
    <body>
        <form method="get" action="/jsp/JspInit">
            <input type="submit" value="Click Me">
        </form>
    </body>
</html>


GetBook.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>
            Author : <%= application.getInitParameter("Author") %> <br />
            Book : <%= application.getInitParameter("Book") %> <br />
            Blog : <%= application.getInitParameter("URL") %>             
        </h1>
    </body>
</html>


web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
        <servlet-name>JspInit</servlet-name>
        <jsp-file>GetBook.jsp</jsp-file>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>JspInit</servlet-name>
        <url-pattern>/JspInit</url-pattern>
    </servlet-mapping>
    
    <context-param>
        <param-name>Author</param-name>
        <param-value>Krishna</param-value>
    </context-param>
        
    <context-param>
        <param-name>Book</param-name>
        <param-value>Self Learning Java</param-value>
    </context-param>
        
    <context-param>
        <param-name>URL</param-name>
        <param-value>self-learning-java-tutorial.blogspot.com</param-value>
    </context-param>  
</web-app>


Output




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment