• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

question--Initializing JSP

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,everyone,

In HFSJ (p. 308), about initializing JSP, I am not very clear about that. How can I do servlet initialization stuff in JSP.

I wrote part of the codes, can anybody add more to finish it.

From the book and errara.
The web.xml is:
-----------------------------
<web-app 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/web-app_2_4.xsd" version="2.4">

<servlet>
<servlet-name>JspInit</servlet-name>
<jsp-file>/TestInit.jsp</jsp-file>
<init-param>
<param-name>email</param-name>
<param-value>annawang@yahoo.com</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>MyTestInit</servlet-name>
<url-pattern>/TestInit.jsp</url-pattern>
</servlet-mapping>
</web-app>
-------------------------------

TestInit.jsp is:
----------------------------
<html>
<body>

<%!
public void jspInit() {
ServletConfig sConfig = getServletConfig();
String e = sConfig.getInitParameter("email");
ServletContext ctx = getServletContext();
ctx.setAttribute("mail", e);
}
%>

The email address is: ???

</body>
</html>

-----------------
How to write next to output: annawang@yahoo.com?

Thank you very much.

Anna
 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Related Thread
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anna,
U can print the email by
${applicationScope.mail}

or

If u have not ovverrided the jspInit() method , u can display it
by <%=config.getInitParameter("email")%>
(config is implicit varialble)
 
Anna Wang
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for the answers. It is very clear.

Anna
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Karne,
applicationScope is a map of attribures of context so ${applicationScope.mail} would not work for printing the config parameters.
although <%=config.getInitParameter("email")%> will fairely work
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic