File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Problem with jspInit() overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Problem with jspInit() overriding" Watch "Problem with jspInit() overriding" New topic
Author

Problem with jspInit() overriding

Deepak Bobal
Ranch Hand

Joined: Feb 06, 2008
Posts: 96
I am using NetBeans6.1 with Tomcat 5.5

to make this program

i am overriding jspInit() method

and tries to fetch "email" parameter that is defined in web.xml

But it shows "null" in index.jsp

i m pasting the code....


...................web.xml..........................

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>
MyTestInit
</servlet-name>
<jsp-file>
/index.jsp
</jsp-file>
<init-param>
<param-name>email</param-name>
<param-value>Deepak@java.com</param-value>
</init-param>

</servlet>

</web-app>

..................index.jsp......................................

<%--
Document : index
Created on : Dec 13, 2008, 1:54:27 PM
Author : Administrator
--%>

<%@page import="javax.servlet.http.*,javax.servlet.*,java.io.*" contentType="text/html" pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>JSP Page</title>
</head>
<body>

<h2>JSP INIT</h2>
<%! String eMail; %>
<%!
public void jspInit()
{

ServletConfig sConfig=getServletConfig();
eMail=sConfig.getInitParameter("email");
System.out.println("Helloooo"+eMail);
}
%>
<%
out.println("Email Id is "+eMail); //Shows
%> //null


</body>
</html>



Please Rectify it...

if somewhere i am wrong

Thank You

Deepak Bahubal


Constant dripping hollows out a stone....
Devasani Naresh
Greenhorn

Joined: Sep 20, 2007
Posts: 18
Have you overridden correct method.
is the signature of the jspInit method is correct?
check if it is like the following.

publc void _jspInit()
Deepak Bobal
Ranch Hand

Joined: Feb 06, 2008
Posts: 96
Naresh Thanks for your response

if you see,i have one

to check if it's being called or not

and it shows it is called

i did that also you suggested me

But still i am getting null
agila arvind
Greenhorn

Joined: Sep 18, 2008
Posts: 7
Hi
This code will work , if you add a <servlet-mapping> tag in your web.xml as below
<servlet-mapping>
<servlet-name>MyTestInit</servlet-name>
<url-pattern>/index.jsp</url-pattern>
</servlet-mapping>
Deepak Bobal
Ranch Hand

Joined: Feb 06, 2008
Posts: 96
Arvind as per your suggestion i converts web.xml

as

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>

<servlet-name>MyTestInit</servlet-name>
<jsp-file>/index.jsp</jsp-file>
<init-param>
<param-name>email</param-name>
<param-value>Deepak@java.com</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>MyTestInit</servlet-name>
<url-pattern>/index.jsp</url-pattern>
</servlet-mapping>

</web-app>


But still it's showing email as null.
Kiril Nugmanov
Ranch Hand

Joined: Jul 09, 2008
Posts: 42
Hi Deepak Bahubal ,

I think error is that variable eMail is out of page scope. Try to add it to servletcontext scope for example.

PS: I'm not sure about correctness.
[ December 15, 2008: Message edited by: Kiril Nugmanov ]

SCJP5, SCWCD5, SCBCD5, OCPJWSD5, OCMJEA 5
Deepak Bobal
Ranch Hand

Joined: Feb 06, 2008
Posts: 96
Hi Kiril

i did the same as you suggested

now

.................index.jsp i..................


<%--
Document : index
Created on : Dec 13, 2008, 1:54:27 PM
Author : Administrator
--%>

<%@page import="javax.servlet.http.*,javax.servlet.*,java.io.*" contentType="text/html" pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>JSP Page</title>
</head>
<body>

<h2>JSP INIT</h2>
<%! String eMail;
ServletContext ctx; %>
<%!
public void jspInit()
{

ServletConfig sConfig=getServletConfig();
eMail=sConfig.getInitParameter("email");
ctx=getServletContext();
ctx.setAttribute("MyMail", eMail);
System.out.println("Helloooo"+eMail);
}
%>
<%
out.println("Email Id is "+ctx.getAttribute("MyMail"));
%>


</body>
</html>


But still it's showing null...
Kiril Nugmanov
Ranch Hand

Joined: Jul 09, 2008
Posts: 42
There is no such ctx impicit variable in Servlet. To access servet Context use application implicit variable. Note that your variable that are defined in jspInit are not accessable for the page. Only if you use implicit variable you can access them (or javaBeans). Example could be following:
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Verify that you are accesing the JSP by its given name or mapped URL. What URL are you using to accesing it?


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Deepak Bobal
Ranch Hand

Joined: Feb 06, 2008
Posts: 96
Hi Bosun

i have my project with the name"JspInit"

and using following url to access it

http://localhost:8080/JspInit/web/index.jsp
agila arvind
Greenhorn

Joined: Sep 18, 2008
Posts: 7
if you are accessing it with url http://localhost:8080/JspInit/web/index.jsp , then your servlet mapping must be
<servlet-mapping>
<servlet-name>MyTestInit</servlet-name>
<url-pattern>/web/index.jsp</url-pattern>
</servlet-mapping>

did you check this ?
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Originally posted by Deepak Bahubal:

and tries to fetch "email" parameter that is defined in web.xml



then you can fetch it from

-----------------------------------------------------
<%= config.getInitParameter("email") %> right? why you try to override the jspInit ?

------------------------------------------------------------
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

more over you need to check url-pattern of web.xml .can you please post your servlet-mapping part /
Deepak Bobal
Ranch Hand

Joined: Feb 06, 2008
Posts: 96
I am pasting web.xml and index.jsp

-----------------------web.xml-------------------

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>

<servlet-name>MyTestInit</servlet-name>
<jsp-file>/web/index.jsp</jsp-file>
<init-param>
<param-name>email</param-name>
<param-value>Deepak@java.com</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>MyTestInit</servlet-name>
<url-pattern>/index.jsp</url-pattern>
</servlet-mapping>

</web-app>


-------------------------------index.jsp----------------------


<%--
Document : index
Created on : Dec 13, 2008, 1:54:27 PM
Author : Administrator
--%>

<%@page import="javax.servlet.http.*,javax.servlet.*,java.io.*" contentType="text/html" pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>JSP Page</title>
</head>
<body>

<h2>JSP INIT</h2>
<%! String eMail;
ServletContext ctx; %>
<%!
public void jspInit()
{

ServletConfig sConfig=getServletConfig();
eMail=sConfig.getInitParameter("email");
ctx=getServletContext();
ctx.setAttribute("MyMail", eMail);
System.out.println("Helloooo"+eMail);
}
%>
<%
out.println("Email Id is "+application.getAttribute("MyMail"));
%>



</body>
</html>
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Are you getting null in both the console output and the output of the page?? If yes then I would suggest that you modify the value of the parameter. Just remove the @ symbol and then try as it might not be allowed to use special characters inside parameter values. This is just a guess so it may not work...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Deepak Bobal
Ranch Hand

Joined: Feb 06, 2008
Posts: 96
Ankit

This guess doesn't work too.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Originally posted by Ankit Garg:
Just remove the @ symbol and then try as it might not be allowed to use special characters inside parameter values.


not a problem Ankit


and deepak , you are using /inex.jsp and /web/index.jsp ?...
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

does this also produces null??

out.println("Email Id is "+application.getAttribute("MyMail"));
Deepak Bobal
Ranch Hand

Joined: Feb 06, 2008
Posts: 96
Now the web.xml is


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>

<servlet-name>MyTestInit</servlet-name>
<jsp-file>/web/index.jsp</jsp-file>
<init-param>
<param-name>email</param-name>
<param-value>Deepakjava.com</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>MyTestInit</servlet-name>
<url-pattern>/web/index.jsp</url-pattern>
</servlet-mapping>

</web-app>

but still not getting the result
Deepak Bobal
Ranch Hand

Joined: Feb 06, 2008
Posts: 96
yes Ankit


it's still giving null.

i wish at least you try once
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

OK. Let me give it a shot. Where is your JSP page?? Just do one thing, put the JSP file in the application root folder i.e. just side by side to web-inf folder. Now use this web.xml



Then just call the webapp with it's name. Eg if the context root of the web-app is myApp, then access it using localhost:8080/myApp/ . Now I think it will work...
[ December 16, 2008: Message edited by: Ankit Garg ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with jspInit() overriding