user1746199
user1746199

Reputation:

How to retrieve init parameter in jsp

I try to get url data using jsp, for that i used jsp code as

  String Url = pageContext.getServletConfig().getInitParameter("url").toString();
 out.println(Url);  

web.xml

<servlet>
<servlet-name>IploginJSP</servlet-name>
<jsp-file>/Iplogin.jsp</jsp-file>
    <init-param>
    <param-name>url</param-name>
    <param-value>http://xxx.xxx.xxx.xxx:9000</param-value>
    </init-param>
</servlet>
<servlet-mapping>
<servlet-name>IploginJSP</servlet-name>
<url-pattern>/Iplogin</url-pattern>
</servlet-mapping>

But i shows NullPointerException

Upvotes: 9

Views: 24991

Answers (2)

Rustem Zhunusov
Rustem Zhunusov

Reputation: 41

It's generally accepted that in JSP we should to use EL rather than scriptlet. So this version with EL in JSP file:

<%@ taglib prefix='c' uri='http://java.sun.com/jsp/jstl/core' %>
<HTML>
<HEAD>
   <TITLE>Using of initParam</TITLE>
</HEAD>
<BODY >
    <c:set var="LANDING_DEFAULT_URL"  value="${initParam.LANDING_DEFAULT_URL}"/>
    <p>${LANDING_DEFAULT_URL}"</p> 
</BODY>

and web.xml:

<context-param>
  <param-name>LANDING_DEFAULT_URL</param-name>
  <param-value>https://xxx.xxx.xxx.com/</param-value>
</context-param>

Upvotes: 3

Bhushan
Bhushan

Reputation: 6181

I think this will solve your issue.

Please restart your server after making changes to the web.xml file.

web.xml

<servlet>
    <servlet-name>GetInitParam</servlet-name>
    <jsp-file>/GetInitParam.jsp</jsp-file>
    <init-param>
        <param-name>url</param-name>  
        <param-value>hello</param-value>  
    </init-param>
</servlet>
<servlet-mapping>  
    <servlet-name>GetInitParam</servlet-name>  
    <url-pattern>/GetInitParam.jsp</url-pattern>  
</servlet-mapping> 

GetInitParam.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>Example of getting init param</title>
</head>
<body>
<%!
    String url= null;
    public void jspInit() { 
        ServletConfig config = getServletConfig(); 
        url= config.getInitParameter("url");
    }
%>
<%
    System.out.println(url);
%>
</body>
</html>

Update 1

As you asked in your comments to access parameters in all jsp files. to access parameters in your all jsp files you have to set <context-param> in your web.xml

Put following lines in your web.xml

<context-param>
<param-name>param1</param-name>
<param-value>hello</param-value>
</context-param>

you can access this parameters in following way in your jsp file:

<% 
    String param1=application.getInitParameter("param1"); 
    System.out.println(param1);
%>

Update2

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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

<context-param>
<param-name>param1</param-name>
<param-value>hello</param-value>
</context-param>
</web-app>

JSP FILE

<%@ 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>Example of getting init param</title>
</head>
<body>
<%
    String param1=application.getInitParameter("param1"); 
    System.out.println(param1);
%>
</body>
</html>

Upvotes: 15

Related Questions