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
Oracle9i AS Container for
J2EE(OC4J) 9.0.3 or later. (Download from here
)
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.
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
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.
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/*
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
Start the OC4J server.
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>
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
Bind the web application to a context root
using the following command:
Here, localhost
is your machine where OC4J instance is running. 8888 is the default
port of OC4J.
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.
From JDeveloper, open the ServerInfo.jws workspace file. This will open
the corresponding project and source files of the application.
Compile the ServerInfo.jpr project.
Click on the GetServerInfo.java and run the file.
This will invoke the servlet which
displays the Server information like the
name, its version and the port at which it is running.