How to read the Server Information

How To Read/Access The Server Information

Date: 05/02/2004

This document will help you understand:
•How to Read/Access the Server Information
•How to run the sample Servlet application 

Table of Contents

Introduction
Prerequisites
Software Requirements
How to Read/Access Server Information
Running the Sample Servlet Applications
Useful References

Introduction

Servlets provide a component-based platform-independent method for building web-based applications, and are generally used to generate dynamic web content. To put it simply, a servlet receives a request from a client (the web browser, for instance), processes the request, and returns the response to the client. For example, the response can be some content queried from a database that is displayed using HTML.

For more information about Servlets, refer to the Useful References section later in this document.

What is a Server Information ?

Web Applications are typically deployed in an application server. The application server sits between the client that accesses the application, and the data source from which the data could come.

The information about the server from a servlet can be retrieved using the ServletContext Object. The information like Server name, version is popularly used as the footer content in the web pages. The APIs used to get the server information like the server software, its name/IP Address and, the port at which requests can be reached are: getServletContext().getServerInfo() and request.getServerPort(). Code Snippets to use these APIs are detailed in the further section of the document.

Prerequisites

To work your way through this HowTo, it is necessary to have a basic understanding of the fundamental concepts of servlets, and how to develop and deploy them to a web server.

Software Requirements

  • Oracle10g JDeveloper or later ( JDeveloper is Oracle's Visual Java Development Tool and can be downloaded from here)

  • OR
    Oracle9AS Container for J2EE(OC4J) 9.0.3 or later. (Download from here
  • JDK1.3.x or later. (Download from here )

How To read/access the Server Information

This section provides the steps to read and access Server information within a servlet.

Server's information can be read from the ServletContext object. The name of the server and the port at which it is running can be obtained using the following APIs.

    getServerInfo(): Returns the information of the server software, for example if a servlet is deployed in OC4J, this returns the value like
    'Oracle Application Server Containers for J2EE (10g) 9.0.4.0.0'

    To read the value of the Server port, HttpRequest object is used.
    getServerPort(): Returns the port of the server at which the client requests can be sent to access an application running on the server.

      ......
      
      public void service(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException  {
            
        // Get Server's Name
          String name = getServletContext().getServerInfo();
             
         // Get Server port number
        String hostname  = request.getServerPort();
        
         System.out.println("Server's Name is: "+name);
         System.out.println("Server Port is: "+port);
         .....
      }                     
      ......               

Running the Sample Servlet Application

This section talks about how to get the servlet up and running. 

Running the Servlet Sample Application in OC4J (EAR Deployment):

    The sample application ServerInfo.jar can be downloaded from here. The Jar file extracts all the sample application files to a folder called ServerInfo. Go through the following steps to deploy and run the servlet in OC4J. 

    1. Add servlet.jar to the CLASSPATH; this file is present under <OC4J_HOME>/j2ee/home/lib folder.
      Note: <OC4J_HOME> is the folder where the OC4J server is installed; for example, d:\oc4j
    2. From the ServerInfo folder, compile the servlet file: GetServerInfo.java using the following command:
      javac src/oracle/otnsamples/servlets/*.java -d ./WEB-INF/classes
      - This creates the classes for the servlet files under the WEB-INF/classes/oracle/otnsamples/servlets folder.
    3. Next, create a WAR file containing all the servlet classes, JSPs, HTML files and the web.xml file using the JAR command:
      jar -cvfM ServerInfo.war WEB-INF/*

    4. Create a EAR file containg the application.xml file and WAR file using the JAR command again.
      jar -cvfM ServerInfo.ear ServerInfo.war META-INF
    5. Start the OC4J server.
    6. Deploy the servlet applications using the command below:

      java -jar <OC4J_HOME>/j2ee/home/admin.jar ormi://<server>:<rmiport> admin <admin_password> -deploy -file <Sample_Name>.ear -deploymentName <earDeploymentName>

      Example:
      java -jar D:/oc4j903/j2ee/home/admin.jar ormi://incq207a.idc.oracle.com:23791 admin welcome -deploy -file ServerInfo.ear -deploymentName ServerInfo

      Note:
      <server> is the IP Address of the system where the OC4J instance is running.
      <rmiport> is the RMI port number of the OC4J instance; default is 23791
      <admin_password> is the password of the OC4J installation
      <Sample_Name>.ear is the EAR file created, that is, ServerInfo.ear
      <earDeploymentName> is the deployment name for the application, that is, ServerInfo

    7. Bind the web application to a context root using the following command:

      java -jar <OC4J_HOME>/j2ee/home/admin.jar ormi://<server>:<rmiport> <admin_username> <admin_password> -bindWebApp <earDeploymentName> <Sample_War_Name> http-web-site /<website_root>

      Example:
      java -jar D:/oc4j903/j2ee/home/admin.jar ormi://incq207a.idc.oracle.com:23791 admin welcome -bindWebApp ServerInfo ServerInfo http-web-site /ServerInfo

    8. Access the servlets using the URLs like this:

      http://localhost:8888/ServerInfo/servlet/GetServerInfo
    9. Here, localhost is your machine where OC4J instance is running. 8888 is the default port of OC4J.

    10. This will invoke the servlet which displays the Server information like the name, its version and the port at which it is running.

Running the Servlet from Oracle JDeveloper 10g:

    The sample application ServerInfo.jar can be downloaded from here. The Jar file extracts all the sample application files to a folder called ServerInfo. Go through the following steps to run the servlet from JDeveloper.

    1. From JDeveloper, open the ServerInfo.jws workspace file. This will open the corresponding project and source files of the application.

    2. Compile the ServerInfo.jpr project.

    3. Click on the  GetServerInfo.java and run the file. 
    4. This will invoke the servlet which displays the Server information like the name, its version and the port at which it is running.

Useful References

Do enter your comments about this HowTo in the OTN Sample Code Discussion Forum.

How To Read the Server Information in Servlets

Please rate this how-to :
Excellent
Good
Average
Below Average
Poor
E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy