Saturday 14 March 2015

jsp Initialization Parameters


You can pass the initialization parameters to a jsp page using web.xml. The initialization parameters of the jsp are accessed to the same jsp, not from other jsp files.
you need a servlet mapping, When you're dealing with a JSP as a Servlet.

<servlet-name>JspInit</servlet-name>
<jsp-file>GetBook.jsp</jsp-file>

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 : <%= config.getInitParameter("Author") %> <br />
            Book : <%= config.getInitParameter("Book") %> <br />
            Blog : <%= config.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>
        
        <init-param>
            <param-name>Author</param-name>
            <param-value>Krishna</param-value>
        </init-param>
        
        <init-param>
            <param-name>Book</param-name>
            <param-value>Self Learning Java</param-value>
        </init-param>
        
        <init-param>
            <param-name>URL</param-name>
            <param-value>self-learning-java-tutorial.blogspot.com</param-value>
        </init-param>  
    </servlet>
    
    <servlet-mapping>
        <servlet-name>JspInit</servlet-name>
        <url-pattern>/JspInit</url-pattern>
    </servlet-mapping>
</web-app>


Output
 
'config' is implicit object provided by JSP container.

Related Links
https://self-learning-java-tutorial.blogspot.com/2015/03/jsp-implicit-objects.html

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment