• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Initializing your JSP

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greetings Ranchers!

I�ve run through some confusion in Overriding jspInit() method. I am using the Head First book. I�ve followed the instruction in the book in configuring the servlet init parameter then inserted the <jsp-file> element in th DD.

<servlet>
<servlet-name>MyTestInit</servlet-name>
<jsp-file>/TestInit.jsp</jsp-file>
<init-param>
<param-name>email</param-name>
<param-value>djabonete@gmail.com</param-value>
</init-param>
</servlet>

Then my jsp:

<%!
public void jspInit() {

ServletConfig sConfig = getServletConfig();
java.util.Enumeration e = sConfig.getInitParameterNames();
while(e.hasMoreElements()) {
System.out.println("sConfig = " + e.nextElement().toString());
}
String emailAddr = sConfig.getInitParameter("email");

ServletContext ctx = getServletContext();
ctx.log("emailAddr = " + emailAddr);
ctx.setAttribute("mail", emailAddr);

System.out.println("jspInit has been Initialized");
System.out.println("ctx.getAttribute(\"mail\") = " + ctx.getAttribute("mail"));
}
%>
<html>
<head>
<title>JSP Init Test Page</title>
</head>
<body>
<%= getServletContext().getAttribute("mail") %>
</body>
</html>

I did get the �mail� attribute sets in the Context. I would really appreciate your help. Thank you very much!
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The easiest way to test this would be to copy this line:
<%= getServletContext().getAttribute("mail") %>
to a new JSP and hit it after hitting your other one.
 
Daniel Jabonete
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it!

It also need <servlet-mapping> in the DD;

<servlet-mapping>
<servlet-name>MyTestInit</servlet-name>
<url-pattern>MyTestInit.jsp</url-pattern>
</servlet-mapping>

Cheers!
reply
    Bookmark Topic Watch Topic
  • New Topic