Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Problem with jspInit() overriding

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Deepak Bobal
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Ranch Hand
Posts: 1512
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Verify that you are accesing the JSP by its given name or mapped URL. What URL are you using to accesing it?
 
Deepak Bobal
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
more over you need to check url-pattern of web.xml .can you please post your servlet-mapping part /
 
Deepak Bobal
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>
 
Sheriff
Posts: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Deepak Bobal
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit

This guess doesn't work too.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
does this also produces null??

out.println("Email Id is "+application.getAttribute("MyMail"));
 
Deepak Bobal
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes Ankit


it's still giving null.

i wish at least you try once
 
Ankit Garg
Sheriff
Posts: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
We can fix it! We just need some baling wire, some WD-40, a bit of duct tape and this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic