Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question--Initializing JSP

 
Anna Wang
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • 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
 
Gaurav Gambhir
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Related Thread
 
Karne Reddy
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thank you very much for the answers. It is very clear.

Anna
 
salil verma
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic