This chapter explains how to administer web applications in the Sun GlassFishTM Enterprise Server v3 environment.
The following topics are addressed here:
Instructions for accomplishing some of these tasks by using the Administration Console are contained in the Administration Console online help.
You can call a servlet deployed to Enterprise Server by using a URL in a browser or embedded as a link in an HTML or JSP file. The format of a servlet invocation URL is as follows:
http://server:port/context-root/servlet-mapping?name=value
The following table describes each URL section.
Table 6–1 URL Fields for Servlets Within an Application
In this example, localhost is the host name, MortPages is the context root, and calcMortgage is the servlet mapping.
http://localhost:8080/MortPages/calcMortgage?rate=8.0&per=360&bal=180000
To invoke a servlet from within a JSP file, you can use a relative path. For example:
<jsp:forward page="TestServlet"/><jsp:include page="TestServlet"/>
ServletContext.log messages are sent to the server log. By default, the System.out and System.err output of servlets are sent to the server log. During startup, server log messages are echoed to the System.err output. Also by default, there is no Windows-only console for the System.err output.
You can change these defaults using the Administration Console Write to System Log box. If this box is checked, System.out output is sent to the server log. If it is unchecked, System.out output is sent to the system default location only.
You can use the default-web.xml file to define features such as filters and security constraints that apply to all web applications.
For example, directory listings are disabled by default for added security. To enable directory listings in your domain's default-web.xml file, search for the definition of the servlet whose servlet-name is equal to default, and set the value of the init-param named listings to true. Then restart the server.
<init-param> <param-name>listings</param-name> <param-value>true</param-value> </init-param>
If listings is set to true, you can also determine how directory listings are sorted. Set the value of the init-param named sortedBy to NAME, SIZE, or LAST_MODIFIED. Then restart the server.
<init-param> <param-name>sortedBy</param-name> <param-value>LAST_MODIFIED</param-value> </init-param>
The mime-mapping elements in default-web.xml are global and inherited by all web applications. You can override these mappings or define your own using mime-mapping elements in your web application's web.xml file. For more information about mime-mapping elements, see the Servlet specification.
You can use the Administration Console to edit the default-web.xml file, or edit the file directly using the following steps.
Place the JAR file for the filter, security constraint, or other feature in the domain-dir/lib directory.
Edit the domain-dir/config/default-web.xml file to refer to the JAR file.
To apply your changes, restart Enterprise Server.
See To Restart a Domain.
You can specify that a request for an old URL be treated as a request for a new URL. This is called redirecting a URL.
To specify a redirected URL for a virtual server, use the redirect_n property, where n is a positive integer that allows specification of more than one. Each of these redirect_n properties is inherited by all web applications deployed on the virtual server.
The value of each redirect_n property has two components which can be specified in any order:
The first component, from, specifies the prefix of the requested URI to match.
The second component, url-prefix, specifies the new URL prefix to return to the client. The from prefix is replaced by this URL prefix.
This example redirects from dummy to etude:
<property name="redirect_1" value="from=/dummy url-prefix=http://etude"/>
The Apache Tomcat Connector mod_jk can be used to connect the web container with web servers such as Apache HTTP Server. By using mod_jk, which comes with Enterprise Server, you can front Enterprise Server with Apache HTTP Server.
You can also use mod_jk directly at the JSP/servlet engine for load balancing.
Supported versions of the software referred to in this section include Apache HTTP Server 2.2.11 (UNIX), mod_ssl 2.2.11, OpenSSL 0.9.8a, and mod_jk 1.2.27.
The following topics are addressed here:
To Enable SSL Between the mod_jk Load Balancer and the Browser
To Enable SSL Between the mod_jk Load Balancer and Enterprise Server
You can front Enterprise Server with Apache HTTP Server by enabling the mod_jk protocol for one of Enterprise Server's network listeners, as described in this procedure. A typical use for mod_jk would be to have Apache HTTP Server handle requests for static resources, while having requests for dynamic resources, such as servlets and JavaServerTM Pages (JSPs), forwarded to, and handled by the Enterprise Server back-end instance.
When you use the jk-enabled attribute of the network listener, you do not need to copy any additional JAR files into the /lib directory. You can also create JK connectors under different virtual servers by using the network listener attribute jk-enabled.
Install Apache HTTP Server and mod_jk.
For information on installing Apache HTTP Server, see http://httpd.apache.org/docs/2.0/install.html.
For information on installing mod_jk, see http://tomcat.apache.org/connectors-doc/webserver_howto/apache.html.
Configure the following files:
apache2/conf/httpd.conf, the main Apache configuration file
apache2/config/workers.properties or domain-dir/config/glassfish-jk.properties (to use non-default values of attributes described at http://tomcat.apache.org/tomcat-5.5-doc/config/ajp.html)
If you use both the workers.properties file and the glassfish-jk.properties file, the file referenced by httpd.conf first takes precedence.
Start Apache HTTP Server (httpd).
Start Enterprise Server with at least one web application deployed.
In order for the mod_jk–enabled network listener to start listening for requests, the web container must be started. Normally, this is achieved by deploying a web application.
Create an HTTP listener by using the create-http-listener(1)subcommand.
asadmin> create-http-listener --listenerport 8009 --listeneraddress 0.0.0.0 --defaultvs server listener-name |
where listener-name is the name of the new listener.
Enable mod_jk by using the set(1) subcommand.
Use the following format:
asadmin> set server-config.network-config.network-listeners. network-listener.listener-name.jk-enabled=true |
where listener-name is the ID of the network listener for which mod_jk is being enabled.
If you are using the glassfish-jk.properties file and not referencing it in the httpd.conf file, point to the properties file by using the create-jvm-options(1) subcommand.
Use the following format:
asadmin> create-jvm-options -Dcom.sun.enterprise.web.connector.enableJK.propertyFile= domain-dir/config/glassfish-jk.properties |
To apply your changes, restart Enterprise Server.
See To Restart a Domain.
This example shows an httpd.conf file that is set for mod_jk.
LoadModule jk_module /usr/lib/httpd/modules/mod_jk.so JkWorkersFile /etc/httpd/conf/worker.properties # Where to put jk logs JkLogFile /var/log/httpd/mod_jk.log # Set the jk log level [debug/error/info] JkLogLevel debug # Select the log format JkLogStampFormat "[%a %b %d %H:%M:%S %Y] " # JkOptions indicate to send SSL KEY SIZE, JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories # JkRequestLogFormat set the request format JkRequestLogFormat "%w %V %T" # Send all jsp requests to GlassFish JkMount /*.jsp worker1 # Send all glassfish-test requests to GlassFish JkMount /glassfish-test/* worker1
This example shows a workers.properties or glassfish-jk.properties file that is set for mod_jk.
# Define 1 real worker using ajp13 worker.list=worker1 # Set properties for worker1 (ajp13) worker.worker1.type=ajp13 worker.worker1.host=localhost worker.worker1.port=8009
For more information on Apache, see http://httpd.apache.org/.
For more information on Apache Tomcat Connector, see http://tomcat.apache.org/connectors-doc/index.html.
Load balancing is the process of dividing the amount of work that a computer has to do between two or more computers so that more work gets done in the same amount of time. Load balancing can be configured with or without security.
In order to support stickiness, the Apache mod_jk load balancer relies on a jvmRoute system property that is included in any JSESSIONID received by the load balancer. This means that every Enterprise Server instance that is front-ended by the Apache load balancer must be configured with a unique jvmRoute system property.
On each of the instances, perform the steps in To Enable mod_jk.
If your instances run on the same machine, you must choose different JK ports. The ports must match worker.worker*.port in your workers.properties file. See the properties file in Example 6–5.
On each of the instances, create the jvmRoute system property of Enterprise Server by using the create-jvm-options(1) subcommand.
Use the following format:
asadmin> create-jvm-options "-DjvmRoute=/instance-worker-name"/ |
where instance-worker-name is the name of the worker that you defined to represent the instance in the workers.properties file.
To apply your changes, restart Apache HTTP Server and Enterprise Server.
This example shows an httpd.conf file that is set for load balancing.
LoadModule jk_module /usr/lib/httpd/modules/mod_jk.so JkWorkersFile /etc/httpd/conf/worker.properties # Where to put jk logs JkLogFile /var/log/httpd/mod_jk.log # Set the jk log level [debug/error/info] JkLogLevel debug # Select the log format JkLogStampFormat "[%a %b %d %H:%M:%S %Y] " # JkOptions indicate to send SSL KEY SIZE, JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories # JkRequestLogFormat set the request format JkRequestLogFormat "%w %V %T" # Send all jsp requests to GlassFish JkMount /*.jsp worker1 # Send all glassfish-test requests to GlassFish JkMount /glassfish-test/* loadbalancer |
This example shows a workers.properties or glassfish-jk.properties file that is set for load balancing. The worker.worker*.port should match with JK ports you created.
worker.list=worker1,worker2,loadbalancer worker.worker1.type=ajp13 worker.worker1.host=localhost worker.worker1.port=8009 worker.worker1.lbfactor=1 worker.worker1.socket_keepalive=1 worker.worker1.socket_timeout=300 worker.worker2.type=ajp13 worker.worker2.host=localhost worker.worker2.port=8010 worker.worker2.lbfactor=1 worker.worker2.socket_keepalive=1 worker.worker2.socket_timeout=300 worker.loadbalancer.type=lb worker.loadbalancer.balance_workers=worker1,worker2 |
To activate security for mod_jk on Enterprise Server, you must first generate a Secure Socket Layer (SSL) self-signed certificate on the Apache HTTP Server with the mod_ssl module. The tasks include generating a private key, a Certificate Signing Request (CSR), a self-signed certificate, and configuring SSL-enabled virtual hosts.
The mod_jk connector must be enabled.
Generate the private key as follows:
openssl genrsa -des3 -rand file1:file2:file3:file4:file5 -out server.key 1024 |
where file1:file2: and so on represents the random compressed files.
Remove the pass-phrase from the key as follows:
openssl rsa -in server.key -out server.pem |
Generate the CSR is as follows:
openssl req -new -key server.pem -out server.csr |
Enter the information you are prompted for.
Generate a temporary certificate as follows:
openssl x509 -req -days 60 -in server.csr -signkey server.pem -out server.crt |
This temporary certificate is good for 60 days.
Create the ssl.conf file under the /etc/apache2/conf.d directory.
In the ssl.conf file, add one of the following redirects:
Redirect a web application, for example, JkMount /hello/* worker1.
Redirect all requests, for example, JkMount /* worker1.
# Send all jsp requests to GlassFish JkMount /*.jsp worker1 # Send all glassfish-test requests to GlassFish JkMount /glassfish-test/* loadbalancer |
A basic SSL-enabled virtual host will appear in the ssl.conf file. In this example, all requests are redirected.
Listen 443 <VirtualHost _default_:443> SSLEngine on SSLCipherSuite ALL:!ADH:!EXP56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL SSLCertificateFile "/etc/apache2/2.2/server.crt" SSLCertificateKeyFile "/etc/apache2/2.2/server.pem" JkMount /* worker1 </VirtualHost> |
The self-signed certificate must be configured.
Perform the steps in To Enable mod_jk.
Start another Enterprise Server with at least one web application deployed.
In order for the mod_jk–enabled network listener to start listening for requests, the web container must be started. Normally, this is achieved by deploying a web application.
Follow instructions from To Configure an HTTP Listener for SSL on the mod_jk connector.
Use the following format:
asadmin> create-ssl --type http-listener --certname sampleCert new-listener |
Add the following directives in the httpd.conf file under the /etc/apache2/conf.d directory:
# Should mod_jk send SSL information (default is On) JkExtractSSL On # What is the indicator for SSL (default is HTTPS) JkHTTPSIndicator HTTPS # What is the indicator for SSL session (default is SSL_SESSION_ID) JkSESSIONIndicator SSL_SESSION_ID # What is the indicator for client SSL cipher suit (default is SSL_CIPHER) JkCIPHERIndicator SSL_CIPHER # What is the indicator for the client SSL certificated? (default is SSL_CLIENT_CERT) JkCERTSIndicator SSL_CLIENT_CERT
(Optional) To support both SSL and non-SSL connection, create two mod_jk–enabled network listeners and workers.
Example workers.properties file:
#This example shows a worker.properties worker.list=ajp13unsecure, ajp13secure worker.ajp13secure.type=ajp13 worker.ajp13secure.host=localhost worker.ajp13secure.port=8009 worker.ajp13unsecure.type=ajp13 worker.ajp13unsecure.host=localhost worker.ajp13unsecure.port=8010 |
Secure and unsecure HTTP listeners with ports 8009 and 8010 are created and enabled for mod_jk. For instructions, see To Enable mod_jk.
To apply your changes, restart Apache HTTP Server and Enterprise Server.