The HttpServlet and GenericServlet classes provide methods to access information and log messages. The following tables describes these methods:
You can get initialization parameters from individual servlet definitions (using the ServletConfig object) or from the ServletContext which provides parameters for all servlets within an application.
To get servlet-specific initialization parameters, use the getInitParameterNames and getInitParameter methods of the ServletConfig object, as the following example shows:
...
ServletConfig config = getServletConfig();
Enumeration eParmNames = config.getInitParameterNames();
while (eParmNames.hasMoreElements()) {
String parm = (String) eParmNames.nextElement();
out.println(" " + parm + ": " + config.getInitParameter(parm) + "<br>");
}
...
Servlet-specific initialization parameters are set in the servlet definition in the web application's deployment descriptor, as the following example shows:
<web-app>
... <servlet> <servlet-name>ConfigParams</servlet-name> <servlet-class>ConfigParams</servlet-class> <init-param> <param-name>datasourcename</param-name> <param-value>lightgoldenrodyellow</param-value> </init-param> </servlet> ... </web-app>
For more information, see "Using the ServletConfig object".
To get initialization parameters that are shared by all servlets in an application use the ServletContext object's getInitParameter and getInitParameterNames methods, as the following example shows:
ServletContext context = this.getServletContext();
Enumeration parmEnum = context.getInitParameterNames();
if (parmEnum.hasMoreElements()) {
out.println("<h2>ServletContext Init Parameters</h2>");
}
while (parmEnum.hasMoreElements()) {
String name = (String)parmEnum.nextElement();
out.println("<b>"+name+": </b>");
out.println(context.getInitParameter(name) + "<br>");
}
...
Application-wide initialization parameters are set in the web application's deployment descriptor, outside of servlet definitions, as the following example shows:
<web-app>
... <context-param> <param-name>datasourcename</param-name> <param-value>fred</param-value> </context-param> ... </web-app>
You use the ServletContext object to store information about your application and to share information among the various components of the application.
For example, your application might consist of multiple servlets (written in Java and as JSPs), HTML tags, and databases. For the various components of the application to communicate information, you can use the application context to store and retrieve that information. Information available through the context object includes the following:
An application context also stores information about the application as it is implemented on the web server. This information includes the file location of the components of the application, servlet initialization parameters, version information, and other application-specific information.
You can access application information in a servlet and a JSP:
javax.servlet.ServletContext object.
object. The ServletContext object enables you to store information about your application and access environment information, such as the following:
Servlets use the getServletContext method to obtain a reference to the ServletContext object.
The following example displays selected servlet context information:
...
ServletContext scntxt = this.getServletContext();
out.println("Server information:" + scntxt.getServerInfo() + "<br>");
int majorVersion = scntxt.getMajorVersion();
int minorVersion = scntxt.getMinorVersion();
out.println("Major version: " + majorVersion + "<br>");
out.println("Minor version: " + minorVersion + "<br>");
java.util.Enumeration parmEnum = scntxt.getInitParameterNames();
if (parmEnum.hasMoreElements()) {
out.println("<h2>ServletContext Parameters</h2>");
}
while (parmEnum.hasMoreElements()) {
String name = (String)parmEnum.nextElement();
out.println("<b>"+name+": </b>");
out.println(scntxt.getInitParameter(name) + "<br>");
}
// ServletContext attributes
java.util.Enumeration attrEnum = scntxt.getAttributeNames();
if (attrEnum.hasMoreElements()) {
out.println("<h2>ServletContext Attributes</h2>");
}
while (attrEnum.hasMoreElements()) {
// Always cast attributes to appropriate class
String attrName = (String)attrEnum.nextElement();
out.println("<b>" + attrName + ": </b>");
out.println(scntxt.getAttribute(attrName) + "<br>");
}
// Get real path for servlet
String path = req.getServletPath();
out.println("<b>Full servlet path: </b>");
out.println(scntxt.getRealPath(path) + "<br>");
// Write to log through ServletContext
Date now = new Date();
scntxt.log("Testing ServletContext: " + now);
...
To see a sample servlet, start the samples JRun server and open a browser to http://localhost:8200/techniques.
JRun passes configuration information to a servlet when it is initialized. This configuration information includes name/value pairs, which describe initialization parameters, and the ServletContext object, which describes the context within which the servlet is running.
Servlets use the getServletConfig method to obtain a reference to the ServletConfig object. You can then use the ServletConfig object to access initialization parameters.
You can access configuration information in a servlet and a JSP:
javax.servlet.ServletConfig object.
The following example shows using the getServletConfig method to get the ServletConfig object in a servlet's init method:
...
public void init(ServletConfig config) throws ServletException {
super.init(config);
try {
bgcolor = config.getInitParameter("bgcolor");
System.out.println("bgcolor: " + bgcolor);
} catch (Exception e) {
System.out.println("error: " + e.toString());
}
}
...
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
out.println("<html><head><title>Servlet Config</title>");
out.println("</head><body bgcolor=\"" + bgcolor + "\">");
out.println("<h1>Servlet Config</h1>");
out.println("</body></html>");
}
...
Note: You must restart the JRun server so the init method is called again. It is only called once when the server starts.
The following excerpt from the web.xml file sets the initialization parameter:
<servlet>
<servlet-name>GetServletConfigInfo</servlet-name> <servlet-class>GetServletConfigInfo</servlet-class> <init-param> <param-name>bgcolor</param-name> <param-value>lightgoldenrodyellow</param-value> </init-param> </servlet>
To see a sample servlet, start the samples JRun server and open a browser to http://localhost:8200/techniques.
The log method writes a programmer-specified message to the log file of the JRun server hosting the servlet.
Note: JRun automatically logs varying amounts of information. For more information about log file settings, see the JRun Administrator's Guide or the JMC.
The following example logs user-access information:
HttpSession thisSession = req.getSession();
String userName = (String)thisSession.getAttribute("name");
if(userName != null) {
out.println("<h2>Welcome " + userName + "</h2>");
// Prepare information for logging.
// This example logs user name and IP address.
String logMsg = userName + ", " + req.getRemoteAddr();
log(logMsg);
}
Just as in any Java application, you can make a call to the System object to get the properties. This gives you access to the user language, time zone, region, file separator, and many other properties that help you make your web application platform-independent. These properties also give you access to some of the services used by JRun, and identify the naming factory, directory structure, and classpath used by the JVM.
The following example lists all the System properties in a table and then accesses the user.region property to provide information to the client about the location of the JRun server:
...
Properties sysprops = System.getProperties();
...
out.println("<TABLE>");
Enumeration enum = sysprops.propertyNames() ;
while (enum.hasMoreElements()) {
out.println("<TR><TD>");
String key = (String) enum.nextElement();
out.println(key + "</TD><TD>" + sysprops.getProperty(key) + "</TD></TR>");
}
out.println("</TABLE>");
...
String region = sysprops.getProperty("user.region");
out.println("<HR>This server is hosted in: <B>" + region + "</B>");
...
To see a sample servlet, start the samples JRun server and open a browser to http://localhost:8200/techniques.