For the complete c ode o f LoadI n i tServl et. j ava,
ShowInitLoaded1.jsp, and ShowInitLoaded2. jsp, see Listings 2.12 through 2.14,
respectively. Figures 2??“15 through 2??“17 show the result of invoking LoadInit-
Servlet (with
/showLoadInit),
ShowInitLoaded1.jsp, and ShowInitLoaded2.jsp, respectively.
Listing 2.12 LoadInitServlet.java
package coreservlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/** Simple servlet used to illustrate loading init-param
* into ServletContext.
*/
public class LoadInitServlet extends HttpServlet {
private String companyName = "Company name is missing";
public void init() {
ServletConfig config = getServletConfig();
if (config.getInitParameter("companyName") != null) {
companyName = config.getInitParameter("companyName");
}
ServletContext context = getServletContext();
context.setAttribute("companyName", companyName);
}
2.6 Initializing and Preloading Servlets and JSP Pages 65
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println(""Transitional//EN\">" + "\n" +
"\n" + "
" +
"Load Init Servlet" + "\n" +
"\n" +
"
Init Parameter:
\n" +
"Company name: " +
getServletContext().
Pages:
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103