Programmer Guide To Web
Programmer Guide To Web
Applications
Sun™ ONE Web Server
Version 6.1
817-1833-10
August 2003
Sun Microsystems, Inc.
4150 Network Circle
Santa Clara, CA 95054 U.S.A.
3
Creating Servlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Creating the Class Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Overriding Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Overriding Initialize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Overriding Destroy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Overriding Service, Get, and Post . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Accessing Parameters and Storing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Handling Sessions and Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Handling Threading Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Delivering Client Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Creating a Servlet Response Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Creating a JSP Response Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Invoking Servlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Calling a Servlet with a URL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Calling a Servlet Programmatically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Servlet Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Caching Servlet Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Caching Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Default Cache Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Caching Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
CacheHelper Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
CacheKeyGenerator Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Maximizing Servlet Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
4 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
<queryBox> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
<submitButton> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
<formAction> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
<formSubmission> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
<formActionMsg> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
<Search> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
<resultIteration> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
<Item> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
<resultStat> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
<resultNav> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
5
Application Developer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Application Assembler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Application Deployer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Common Security Terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Authorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Realms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
J2EE Application Role Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Container Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Programmatic Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Declarative Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
Sun ONE Web Server-specific Security Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
Sun ONE Web Server Security Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
ACL-based Authentication and Authorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
J2EE/Servlet-based Authentication and Authorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Web Application and URL Authorizations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
User Authentication by Servlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
HTTP Basic Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
SSL Mutual Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Form-Based Login . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
User Authentication for Single Sign-on . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
User Authorization by Servlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Defining Roles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Defining Servlet Authorization Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Fetching the Client Certificate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Realm Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
LDAP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Solaris . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Certificate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Custom Realm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Native Realm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Programmatic Login . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Precautions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Granting Programmatic Login Permission . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
The ProgrammaticLogin Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Enabling the Java Security Manager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
The server.policy File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Default Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Changing Permissions for an Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
For More Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
6 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Chapter 6 Deploying Web Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Web Application Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Creating Web Deployment Descriptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Deploying Web Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Using the Administration Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Deploying a Web Application using wdeploy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Using Sun ONE Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Enabling and Disabling Web Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Using the Administration Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Editing the server.xml File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Dynamic Reloading of Web Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Classloaders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
The sun-web-app_2_3-1.dtd File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Subelements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Elements in the sun-web.xml File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
General Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
sun-web-app . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Security Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
security-role-mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
servlet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
servlet-name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
role-name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
principal-name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
group-name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Session Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
session-config . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
session-manager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
manager-properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
store-properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
session-properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
cookie-properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Reference Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
resource-env-ref . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
resource-env-ref-name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
resource-ref . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
res-ref-name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
default-resource-principal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
password . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
7
jndi-name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
Caching Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
cache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
cache-helper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
default-helper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
cache-mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
url-pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
cache-helper-ref . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
timeout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
refresh-field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
http-method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
key-field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
constraint-field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Classloader Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
class-loader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
JSP Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
jsp-config . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Internationalization Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
parameter-encoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
locale-charset-info . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
locale-charset-map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Alphabetical List of sun-web.xml Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Sample Web Application XML Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
Sample web.xml File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
Sample sun-web.xml File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
8 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Appendix A Internationalization Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Servlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Servlet Request . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Servlet Response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
JSPs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
9
10 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
About This Guide
This guide describes how to create and run Java™ 2 Platform, Enterprise Edition
(J2EE™ platform) applications that follow the new open Java standards model for
servlets and JavaServer Pages™ (JSP™) technology on the Sun™ Open Net
Environment (Sun ONE) Web Server 6.1. In addition to describing programming
concepts and tasks, this guide offers implementation tips and reference material.
This preface contains the following topics:
• Who Should Use This Guide
• Using the Documentation
• How This Guide Is Organized
• Documentation Conventions
• Product Support
11
Using the Documentation
12 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Using the Documentation
14 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Documentation Conventions
Documentation Conventions
This section describes the types of conventions used throughout this guide.
• File and directory paths
These are given in UNIX® format (with forward slashes separating directory
names). For Windows versions, the directory paths are the same, except that
backslashes are used to separate directories.
• URLs are given in the format:
http://server.domain/path/file.html
In these URLs, server is the server name where applications are run; domain is
your Internet domain name; path is the server's directory structure; and file is
an individual file name. Italic items in URLs are placeholders.
• Font conventions include:
❍ The monospace font is used for sample code and code listings, API and
language elements (such as function names and class names), file names,
path names, directory names, and HTML tags.
❍ Italic monospace type is used for code variables.
❍ Italic type is also used for book titles, emphasis, variables and placeholders,
and words used in the literal sense.
❍ Bold type is used as either a paragraph lead-in or to indicate words used in
the literal sense.
• Installation root directories are indicated by install_dir in this guide.
By default, the location of install_dir is as follows:
Product Support
If you have problems with your system, contact customer support using one of the
following mechanisms:
• The online support web site at:
http://www.sun.com/supportraining/
• The telephone dispatch number associated with your maintenance contract
Please have the following information available prior to contacting support. This
helps to ensure that our support staff can best assist you in resolving problems:
• Description of the problem, including the situation where the problem occurs
and its impact on your operation.
• Machine type, operating system version, and product version, including any
patches and other software that might be affecting the problem.
• Detailed steps on the methods you have used to reproduce the problem.
• Any error logs or core dumps.
16 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Chapter 1
Web Applications
This chapter provides a basic overview of how web applications are supported in
the Sun™ Open Net Environment (Sun ONE) Web Server 6.1, and includes the
following sections:
• Introducing Web Applications
• Creating a Web Application
• Securing a Web Application
• Deploying a Web Application
• Virtual Servers
• Default Web Applications
• Servlet and JSP Caching
• Database Connection Pooling
• Configuring the Web Container
• Web Application Samples
17
Introducing Web Applications
In addition, Sun ONE Web Server 6.1 supports SHTML and CGI, which are
non-Java™ 2 Platform, Enterprise Edition (J2EE™ platform) application
components.
This section includes summaries of the following topics:
• Servlets
• JavaServer Pages (JSP)
• SHTML
• CGI
Servlets
Java servlets are server-side Java programs that application servers can run to
generate content in response to a client request. Servlets can be thought of as
applets that run on the server side without a user interface. Servlets are invoked
through URL invocation or by other servlets.
Sun ONE Web Server 6.1 supports the Java Servlet 2.3 specification.
NOTE Java Servlet API version 2.3 is fully backward compatible with
versions 2.1 and 2.2, so all existing servlets will continue to work
without modification or recompilation.
To develop servlets, use Sun's Java Servlet API. For information about using the
Java Servlet API, see the documentation provided by Sun at:
http://java.sun.com/products/servlet/index.html
For the Java Servlet 2.3 specification, please visit:
http://java.sun.com/products/servlet/download.html
For information about developing servlets in Sun ONE Web Server, see Chapter 2,
“Using Servlets.”
18 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Introducing Web Applications
SHTML
HTML files can contain tags that are executed on the server. In addition to
supporting the standard server-side tags, or SSIs, Sun ONE Web Server 6.1 allows
you to embed servlets and define your own server-side tags. For more information,
see the Sun ONE Web Server 6.1 Programmer’s Guide.
CGI
Common Gateway Interface (CGI) programs run on the server and generate a
response to return to the requesting client. CGI programs can be written in various
languages, including C, C++, Java, Perl, and as shell scripts. CGI programs are
invoked through URL invocation. Sun ONE Web Server complies with the version
1.1 CGI specification. For more information, see the Sun ONE Web Server 6.1
Programmer’s Guide.
9. Deploy the web application. For more information, see “Deploying Web
Applications” on page 115.
You can create a web application manually, or you can use Sun™ ONE Studio.
20 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Securing a Web Application
Virtual Servers
A virtual server is a virtual web server that uses a unique combination of IP
address, port number, and host name to identify it. You might have several virtual
servers, all of which use the same IP address and port number but are
distinguished by their unique host names.
When you first install Sun ONE Web Server, a default virtual server is created. You
can also assign a default virtual server to each new HTTP listener you create. For
details, see the Sun ONE Web Server 6.1 Administrator's Guide.
Web applications can be hosted under virtual servers.
22 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Configuring the Web Container
You can also deploy these examples using the wdeploy utility. For more
information about this utility, see “Deploying Web Applications” on page 115.
24 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Chapter 2
Using Servlets
This chapter describes how to create effective servlets to control web application
interactions running on a Sun ONE Web Server, including standard servlets. In
addition, this chapter describes the Sun ONE Web Server features used to augment
the standards.
This chapter has the following sections:
• About Servlets
• Creating Servlets
• Invoking Servlets
• Servlet Output
• Caching Servlet Results
• Maximizing Servlet Performance
For information about internationalization issues for servlets, see Appendix A,
“Internationalization Issues.” For information about migrating legacy servlets, see
Appendix B, “Migrating Legacy Servlets.”
About Servlets
Servlets, like applets, are reusable Java applications. Servlets, however, run on a
web server rather than in a web browser.
Servlets provide a component-based, platform-independent method for building
web-based applications, without the performance overheads, process limitations,
and platform-specific liabilities of CGI programs.
Servlets supported by the Sun ONE Web Server are based on the Java Servlet 2.3
specification.
25
About Servlets
26 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
About Servlets
Servlet Types
There are two main servlet types, generic and HTTP:
• Generic servlets
❍ Extend javax.servlet.GenericServlet.
❍ Are protocol independent. They contain no inherent HTTP support or any
other transport protocol.
• HTTP servlets
❍ Extend javax.servlet.HttpServlet.
❍ Have built-in HTTP protocol support and are more useful in a Sun ONE
Web Server environment.
For both servlet types, you implement the constructor method init() and the
destructor method destroy() to initialize or deallocate resources.
Creating Servlets
To create a servlet, perform the following tasks:
• Design the servlet into your web application, or, if accessed in a generic way,
design it to access no application data.
• Create a class that extends either GenericServlet or HttpServlet,
overriding the appropriate methods so it handles requests.
• Use the Sun ONE Web Server Administration interface to create a web
application deployment descriptor. For details, see Chapter 6, “Deploying Web
Applications.”
The rest of this section discusses the following topics:
• Creating the Class Declaration
• Overriding Methods
• Overriding Initialize
• Overriding Destroy
• Overriding Service, Get, and Post
• Accessing Parameters and Storing Data
• Handling Sessions and Security
• Handling Threading Issues
• Delivering Client Results
28 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Creating Servlets
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
Overriding Methods
Next, override one or more methods to provide servlet instructions to perform its
intended task. All processing by a servlet is done on a request-by-request basis and
happens in the service methods, either service() for generic servlets or one of the
doOperation() methods for HTTP servlets. This method accepts incoming
requests, processing them according to the instructions you provide, and directs
the output appropriately. You can create other methods in a servlet as well.
Overriding Initialize
Override the class initializer init() to initialize or allocate resources for the
servlet instance's life, such as a counter. The init() method runs after the servlet
is instantiated but before it accepts any requests. For more information, see the
servlet API specification.
Overriding Destroy
Override the class destructor destroy() to write log messages or to release
resources that have been opened in the servlet's life cycle. Resources should be
appropriately closed and dereferenced so that they are recycled or garbage
collected. The destroy() method runs just before the servlet itself is deallocated
from memory. For more information, see the servlet API specification.
For example, the destroy() method could write a log message like the following,
based on the example for “Overriding Initialize” above:
30 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Creating Servlets
The default service() method in an HTTP servlet routes the request to another
method based on the HTTP transfer method (POST, GET, and so on). For example,
HTTP POST requests are routed to the doPost() method, HTTP GET requests are
routed to the doGet() method, and so on. This enables the servlet to perform
different request data processing depending on the transfer method. Since the
routing takes place in service(), there is no need to generally override service()
in an HTTP servlet. Instead, override doGet(), doPost(), and so on, depending on
the expected request type.
The automatic routing in an HTTP servlet is based simply on a call to
request.getMethod(), which provides the HTTP transfer method. In a Sun ONE
Web Server, request data is already preprocessed into a name-value list by the time
the servlet sees the data, so simply overriding the service() method in an HTTP
servlet does not lose any functionality. However, this does make the servlet less
portable, since it is now dependent on preprocessed request data.
Override the service() method (for generic servlets) or the doGet() or doPost()
methods (for HTTP servlets) to perform tasks needed to answer the request. Very
often, this means collating the needed information (in the request object or in a
JDBC result set object), and then passing the newly generated content to a JSP for
formatting and delivery back to the client.
Most operations that involve forms use either a GET or a POST operation, so for
most servlets you override either doGet() or doPost(). Implement both methods
to provide for both input types or simply pass the request object to a central
processing method, as shown in the following example:
request.setAttribute("favoriteDwarf", "Dwalin");
32 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Creating Servlets
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
34 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Creating Servlets
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
response.setContentType("text/html");
For textual output, such as plain HTML, create a PrintWriter object and then
write to it using println. For example:
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher("JSP_URI");
dispatcher.include(request, response);
... //processing continues
NOTE You cannot use the forward() method if you have already defined
a PrintWriter or ServletOutputStream object.
36 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Invoking Servlets
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher("JSP_URI");
dispatcher.forward(request, response);
For more information about JSPs, see Chapter 3, “Using JavaServer Pages.”
Invoking Servlets
You can invoke a servlet by directly addressing it from a Web page with a URL or
by calling it programmatically from an already running servlet, as described in the
following sections:
• Calling a Servlet with a URL
• Calling a Servlet Programmatically
The following table describes each URL section. The left column lists the URL
elements, and the right column lists descriptions of each URL element.
server:port The IP address (or host name) and optional port number.
To access the default web application for a virtual server,
specify only this URL section. You do not need to specify the
context_root or servlet_name unless you also wish to
specify name-value parameters.
context_root The context path without the leading “/” at which the web
application is installed.
servlet Only needed if no servlet-mapping is defined in the
web.xml file.
servlet_name The servlet-name (or servlet-mapping if defined) as
configured in the web.xml file.
?name=value... Optional servlet name-value parameters.
In this example, leMort is the host name, MortPages is the context root, and
calcMortgage is the servlet name:
http://www.leMort.com/MortPages/servlet/calcMortgage?rate=8.0&per=3
60&bal=180000
http://server:port/OfficeApp/OfficeFrontEnd/servlet/ShowSupplies?name=
value
You can call this servlet programmatically from another servlet in one of two ways,
as described below.
• To include another servlet's output, use the include() method from the
RequestDispatcher interface. This method calls a servlet by its URI and waits
for it to return before continuing to process the interaction. The include()
method can be called multiple times within a given servlet.
38 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Servlet Output
For example:
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher("/ShowSupplies");
dispatcher.include(request, response);
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher("/ShowSupplies");
dispatcher.forward(request, response);
Servlet Output
By default, the System.out and System.err output of servlets is sent to the server
log, and during startup server log messages are echoed to the System.err output.
Also by default, on Windows, there is no console created for the System.err
output.
You can change these defaults using the Administration interface. For more
information, see “Setting Error Logging Options” in Chapter 10 of the Sun ONE
Web Server 6.1 Administrator’s Guide.
Whether to cache results and how to cache them depends on the data involved. For
example, it makes no sense to cache the results of a quiz submission, because the
input to the servlet is different each time. However, you could cache a high-level
report showing demographic data taken from quiz results that is updated once an
hour.
You can define how a Sun ONE Web Server web application handles response
caching by editing specific fields in the sun-web.xml file. In this way, you can
create programmatically standard servlets that still take advantage of this valuable
Sun ONE Web Server feature.
For more information about JSP caching, see “JSP Cache Tags” on page 53.
NOTE For information about caching static file content, see the nsfc.conf
file, described in the Sun ONE Web Server 6.1 Administrator's
Configuration File Reference.
Caching Features
Sun ONE Web Server 6.1 has the following web application response caching
capabilities:
• Caching is configurable based on the servlet name or the URI.
• When caching is based on the URI, this includes user-specified parameters in
the query string. For example, a response from
/garden/catalog?category=roses is different from a response from
/garden/catalog?category=lilies. These responses are stored under
different keys in the cache.
• Cache size, entry timeout, and other caching behaviors are configurable.
40 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Caching Servlet Results
• Entry timeout is measured from the time an entry is created or refreshed. You
can override this timeout for an individual cache mapping by specifying the
cache-mapping subelement timeout.
Caching Example
Here is an example cache element in the sun-web.xml file:
<cache max-capacity="8192" timeout="60">
<cache-helper name="myHelper" class-name="MyCacheHelper"/>
<cache-mapping>
<servlet-name>myservlet</servlet name>
<timeout name="timefield">120</timeout>
<http-method>GET</http-method>
<http-method>POST</http-method>
</cache-mapping>
<cache-mapping>
<url-pattern> /catalog/* </url-pattern>
<!-- cache the best selling category; cache the responses to
-- this resource only when the given parameters exist. cache
-- only when the catalog parameter has 'lilies' or 'roses'
-- but no other catalog varieties:
-- /orchard/catalog?best&category='lilies'
-- /orchard/catalog?best&category='roses'
-- but not the result of
-- /orchard/catalog?best&category='wild'
-->
<constraint-field name='best' scope='request.parameter'/>
<constraint-field name='category' scope='request.parameter'>
<value> roses </value>
<value> lilies </value>
</constraint-field>
<!-- Specify that a particular field is of given range but the
-- field doesn't need to be present in all the requests -->
<constraint-field name='SKUnum' scope='request.parameter'>
<value match-expr='in-range'> 1000 - 2000 </value>
</constraint-field>
<!-- cache when the category matches with any value other than
-- a specific value -->
<constraint-field name="category" scope="request.parameter>
<value match-expr="equals"
cache-on-match-failure="true">bogus</value>
</constraint-field>
</cache-mapping>
<cache-mapping>
<servlet-name> InfoServlet </servlet name>
<cache-helper-ref>myHelper</cache-helper-ref>
</cache-mapping>
</cache>
42 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Caching Servlet Results
For more information about the sun-web.xml caching settings, see “Caching
Elements” on page 143.
CacheHelper Interface
Here is the CacheHelper interface:
package com.sun.appserv.web.cache;
import java.util.Map
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
/** CacheHelper interface is an user-extensible interface to
customize:
* a) the key generation b) whether to cache the response.
*/
public interface CacheHelper {
// name of request attributes
public static final String ATTR_CACHE_MAPPED_SERVLET_NAME =
"com.sun.appserv.web.cachedServletName";
public static final String ATTR_CACHE_MAPPED_URL_PATTERN =
"com.sun.appserv.web.cachedURLPattern";
public static final int TIMEOUT_VALUE_NOT_SET = -2;
/** initialize the helper
* @param context the web application context this helper belongs
to
* @exception Exception if a startup error occurs
*/
public void init(ServletContext context, Map props) throws
Exception;
/** getCacheKey: generate the key to be used to cache this request
* @param request incoming <code>HttpServletRequest</code> object
* @returns the generated key for this requested cacheable
resource.
*/
public String getCacheKey(HttpServletRequest request);
/** isCacheable: is the response to given request cacheable?
* @param request incoming <code>HttpServletRequest</code> object
* @returns <code>true</code> if the response could be cached. or
* <code>false</code> if the results of this request must not be
cached.
*/
public boolean isCacheable(HttpServletRequest request);
/** isRefreshNeeded: is the response to given request to be
refreshed?
* @param request incoming <code>HttpServletRequest</code> object
* @returns <code>true</code> if the response needs to be
refreshed.
* or return <code>false</code> if the results of this request
* don't need to be refreshed.
*/
public boolean isRefreshNeeded(HttpServletRequest request);
/** get timeout for the cached response.
* @param request incoming <code>HttpServletRequest</code> object
* @returns the timeout in seconds for the cached response; a
return
* value of -1 means the response never expires and a value of -2
indicates
* helper cannot determine the timeout (container assigns default
timeout)
*/
public int getTimeout(HttpServletRequest request);
/**
* Stop the helper from active use
* @exception Exception if an error occurs
*/
public void destroy() throws Exception;
}
CacheKeyGenerator Interface
The built-in default CacheHelper implementation allows web applications to
customize the key generation. An application component (in a servlet or JSP) can
set up a custom CacheKeyGenerator implementation as an attribute in the
ServletContext.
44 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Maximizing Servlet Performance
* <property
* name="cacheKeyGeneratorAttrName"
* value="com.acme.web.MyCacheKeyGenerator" />
* </default-helper>
*
* Caching engine looks up the specified attribute in the servlet
* context; the result of the lookup must be an implementation of the
* CacheKeyGenerator interface.
*/
public interface CacheKeyGenerator {
/** getCacheKey: generate the key to be used to cache the
* response.
* @param context the web application context
* @param request incoming <code>HttpServletRequest</code>
* @returns key string used to access the cache entry.
* if the return value is null, a default key is used.
*/
public String getCacheKey(ServletContext context,
HttpServletRequest request);
}
46 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Chapter 3
This chapter describes how to use JavaServer Pages (JSPs) as page templates in a
Sun ONE Web Server web application.
This chapter has the following sections:
• Introducing JSPs
• Creating JSPs
• Compiling JSPs: The Command-Line Compiler
• Debugging JSPs
• JSP Tag Libraries and Standard Portable Tags
• JSP Cache Tags
• JSP Search Tags
For information about internationalization issues for JSPs, see Appendix A,
“Internationalization Issues.”
Introducing JSPs
JSPs are browser pages in HTML or XML. They also contain Java code, which
enables them to perform complex processing, conditionalize output, and
communicate with other application objects. JSPs in Sun ONE Web Server are
based on the JSP 1.2 specification.
In a Sun ONE Web Server application, JSPs are the individual pages that make up
an application. You can call a JSP from a servlet to handle the user interaction
output, or, since JSPs have the same application environment access as any other
application component, you can use a JSP as an interaction destination.
47
Creating JSPs
JSPs are made up of JSP elements and template data. Template data is anything not
in the JSP specification, including text and HTML tags. For example, the minimal
JSP requires no processing by the JSP engine and is a static HTML page.
The Sun ONE Web Server compiles JSPs into HTTP servlets the first time they are
called (or they can be precompiled for better performance). This makes them
available to the application environment as standard objects and enables them to
be called from a client using a URL.
JSPs run inside the server's JSP engine, which is responsible for interpreting
JSP-specific tags and performing the actions they specify in order to generate
dynamic content. This content, along with any template data surrounding it, is
assembled into an output page and is returned to the caller.
Creating JSPs
You create JSPs in basically the same way you create HTML files. You can use an
HTML editor to create pages and edit the page layout. You make a page a JSP by
inserting JSP-specific tags into the raw source code where needed, and by giving
the file a .jsp extension.
JSPs that adhere to the JSP 1.2 specification follow XML syntax for the most part,
which is consistent with HTML. For a summary of the JSP tags you can use, see
“JSP Tag Libraries and Standard Portable Tags” on page 53.
JSPs are compiled into servlets, so servlet design considerations also apply to JSPs.
JSPs and servlets can perform the same tasks, but each excels at one task at the
expense of the other. Servlets are strong in processing and adaptability. However,
performing HTML output from them involves many cumbersome println
statements that must be coded by hand. Conversely, JSPs excel at layout tasks
because they are simply HTML files and can be created with HTML editors,
though performing complex computational or processing tasks with them is
awkward. For information about servlets, see Chapter 2, “Using Servlets.”
Additional JSP design tips are described in the following sections:
• Designing for Ease of Maintenance
• Designing for Portability
• Handling Exceptions
48 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Compiling JSPs: The Command-Line Compiler
Handling Exceptions
If an uncaught exception occurs in a JSP file, Sun ONE Web Server generates an
exception, usually a 404 or 500 error. To avoid this problem, set the errorPage
attribute of the <%@ page%> tag.
To allow the JSP container to pick up the precompiled JSPs from a JAR file, you
must disable dynamic reloading of JSPs. To do this, set the reload-interval
property to -1 in the jsp-config element of the sun-web.xml file. For more
information, see “JSP Elements” on page 156.
The jspc command-line tool is located under install_dir/bin/https/bin (make
sure this directory is in your path). The format of the jspc command is as follows:
jspc [options] file_specifier
The following table shows what file_specifier can be in the jspc command. The left
column lists file specifiers, and the right column lists descriptions of those file
specifiers.
The following table shows the basic options for the jspc command. The left column
lists the option, and the right column describes what the option does.
50 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Compiling JSPs: The Command-Line Compiler
The following table shows the advanced options for the jspc command. The left
column lists the option, and the right column describes what the option does.
-webinc file Creates partial servlet mappings for the -webapp option,
which can be pasted into a web.xml file.
-webxml file Creates an entire web.xml file for the -webapp option.
-ieplugin class_id Specifies the Java plugin COM class ID for Internet
Explorer. Used by the <jsp:plugin> tags.
-deprecatedjavac Forces compilation of generated servlets using the
deprecated sun.tools.javac.Main.
For example, this command compiles the hello JSP file and writes the compiled
JSP under hellodir:
jspc -d hellodir -genclass hello.jsp
This command compiles all of the JSP files in the web application under webappdir
into class files under jspclassdir:
jspc -d jspclassdir -genclass -webapp webappdir
To use either of these precompiled JSPs in a web application, put the classes under
hellodir or jspclassdir into a JAR file, place the JAR file under WEB-INF/lib,
and set the reload-interval property to -1 in the sun-web.xml file.
52 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Debugging JSPs
Debugging JSPs
When you use Sun ONE Studio to debug JSPs, you can set breakpoints in either the
JSP code or the generated servlet code, and you can switch between them and see
the same breakpoints in both.
For information about setting up debugging in Sun ONE Studio, see “Using Sun
ONE Studio for Debugging” on page 169.
You can add a taglib mapping in the web.xml of your application as follows:
<taglib>
<taglib-uri>/com/sun/web/taglibs/cache</taglib-uri>
<taglib-location>/WEB-INF/sun-web-cache.tld</taglib-location>
</taglib>
You can then refer to these tags in your JSP files as follows:
<%@ taglib prefix="mypfx" uri="/com/sun/web/taglibs/cache" %>
Subsequently, the cache tags are available as <mypfx:cache> and <mypfx:flush>.
The tags are as follows:
• cache
• flush
cache
The cache tag allows you to cache fragments of your JSP pages. It caches the body
between the beginning and ending tags according to the attributes specified. The
first time the tag is encountered, the body content is executed and cached. Each
subsequent time it is run, the cached content is checked to see if it needs to be
refreshed and if so, it is executed again, and the cached data is refreshed.
Otherwise, the cached data is served.
Attributes
The following table describes attributes for the cache tag. The left column lists the
attribute name, the middle column indicates the default value, and the right
column describes what the attribute does.
key ServletPath_Suf (optional) The name used by the container to access the
fix cached entry. The cache key is suffixed to the servlet path to
generate a key to access the cached entry. If no key is
specified, a number is generated according to the position of
the tag in the page.
54 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
JSP Cache Tags
timeout 60s (optional) The time in seconds after which the body of the
tag is executed and the cache is refreshed. By default, this
value is interpreted in seconds. To specify a different unit of
time, add a suffix to the timeout value as follows: s for
seconds, m for minutes, h for hours, d for days. For example,
2h specifies two hours.
nocache false (optional) If set to true, the body content is executed and
served as if there were no cache tag. This offers a way to
programmatically decide whether the cached response
should be sent or whether the body must be executed,
though the response is not cached.
refresh false (optional) If set to true, the body content is executed and the
response is cached again. This lets you programmatically
refresh the cache immediately, regardless of the timeout
setting.
Example
The following example represents a cached JSP page:
<%@ taglib prefix="mypfx" uri="/com/sun/web/taglibs/cache" %>
<%
String cacheKey = null;
if (session != null)
cacheKey = (String)session.getAttribute("loginId");
// force reload
boolean reload=false;
String refresh = request.getParameter("refresh");
if (refresh != null)
reload = true;
%>
<mypfx:cache timeout="1h">
<h2> Local News </h2>
<%
// get the headline news and cache them
%>
</mypfx:cache>
flush
Forces the cache to be flushed. If a key is specified, only the entry with that key is
flushed. If no key is specified, the entire cache is flushed.
Attributes
The following table describes attributes for the flush tag. The left column lists the
attribute name, the middle column indicates the default value, and the right
column describes what the attribute does.
key ServletPath_Suffix (optional) The name used by the container to access the
cached entry. The cache key is suffixed to the servlet path to
generate a key to access the cached entry. If no key is
specified, a number is generated according to the position of
the tag in the page.
Examples
To flush the entry with key="foobar":
<mypfx:flush key="foobar"/>
56 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
JSP Search Tags
• <resultStat>
• <resultNav>
NOTE The Sun ONE Web Server search feature is i18n compliant, and
supports multiple character encoding schemes in the same
collection. Custom JSPs that expose search can be in any encoding.
<searchForm>
Function
Constructs an HTML form that contains default, hidden form elements such as the
current search result index and number of records per page by default. The default
names for the form, index, and number of records are searchform, si, and ns.
Attributes
Name. Specifies the name of the form. The default is searchform. The name of a
form is the identifier for all other tags.
Action. (optional) Specifies the form action.
Method. (optional) Specifies the method of submission, GET or POST. The default
is GET.
elemStart. (optional) Specifies the name of the hidden Start element. If not
specified, the default "si" will be used.
Start. (optional) An integer indicating the starting index for displaying the search
result. The default is 1.
elemNumShown. (optional) The name of the nShown element. If not specified, the
default "ns" is used.
numShown. (optional) An integer indicating the number of results to be shown
per page. The value of the attribute will be retrieved by requesting parameter
elemNumShown. The default is 10.
Usage
<s1ws:form action="results.jsp" />
...
</s1ws:searchForm>
This creates an HTML form tag <form name="searchform"
action="results.jsp" method="GET"> along with two hidden inputboxes:
58 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
JSP Search Tags
• A hidden inputbox for starting index named "si" with a value from the "si"
parameter or default 1, and
• A hidden inputbox for number of records per page named "ns" with a value
from the ns parameter or default 20.
<CollElem>
Function
Creates a hidden inputbox or select box, or multiple checkboxes depending on the
attribute input. If there is only one collection, the tag will create a hidden inputbox
by default.
Attributes
Name. Specifies the name of the form element created. The default is "c."
items. (optional) A comma-delimited string representing search collections
available. The tag retrieves all collections available on the server if the attribute is
empty.
Type. (optional) The type of form element used for displaying collections. Valid
options are hidden, select, and checkbox. The default value is hidden if there is
only one collection, and checkbox if there are multiple collections.
Rows. (optional) Represents size if type is select, or number of rows if
checkboxes. The default behavior is to satisfy the Cols attribute first. That is, the
collections will be listed in columns as specified by the Cols attribute.
Cols. Represents number of columns and is only required if type is checkbox. If
Cols and Rows are not specified, the collections will be listed horizontally, that is,
in one row.
Defaults. Specifies a comma-delimited string containing 1s or 0s indicating the
selection status of the search collections. An item is selected if 1, and not selected if
0. If there is a form action, these values will be retrieved from the form elements.
cssClass. (optional) The class name that will be used in every HTML tag created in
this tag. This is particularly useful when the type is checkbox, since an HTML table
will be used for the layout. See the sample code below for details.
Usage
<s1ws:collElem type="checkbox" cols="2" values="1,0,1,0"
cssClass="body" />
This creates checkboxes in 2 columns with a default name "c," with the first and
third items selected. Fonts and any other HTML styles are defined in css classes
"body," which includes tr.body, td.body, and input.body.
<collection>
Function
Retrieves the name of search collections on the server, and iterates through and
passes each of them to the collectionitem tag. Users may choose to use this tag
along with collection item tags so they can write their own HTMLs.
Attributes
items. (optional) A comma-delimited string representing the search collections
available. The tag retrieves all collections available on the server if the attribute is
empty.
Usage
<table border=0>
<s1ws:collection>
<tr><td><input type=checkbox name="c" value="<s1ws:collItem
property="name" />"><s1ws:collItem property="display name"
/></td></tr>
</s1ws:collection>
</table>
The above code will display all collections with checkboxes.
<select name=elementname>
<s1ws:collection>
<option value="<s1ws:collItem property=\"name\" />"><s1ws:colleItem
property="display name" />
</s1ws:collection>
</select>
This iterates through the available collections and passes each item to the collection
item tag, which in turn displays the name and display name of the item.
<colIItem>
Function
Displays the name and label of one collection item. Must be used inside the
collection tag.
60 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
JSP Search Tags
Attributes
property. Specifies a keyword indicating which property the tag should output.
Valid inputs include "name," "display name," and "description." Default is "name."
Usage
<select name=elementname>
<s1ws:collection>
<option value="<s1ws:collItem property=\"name\" />"><s1ws:collItem
property="display name" />
</s1ws:collection>
</select>
This iterates through the available collections and passes each item to the collection
item tag, which in turn displays the name and display name of the item.
<queryBox>
Function
Creates an inputbox.
Attributes
name. (optional) The name of the inputbox. The default is "qt."
default. (optional) The default value for the query box. If the form is submitted, its
value will be set using what has been submitted.
size. (optional) The size of the inputbox. The default is 50.
maxlength. (optional) The maxlength of the inputbox. The default is 50.
cssClass. (optional) The CSS class.
Usage
<s1ws:queryBox size="30" />
This creates an inputbox with default name "qt" and size=30.
<submitButton>
Function
Creates a submit button.
Attributes
name. (optional) The name of the button. The default is "sb."
default. (optional) The default value of the button, which will be "search."
cssClass. (optional) The CSS class name.
style. The CSS style.
image. The optional image for the button.
Usage
<s1ws:submitButton cssClass="navBar" style="padding: 0px; margin:
0px; width: 50px">
This creates a submit button with default name "sb."
<formAction>
Function
Handles form action. It retrieves all elements on the search form, validates that
there is at least one collection selected and the query is not empty, and passes the
values on to search and results tags as parent or through the page context.
Attributes
formId. Specifies the name of the form. If not specified, the default form
"searchForm" will be used.
elemColl. (optional) The name of the Collection element. The default name "c" is
used.
elemQuery. (optional) The name of the Start element. The default name "qt" is
used.
elemStart. (optional) The name of the Start element. The default name "si" is used.
elemNumShown. (optional) The name of the numShown element. The default name
"ns" is used.
Usage
<s1ws:formAction />
62 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
JSP Search Tags
<formSubmission>
Function
Tests if the form submission is successful.
Attributes
formId. Specifies the name of the form in question. It must be the same as that for
<formAction>.
success. Indicates if the form submission is successful. The values true or yes
represents successful action. All other inputs will be rendered as failure.
Usage
<s1ws:formSubmission success="true" >
<s1ws:search>
...
</s1ws:formSubmission>
<formActionMsg>
Function
Prints out an error message from formAction, if any.
Attributes
formId. (optional) Specifies the name of the form in question. If not specified, the
default id is "searchForm."
elem. (optional) Specifies the name of the element. Valid inputs are "query" and
"collection." When specified, the tag returns an error message, if any, regarding the
element. Otherwise, it prints out all of the error messages generated.
Usage
<FormActionMsg elem="query">
This tag will display a message "query text not specified" if a query is not
submitted.
The message is printed from the form action where the tag is placed.
<Search>
Function
Executes a search query and returns search results. The search tag retrieves a query
string and collection(s) from either a form parent tag or the query and collection
attributes. The search results are saved in the page context with a page or session
scope.
Attributes
formId. Specifies the name of the form used for the search. The default form will be
used if the attribute is left empty. If this tag is used without a form, this attribute
must be set to provide an identifier for the resultIterate tag.
Collection. (optional) A comma-delimited string representing collection(s) used
for a search. If there is a form action, this attribute is ignored and the values are
retrieved by requesting the collection element.
Query. (optional) Specifies the query text. If not provided, it is retrieved from the
query element.
Scope. Specifies an integer indicating the scope of the search results. The value 1,
which is the default, indicates page scope, and 2 indicates session scope.
Usage
<s1ws:search />
This search tag uses the default parameters and executes a search. The search
results will be saved in pageContext with a page scope.
<s1ws:search Collection="col1, col2" Query="Java Web Service"
scope="2" />
This search tag will execute a search in col1 and col2 using "Java Web Service"
as the query string. The search results will be saved in pageContext with a session
scope.
<resultIteration>
Function
Retrieves the search results from either the parent search tag or the page context.
The tag is responsible for iterating through the results and passing the
searchitems to the item tags.
64 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
JSP Search Tags
Attributes
formId. Specifies the name of the form associated. The default form will be used if
the attribute is left empty. If this tag is used without a form, this attribute must be
set as a reference to the search tag.
Start. Specifies an integer representing the starting position in the search results.
The default is 0. The value is retrieved from the parent <formAction> tag or
pageContext, or the parameter value.
<Item>
Function
Retrieves a searchitem from the Results parent tag and outputs its properties as
specified by the property attribute.
Attributes
Property. Specifies a case-sensitive string representing field names in a search item,
such as title, number, score, filename, URL, size, and date.
<resultStat>
Function
Returns numbers indicating number of records returned and the range currently
displayed.
Attributes
formId. Specifies the name of the form associated. The default form will be used if
the attribute is left empty. If this tag is used without a form, this attribute must be
set as a reference to the search tag.
type. Specifies the type of statistics data. Valid inputs are "start," "end," "range" (for
example, 1-20), and "total."
<resultNav>
Function
Creates a navigation bar.
Attributes
formId. Specifies the name of the form associated. The default form will be used if
the attribute is left empty. If this tag is used without a form, this attribute must be
set as a reference to the search tag.
type. Specifies the type of navigation bar. Valid inputs are "full," "previous," and
"next." A full navigation bar looks like this: previous 1 2 3 4 5 6 7 8 9 10
next, where 5 is currently selected. The number of "page number" links is
determined by the "offset" attribute and the number of pages available.
caption. Only necessary if type is previous or next and the default text is not
wanted. Caption can be any HTML.
offset. Specifies the number of page links around the selected page. For example, if
offset=2, the sample bar above would look like this: previous 3 4 5 6 7 next .
Only required for type "full."
66 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Chapter 4
Session Managers
Session objects maintain state and user identity across multiple page requests over
the normally stateless HTTP protocol. A session persists for a specified period of
time, across more than one connection or page request from the user. A session
usually corresponds to one user, who may visit a site many times. The server can
maintain a session either by using cookies or by rewriting URLs. Servlets can access
the session objects to retrieve state information about the session.
This chapter describes sessions and session managers, and has the following
sections:
• Introducing Sessions
• How to Use Sessions
• Session Managers
Introducing Sessions
The term user session refers to a series of user application interactions that are
tracked by the server. Sessions are used for maintaining user specific state,
including persistent objects (such as handles to database result sets) and
authenticated user identities, among many interactions. For example, a session
could be used to track a validated user login followed by a series of directed
activities for a particular user.
The session itself resides in the server. For each request, the client transmits the
session ID in a cookie or, if the browser does not allow cookies, the server
automatically writes the session ID into the URL.
The Sun ONE Web Server supports the servlet standard session interface, called
HttpSession, for all session activities. This interface enables you to write portable,
secure servlets.
67
Introducing Sessions
NOTE As of Sun ONE Web Server 6.1, form-login sessions are no longer
supported. You can use single sign-on sessions instead.
68 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
How to Use Sessions
You can configure whether sessions use URL rewriting. See the
session-properties element in the sun-web.xml file, described in Chapter 6,
“Deploying Web Applications.”
For more information about getSession(), see the Java Servlet 2.3 specification.
70 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
How to Use Sessions
For example:
The following table shows the methods used to examine servlet request properties.
The left column lists HttpServletRequest methods, and the right column lists
descriptions of these methods.
getRequestedSessio Returns the session ID specified with the request. This may
nId() differ from the session ID in the current session if the
session ID given by the client is invalid and a new session
was created. Returns null if the request does not have a
session associated with it.
isRequestedSession Checks if the request is associated to a currently valid
IdValid() session. If the session requested is not valid, it is not
returned through the getSession() method.
isRequestedSession Returns true if the request's session ID provided by the
IdFromCookie() client is a cookie, or false otherwise.
isRequestedSession Returns true if the request's session ID provided by the
IdFromURL() client is a part of a URL, or false otherwise.
For example:
if ( request.isRequestedSessionIdValid() ) {
if ( request.isRequestedSessionIdFromCookie() ) {
// this session is maintained in a session cookie
}
// any other tasks that require a valid session
} else {
// log an application error
}
72 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
How to Use Sessions
Invalidating a Session
Direct the session to invalidate itself automatically after being inactive for a defined
time period. Alternatively, invalidate the session manually with the HttpSession
method invalidate().
Session Managers
A session manager automatically creates new session objects whenever a new
session starts. In some circumstances, clients do not join the session, for example, if
the session manager uses cookies and the client does not accept cookies.
In compliance with the Java Servlet 2.3 API specification, session managers allow
for session scoping only by web application.
To successfully restore the state of session attributes, all such attributes must
implement the java.io.Serializable interface. You can configure the session
manager to enforce this restriction by including the distributable element in the
web application deployment descriptor file, web.xml.
Sun ONE Web Server 6.1 provides the following session management options,
which are described in this section:
• StandardManager, the default session manager
• PersistentManager, a provided session manager that uses a persistent data
store
• IWSSessionManager, a provided session manager that allows backward
compatibility with any custom session managers you may have created using
Sun ONE Web Server 6.0
74 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Session Managers
StandardManager
The StandardManager is the default session manager.
Enabling StandardManager
You may want to specify StandardManager explicitly to change its default
parameters. To do so, edit the sun-web.xml file for the web application as in the
following example. Note that persistence-type must be set to memory.
<sun-web-app>
...
<session-config>
<session-manager persistence-type=”memory”>
<manager-properties>
<property name="reapIntervalSeconds" value="20" />
</manager-properties>
</session-manager>
...
</session-config>
...
</sun-web-app>
For more information about the sun-web.xml file, Chapter 6, “Deploying Web
Applications.”
PersistentManager
The PersistentManager is another session manager provided with Sun ONE Web
Server. For session persistence, PersistentManager can use a file, to which each
session is serialized. You can also create your own persistence mechanism.
Enabling PersistentManager
You may want to specify PersistentManager explicitly to change its default
parameters. To do so, edit the sun-web.xml file for the web application as in the
following example. Note that persistence-type must be set to file.
<sun-web-app>
...
<session-config>
<session-manager persistence-type=”file”>
<manager-properties>
<property name=reapIntervalSeconds value=20 />
</manager-properties>
76 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Session Managers
<store-properties>
<property name=directory value=sessions />
</store-properties>
</session-manager>
...
</session-config>
...
</sun-web-app>
For more information about the sun-web.xml file, Chapter 6, “Deploying Web
Applications.”
IWSSessionManager
The IWSSessionManager manager ensures backward compatibility with any
custom session managers that you may have created on Sun ONE Web Server 6.0.
IWSSessionManager works in both single-process and multi-process mode. It can
be used for sharing session information across multiple processes possibly running
on different machines. The MaxProcs directive in the magnus.conf file determines
whether the server is running in single-process or multi-process mode.
For more information on the MaxProcs directive, see the Sun ONE Web Server 6.1
NSAPI Programmer’s Guide.
For session persistence, IWSSessionManager can use a database or a distributed file
system (DFS) path that is accessible from all servers in a server farm. Each session
is serialized to the database or distributed file system. You can also create your
own persistence mechanism.
If Sun ONE Web Server is running in single-process mode, then by default, no
session persistence mode is defined and therefore sessions are not persistent.
If Sun ONE Web Server is running in multi-process mode, sessions are persistent
by default. If a persistence mode is not defined, IWSSessionManager uses a DFS.
Multi-process mode is supported only on UNIX platforms. All multi-process mode
features of IWSSessionManager are ignored on Windows.
Enabling IWSSessionManager
You may want to enable IWSSessionManager to change its default parameters. You
can also enable IWSSessionManager for a particular context if the server is running
in single-process mode. To do so, edit the sun-web.xml file for the web application
as in the following example. Note that persistence-type must be set to s1ws60.
<sun-web-app>
...
<session-config>
<session-manager persistence-type=”s1ws60”>
<manager-properties>
<property name=”classname” value=”com.iplanet.server.http.
session.IWSSessionManager”/>
// other manager-related properties
</manager-properties>
</session-manager>
...
</session-config>
...
</sun-web-app>
78 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Session Managers
80 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Session Managers
82 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Session Managers
84 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Session Managers
Enabling MMapSessionManager
You may want to enable MMapSessionManager to change its default parameters.
You can also enable MMapSessionManager for a particular context if the server is
running in single-process mode. To do so, edit the sun-web.xml file for the web
application as in the following example. Note that persistence-type must be set
to mmap.
<sun-web-app>
...
<session-config>
<session-manager persistence-type=”mmap”>
...
</session-manager>
...
</session-config>
...
</sun-web-app>
For more information about the sun-web.xml file, see Chapter 6, “Deploying Web
Applications.”
86 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Chapter 5
This chapter describes the basic goals and features of Sun ONE Web Server 6.1
security, and describes how to write secure web applications containing
components that perform user authentication and access authorization tasks.
Because it is helpful to have a basic understanding of security responsibilities and
terminology, the beginning of the chapter discusses those topics.
This chapter has the following sections:
• Sun ONE Web Server Security Goals
• Security Responsibilities Overview
• Common Security Terminology
• Sun ONE Web Server-specific Security Features
• User Authentication by Servlets
• User Authentication for Single Sign-on
• User Authorization by Servlets
• Fetching the Client Certificate
• Realm Configuration
• Programmatic Login
• Enabling the Java Security Manager
• The server.policy File
• For More Information
87
Sun ONE Web Server Security Goals
The roles described in this section apply to the J2EE/Servlet model and are
described in more detail in the J2EE specification:
• Application Developer
• Application Assembler
• Application Deployer
Application Developer
The application developer is responsible for the following:
• Specifying application roles.
• Defining role-based access restrictions for the application components (servlets
and JSPs components).
• If programmatic security is used, verifying the user roles and authorizing
access to features based on these roles. (Programmatic security management is
discouraged because it hard-codes the security login in the application instead
of allowing the containers to manage it.)
Application Assembler
The application assembler or application component provider must identify all
security dependencies embedded in a component, including:
• All role names used by the components that call isUserInRole.
• References to all external resources accessed by the components.
• References to all intercomponent calls made by the component.
Application Deployer
The application deployer takes all component security views provided by the
assembler and uses them to secure a particular enterprise environment in the
application, including:
• Assigning users or groups (or both) to security roles.
• Refining the privileges required to access component methods to suit the
requirements of the specific deployment scenario.
Authentication
Authentication verifies the user. For example, the user may enter a user name and
password in a web browser, and if those credentials match the permanent profile
stored in the active realm, the user is authenticated. The user is associated with a
security identity for the remainder of the session.
Authorization
Authorization permits a user to perform the desired operations, after being
authenticated. For example, a human resources application may authorize
managers to view personal employee information for all employees, but allow
employees to view only their own personal information.
Realms
A realm, also called a security policy domain or a security domain in the J2EE
specification, is a scope over which a common security policy is defined and
enforced by the security administrator of the security service. Supported realms in
Sun ONE Web Server are file, ldap, certificate, solaris, custom, and
nativerealm. For more information about supported realms, see “Realm
Configuration” on page 102.
Container Security
The component containers are responsible for providing J2EE application security.
Two security forms are provided by the container, as discussed below:
programmatic security and declarative security.
Programmatic Security
Programmatic security is when a servlet uses method calls to the security API, as
specified by the J2EE security model, to make business logic decisions based on the
caller or remote user's security role. Programmatic security should only be used
when declarative security alone is insufficient to meet the application's security
model.
The J2EE 1.3 specification defines programmatic security with respect to servlets as
consisting of two methods of the servlet HttpServletRequest interface. Sun ONE
Web Server supports these interfaces as defined in the specification.
In addition to the programmatic security defined in the J2EE specifications, Sun
ONE Web Server also supports programmatic login. For more information, see
“Programmatic Login” on page 107.
Declarative Security
Declarative security means that the security mechanism for an application is
declared and handled external to the application. Deployment descriptors describe
the J2EE application's security structure, including security roles, access control,
and authentication requirements.
Sun ONE Web Server supports the DTDs specified by the J2EE 1.3 specification,
and has additional security elements included in its own deployment descriptors.
Declarative security is the application deployer's responsibility.
acl "path=/export/user/990628.1/docs/misc/";
authenticate (user,group) {
database = "default";
method = "basic";
};
deny (all)
(user = "John");
allow (read);
The core ACLs in Sun ONE Web Server 6.1 support three types of authentication:
basic, certificate, and digest.
Basic authentication relies on lists of user names and passwords passed as
cleartext. Certificates bind a name to a public key. Digest authentication uses
encryption techniques to encrypt the user’s credentials.
The main features of the ACL-based access control model are described below:
• ACL-based authentication uses the following configuration files:
❍ server-install/httpacl/*.acl files
❍ server-install/userdb/dbswitch.conf
❍ server-install/config/server.xml
In addition, the Sun ONE Web Server 6.1 SSL engine supports external crypto
hardware to offload SSL processing and to provide optional tamper-resistant key
storage.
For more information about access control and the use of external crypto hardware,
see the Sun ONE Web Server 6.1 Administrator’s Guide.
The main features of the J2EE/Servlet-based access control model are described
below:
• J2EE/Servlet-based authentication uses the following configuration files:
❍ The web application deployment descriptor files web.xml and
sun-web.xml
❍ server-install/config/server.xml
• Authentication is performed by Java security realms that are configured
through AUTHREALM entries in the server.xml file.
• Authorization is performed by access control rules in the deployment
descriptor file, web.xml, in case any such rules have been set.
For more information about web.xml elements, see the Java Servlet 2.3 specification
(chapter SRV.13, “Deployment Descriptor”). You can find the specification here:
http://java.sun.com/products/servlet/download.html
For more information regarding sun-web.xml elements, see Chapter 6,
“Deploying Web Applications.”
Form-Based Login
The login screen's look and feel cannot be controlled with the HTTP browser's
built-in mechanisms. J2EE introduces the ability to package a standard HTML or
servlet/JSP based form for logging in. The login form is associated with a web
protection domain (an HTTP realm) and is used to authenticate previously
unauthenticated users.
Because passwords are sent in the clear (unless protected by the underlying
transport), this authentication type is not very secure. Use of SSL or another
equivalent transport encryption is recommended to protect the password during
transmission.
For the authentication to proceed appropriately, the login form action must always
be j_security_check.
The following is an HTML sample showing how to program the form in an HTML
page:
You can specify the parameter encoding for the form. For details, see
“parameter-encoding” on page 159.
• When the user logs out of one web application (for example, by invalidating or
timing out the corresponding session if form-based login is used), the user's
sessions in all web applications are invalidated. Any subsequent attempt to
access a protected resource in any application requires the user to authenticate
him or herself again.
• The single sign-on feature utilizes HTTP cookies to transmit a token that
associates each request with the saved user identity, so it can only be used in
client environments that support cookies.
To configure single sign-on, set the following properties in the VS element of the
server.xml file:
• sso-enabled: If false, single sign-on is disabled for this virtual server, and
users must authenticate separately to every application on the virtual server.
The default is false.
• sso-max-inactive-seconds: Specifies the time after which a user's single
sign-on record becomes eligible for purging if no client activity is received.
Since single sign-on applies across several applications on the same virtual
server, access to any of the applications keeps the single sign-on record active.
The default value is 5 minutes (300 seconds). Higher values provide longer
single sign-on persistence for the users at the expense of more memory use on
the server.
• sso-reap-interval-seconds: Specifies the interval between purges of
expired single sign-on records. The default value is 60.
Here is an example configuration with all default values:
Defining Roles
Security roles define an application function, made up of a number of users,
groups, or both. The relationship between users and groups is determined by the
specific realm implementation being used.
You define roles in the J2EE deployment descriptor file, web.xml, and the
corresponding role mappings in the Sun ONE Web Server deployment descriptor
file, sun-web.xml. For more information about sun-web.xml, see Chapter 6,
“Deploying Web Applications.”
Each security-role-mapping element in the sun-web.xml file maps a role name
permitted by the web application to principals and groups. For example, a
sun-web.xml file for a deployed web application might contain the following:
<sun-web-app>
<security-role-mapping>
<role-name>manager</role-name>
<principal-name>jgarcia</principal-name>
<principal-name>mwebster</principal-name>
<group-name>team-leads</group-name>
</security-role-mapping>
<security-role-mapping>
<role-name>administrator</role-name>
<principal-name>dsmith</principal-name>
</security-role-mapping>
</sun-web-app>
Note that the role-name in this example must match the role-name in the
security-role element of the corresponding web.xml file.
100 Sun ONE Web Server 6.1 • Programmer’s Guide • August 2003
Fetching the Client Certificate
For web applications, the roles are always specified in the sun-web.xml file. A role
can be mapped to either specific principals or to groups (or both). The principal or
group names used must be valid principals or groups in the current realm.
if (request.isSecure()) {
java.security.cert.X509Certificate[] certs;
certs =
request.getAttribute("javax.servlet.request.X509Certificate");
if (certs != null) {
clientCert = certs[0];
if (clientCert != null) {
// Get the Distinguised Name for the user.
java.security.Principal userDN =
clientCert.getSubjectDN();
...
}
}
}
The userDn is the fully qualified Distinguished Name for the user.
Realm Configuration
This section provides an overview of the configuration characteristics of the
supported realms. For detailed information about configuring realms, see the Sun
ONE Web Server 6.1 Administrator’s Guide.
The section describes the following realms:
• File
• LDAP
• Solaris
• Certificate
• Custom Realm
• Native Realm
File
The file realm is the default realm when you first install Sun ONE Web Server, and
has the following configuration characteristics:
• Name: file
• Classname: com.iplanet.ias.security.auth.realm.file.FileRealm
Required properties are as follows:
• file: The name of the file that stores user information. By default this file is
instance_dir/config/keyfile.
• jaas-context: The value must be fileRealm.
The user information file is initially empty, so you must add users before you can
use the file realm.
LDAP
The LDAP realm allows you to use an LDAP database for user security
information, and has the following configuration characteristics:
• Name: ldap
• Classname: com.iplanet.ias.security.auth.realm.ldap.LDAPRealm
102 Sun ONE Web Server 6.1 • Programmer’s Guide • August 2003
Realm Configuration
• base-dn: The base DN for the location of user data. This base DN can be at any
level above the user data, since a tree scope search is performed. The smaller
the search tree, the better the performance.
• jaas-context: The value must be ldapRealm.
You can add the following optional properties to tailor the LDAP realm behavior:
• search-filter: The search filter to use to find the user. The default is uid=%s
(%s expands to the subject name).
• group-base-dn: The base DN for the location of group data. By default it is
same as the base-dn, but it can be tuned if necessary.
• group-search-filter: The search filter to find group memberships for the
user. The default is uniquemember=%d (%d expands to the user element DN).
• group-target: The LDAP attribute name that contains group name entries.
The default is CN.
• search-bind-dn: An optional DN used to authenticate to the directory for
performing the search-filter lookup. Only required for directories that do
not allow anonymous search.
• search-bind-password: The LDAP password for the DN given in
search-bind-dn.
You must create the desired user(s) in your LDAP directory. You can do this from
the Sun™ ONE Directory Server console, or through any other administration tool
that supports LDAP and your directory's schema. User and group information is
stored in the external LDAP directory.
The principal-name used in the deployment descriptors must correspond to
your LDAP user information.
Solaris
The Solaris realm allows authentication using Solaris user name and password
data. This realm is supported only on Solaris 9, and has the following configuration
characteristics:
• Name: solaris
• Classname: com.iplanet.ias.security.auth.realm.file.SolarisRealm
Users and groups are stored in the underlying Solaris user database, as determined
by the system’s PAM (Pluggable Authentication Module) configuration.
NOTE The Solaris realm invokes the underlying PAM infrastructure for
authenticating. If the configured PAM modules require root privileges, the
instance must run as root to use this realm. For details, see the "Using
Authentication Services (Tasks)" chapter in the Solaris 9 System
Administration Guide: Security Services.
Certificate
The certificate realm supports SSL authentication. The certificate realm sets up the
user identity in Sun ONE Web Server's security context and populates it with user
data from the client certificate. The J2EE containers then handle authorization
processing based on each user's DN from his or her certificate. The certificate
realm has the following configuration characteristics:
• Name: certificate
• Classname:
com.iplanet.ias.security.auth.realm.certificate.CertificateRealm
You can add the following optional property to tailor the certificate realm
behavior:
• assign-groups: If this property is set, its value is taken to be a
comma-separated list of group names. All clients presenting valid certificates
are assigned membership to these groups for the purposes of authorization
decisions in the web container.
When you deploy an application, you must specify CLIENT-CERT as the
authentication mechanism in the web.xml file as follows:
<login-config>
<auth-method>CLIENT-CERT</auth-method>
</login-config>
You must obtain a client certificate and install it in your browser to complete the
setup for client certificate authentication. For details on how to set up the server
and client certificates, see the Sun ONE Web Server 6.1 Administrator’s Guide.
You can configure the server instance for SSL authentication in these ways:
104 Sun ONE Web Server 6.1 • Programmer’s Guide • August 2003
Realm Configuration
Custom Realm
You can create a custom realm by providing a Java™ Authentication and
Authorization Service (JAAS) login module and a realm implementation. Note that
client-side JAAS login modules are not suitable for use with Sun ONE Web Server.
For more information about JAAS, refer to the JAAS specification for Java 2 SDK,
v1.4, available here:
http://java.sun.com/products/jaas/
A sample application that uses a custom realm is available with Sun ONE Web
Server at the following location:
server_root/plugins/java/samples/webapps/security
Native Realm
The native realm is a special realm that provides a bridge between the core Sun
ONE Web Server ACL-based authentication and the J2EE/Servlet authentication
model. By using the native realm for Java web applications, it becomes possible to
have the ACL subsystem perform the authentication (instead of having the Java
web container do so) and yet have this identity available for Java web applications.
This functionality is provided by pluggable realm called NativeRealm, which acts
as a bridge between the J2EE security subsystem and the access control security
subsystem.
106 Sun ONE Web Server 6.1 • Programmer’s Guide • August 2003
Programmatic Login
NOTE While it is possible to apply both ACL access control rules and web.xml
security constraints on a single application, this usage is discouraged. It
may lead to duplicate authentication prompts or otherwise confusing
behavior. You should always pick either core ACL or J2EE web.xml-based
access control mechanisms for a given web application.
Programmatic Login
Programmatic login allows a deployed J2EE application to invoke a login method.
If the login is successful, a SecurityContext is established as if the client had
authenticated using any of the conventional J2EE mechanisms.
Programmatic login is useful for an application with unique needs that cannot be
accommodated by any of the J2EE standard authentication mechanisms.
This section discusses the following topics:
• Precautions
• Granting Programmatic Login Permission
• The ProgrammaticLogin Class
Precautions
The Sun ONE Web Server is not involved in how the login information (user name
and password) is obtained by the deployed application. Programmatic login places
the burden on the application developer with respect to assuring that the resulting
system meets security requirements. If the application code reads the
authentication information across the network, it is up to the application to
determine whether to trust the user.
Programmatic login allows the application developer to bypass the application
server-supported authentication mechanisms and feed authentication data directly
to the security service. While flexible, this capability should not be used without
some understanding of security issues.
Since this mechanism bypasses the container-managed authentication process and
sequence, the application developer must be very careful in making sure that
authentication is established before accessing any restricted resources or methods.
It is also the application developer's responsibility to verify the status of the login
attempt and to alter the behavior of the application accordingly.
The programmatic login state does not necessarily persist in sessions or participate
in single sign-on.
Lazy authentication is not supported for programmatic login. If an access check is
reached and the deployed application has not properly authenticated using the
programmatic login method, access is denied immediately and the application may
fail if not properly coded to account for this occurrence.
108 Sun ONE Web Server 6.1 • Programmer’s Guide • August 2003
Enabling the Java Security Manager
• Default Permissions
• Changing Permissions for an Application
Default Permissions
Internal server code is granted all permissions. These are covered by the
AllPermission grant blocks to various parts of the server infrastructure code. Do
not modify these entries.
Application permissions are granted in the default grant block. These permissions
apply to all code not part of the internal server code listed previously.
A few permissions above the minimal set are also granted in the default
server.policy file. These are necessary due to various internal dependencies of
the server implementation. J2EE application developers must not rely on these
additional permissions.
110 Sun ONE Web Server 6.1 • Programmer’s Guide • August 2003
For More Information
As a last resort, you can iteratively determine the permission set an application
needs by observing AccessControlException occurrences in the server log. If
this is not sufficient, you can add the -Djava.security.debug=all JVM option to
the server instance. For details, see the Sun ONE Web Server 6.1 Administrator's
Guide or the Sun ONE Web Server 6.1 Administrator's Configuration File Reference.
You can use the J2SE standard policytool or any text editor to edit the
server.policy file. For more information, see:
http://java.sun.com/docs/books/tutorial/security1.2/tour2/index.htm
l
For detailed information about the permissions you can set in the server.policy
file, see:
http://java.sun.com/j2se/1.4/docs/guide/security/permissions.html
The Javadoc for the Permission class is here:
http://java.sun.com/j2se/1.4/docs/api/java/security/Permission.html
Configuring Java The chapter “Securing Your Web Server” in the Sun ONE
security and Web Server 6.1 Administrator’s Guide.
realm-based
authentication
Certificates and public The chapter “Using Certificates and Keys” in the Sun ONE
key cryptography Web Server 6.1 Administrator’s Guide.
ACL-based security The chapter “Controlling Access to Your Server” in the Sun
ONE Web Server 6.1 Administrator’s Guide.
Configuring auth-db The chapter “Controlling Access to Your Server” in the Sun
in the dbswitch.conf ONE Web Server 6.1 Administrator’s Guide.
and server.xml files
112 Sun ONE Web Server 6.1 • Programmer’s Guide • August 2003
Chapter 6
This chapter describes how web applications are assembled and deployed in Sun
ONE Web Server, and has the following sections:
• Web Application Structure
• Creating Web Deployment Descriptors
• Deploying Web Applications
• Enabling and Disabling Web Applications
• Dynamic Reloading of Web Applications
• Classloaders
• The sun-web-app_2_3-1.dtd File
• Elements in the sun-web.xml File
• Sample Web Application XML Files
113
Creating Web Deployment Descriptors
+ hello/
|--- index.jsp
|--+ META-INF/
| |--- MANIFEST.MF
'--+ WEB-INF/
|--- web.xml
'--- sun-web.xml
114 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Deploying Web Applications
uri_path The URI prefix for the web application (requires a leading
“/”).
instance The server instance name.
vs_id The virtual server ID.
directory (optional) The directory to which the application is
deployed, or from which the application is deleted. If not
specified for deployment, the application is deployed to
instance_directory/webapps/vs_id/webappname. For
example:
/opt/SUNWwbsvr/https-test/webapps/
https-test/testapp
116 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Deploying Web Applications
When you execute the wdeploy deploy command, two things happen:
• A web application with the given uri_path and directory gets added to the
server.xml file.
For example:
http://acme.com:80/hello/index.jsp
- or -
http://acme.com/hello/
NOTE Please note that the Sun ONE Studio 5 plugin for Sun ONE Web
Server 6.1 works only with a local Web Server (that is, with the IDE
and the Web Server on the same machine).
For information about using the web application features in Sun ONE Studio 5,
explore the resources at
http://developers.sun.com/prodtech/javatools/reference/docs/index.h
tml
The behavior of the Sun ONE Studio 5 plugin for Sun ONE Web Server 6.1 is the
same as that for Sun™ ONE Application Server 7. If you’re using the "Web
Application Tutorial" at the site listed above, for instance, you would set the Sun
ONE Web Server 6.1 instance as the default, and then take the same actions
described in the tutorial.
Also see the following NetBeans tutorial
http://usersguide.netbeans.org/tutorials/webapps/index.html
118 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Enabling and Disabling Web Applications
NOTE For basic information about using Sun ONE Studio to debug web
applications, also see “Using Sun ONE Studio for Debugging” on
page 169 in this guide.
Example:
<WEBAPP uri="/catalog" path="/export/apps/catalog" enabled="false"/>
For more information, see the Sun ONE Web Server 6.1 Administrator’s
Configuration File Reference.
dynamicreloadinterval=integer
where integer specifies the interval (in seconds) after which a deployed application
will be checked for modifications and reloaded if necessary. To enable dynamic
reloading, you must specify a value greater than 0.
The server.xml setting is the default value for all applications. An individual
application can override the value for dynamicreloadinterval by specifying a
value to the class-loader element in the sun-web.xml file.
For information about sun-web.xml, see the following section, “The
sun-web-app_2_3-1.dtd File.” For details about server.xml, see the Sun ONE Web
Server 6.1 Administrator's Configuration File Reference.
In addition, to load new servlet files or reload deployment descriptor changes, you
must do the following:
1. Create an empty file named .reload at the root of the deployed module. For
example:
instance_dir/webapps/vs_id/uri/.reload
where vs_id is the virtual server ID in which the web application is deployed,
and uri is the value of the uri attribute of the <WEBAPP> element.
2. Explicitly update the .reload file's timestamp (touch .reload in UNIX) each
time you make the above changes.
For JSPs, changes are reloaded automatically at a frequency set in the
reload-interval property of the jsp-config element in the sun-web.xml file.
To disable dynamic reloading of JSPs, set the reload-interval property to -1.
120 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Classloaders
Classloaders
In a Java Virtual Machine (JVM), the classloaders dynamically load a specific Java
class file needed for resolving a dependency. For example, when an instance of
java.util.Enumeration needs to be created, one of the classloaders loads the
relevant class into the environment.
Classloaders in the Sun ONE Web Server 6.1 runtime follow the hierarchy shown
in the following figure.
Note that this is not a Java inheritance hierarchy, but a delegation hierarchy. In the
delegation design, a classloader delegates classloading to its parent before
attempting to load a class itself. If the parent classloader can't load a class, the
findClass()method is called on the classloader subclass. In effect, a classloader is
responsible for loading only the classes not available to the parent.
The exception is the Web Application Classloader, which follows the delegation
model in the Servlet specification. The Web Application Classloader looks in the
local classloader before delegating to its parent. You can make the Web Application
Classloader delegate to its parent first by setting delegate="true" in the
class-loader element of the sun-web.xml file. For more information, see
“Classloader Elements” on page 154.
The following table describes Sun ONE Web Server 6.1 classloaders. The left
column lists the classloaders, and the right column lists descriptions of those
classloaders and the files they examine.
122 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
The sun-web-app_2_3-1.dtd File
Web Application The Web Application Classloader loads the servlets and
other classes in a specific web application. That is, from
WEB-INF/lib and WEB-INF/classes and from any
additional classpaths specified in the extra-class-path
attribute of the class-loader element in sun-web.xml.
For more information, see “Classloader Elements” on
page 154.
An instance of this classloader is created for each web
application. If dynamic reloading has been enabled, any
changes made to these attributes/classes are reloaded by
the server without the need for a restart. For more
information, see “Dynamic Reloading of Web
Applications”.
JSP The JSP Classloader loads the compiled JSP classes of JSPs.
An instance of this classloader is created for each JSP file.
Any changes made to a JSP are automatically detected and
reloaded by the server, unless dynamic reloading of JSPs
has been disabled by setting the reload-interval
property to -1 in the jsp-config element of the
sun-web.xml file. For more information, see “jsp-config”
on page 156.
NOTE Do not edit the sun-web-app_2_3-1.dtd file. Its contents change only
with new versions of Sun ONE Web Server.
For general information about DTD files and XML, see the XML specification at:
http://www.w3.org/TR/REC-xml
Each element defined in a DTD file (which may be present in the corresponding
XML file) can contain the following:
• Subelements
• Data
• Attributes
Subelements
Elements can contain subelements. For example, the following file fragment
defines the cache element.
<!ELEMENT cache (cache-helper*, default-helper?, property*,
cache-mapping*)>
The ELEMENT tag specifies that a cache element can contain cache-helper,
default-helper, property, and cache-mapping subelements.
If an element cannot contain other elements, you see EMPTY or (#PCDATA) instead
of a list of element names in parentheses.
124 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
The sun-web-app_2_3-1.dtd File
Data
Some elements contain character data instead of subelements. These elements have
definitions of the following format:
<!ELEMENT element-name (#PCDATA)>
For example:
<!ELEMENT description (#PCDATA)>
In the sun-web.xml file, white space is treated as part of the data in a data element.
Therefore, there should be no extra white space before or after the data delimited
by a data element. For example:
<description>class name of session manager</description>
Attributes
Elements that have ATTLIST tags contain attributes (name-value pairs). For
example:
<!ATTLIST cachemax-capacity CDATA "4096"
timeout CDATA "30"
enabled %boolean; "false">
A cache element can contain max-capacity, timeout, and enabled
attributes.
The #REQUIRED label means that a value must be supplied. The #IMPLIED label
means that the attribute is optional, and that Sun ONE Web Server generates a
default value. Wherever possible, explicit defaults for optional attributes (such as
"true") are listed.
Attribute declarations specify the type of the attribute. For example, CDATA means
character data, and %boolean is a predefined enumeration.
NOTE Subelements must be defined in the order in which they are listed
under each Subelements heading, unless otherwise noted.
NOTE Each sun-web.xml file must begin with the following DOCTYPE header:
<!DOCTYPE sun-web-app PUBLIC '-//Sun Microsystems,
Inc.//DTD Sun ONE Web Server 6.1 Servlet 2.3//EN'
'http://www.sun.com/software/sunone/webserver/dtds/sun-
web-app_2_3-1.dtd'>
126 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Elements in the sun-web.xml File
General Elements
General elements are as follows:
• sun-web-app
• property
• description
sun-web-app
Defines Sun ONE Web Server-specific configuration for a web application. This is
the root element; there can only be one sun-web-app element in a sun-web.xml
file.
Subelements
The following table describes subelements for the sun-web-app element. The left
column lists the subelement name, the middle column indicates the requirement
rule, and the right column describes what the element does.
Attributes
none
Properties
The following table describes properties for the sun-web-app element. The left
column lists the property name, the middle column indicates the default value, and
the right column describes what the property does.
128 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Elements in the sun-web.xml File
property
Specifies a property, which has a name and a value. A property adds configuration
information to its parent element that is one or both of the following:
• Optional with respect to Sun ONE Web Server.
• Needed by a system or object that Sun ONE Web Server doesn't have
knowledge of, such as an LDAP server or a Java class.
For example, a manager-properties element can include property subelements:
<manager-properties>
<property name="reapIntervalSeconds" value="20" />
</manager-properties>
Which properties a manager-properties element uses depends on the value of
the parent session-manager element's persistence-type attribute. For details,
see the description of the session-manager element.
Subelements
The following table describes subelements for the property element. The left
column lists the subelement name, the middle column indicates the requirement
rule, and the right column describes what the element does.
Attributes
The following table describes attributes for the property element. The left column
lists the attribute name, the middle column indicates the default value, and the
right column describes what the attribute does.
description
Contains a text description of the parent element.
Subelements
none
Attributes
none
Security Elements
Security elements are as follows:
• security-role-mapping
• servlet
• servlet-name
• role-name
• principal-name
• group-name
security-role-mapping
Maps roles to users or groups in the currently active realm.
Subelements
The following table describes subelements for the security-role-mapping
element. The left column lists the subelement name, the middle column indicates
the requirement rule, and the right column describes what the element does.
130 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Elements in the sun-web.xml File
Attributes
none
servlet
Specifies a principal name for a servlet, which is used for the run-as role defined
in web-xml.
Subelements
The following table describes subelements for the servlet element. The left
column lists the subelement name, the middle column indicates the requirement
rule, and the right column describes what the element does.
Attributes
none
servlet-name
Contains data that specifies the name of a servlet, which is matched to a
servlet-name in web.xml. This name must be present in web.xml.
Subelements
none
Attributes
none
role-name
Contains data that specifies the role-name in the security-role element of the
web.xml file.
Subelements
none
Attributes
none
principal-name
Contains data that specifies a principal (user) name in the current realm.
Subelements
none
Attributes
none
group-name
Contains data that specifies a group name in the current realm.
Subelements
none
Attributes
none
132 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Elements in the sun-web.xml File
Session Elements
Session elements are as follows:
• session-config
• session-manager
• manager-properties
• store-properties
• session-properties
• cookie-properties
session-config
Specifies session configuration information.
Subelements
The following table describes subelements for the session-config element. The
left column lists the subelement name, the middle column indicates the
requirement rule, and the right column describes what the element does.
Attributes
none
session-manager
Specifies session manager information.
NOTE As of Sun ONE Web Server 6.1, you cannot define a session manager either
for a single sign-on session or for a virtual server. You must define session
managers at the level of web applications.
Subelements
The following table describes subelements for the session-manager element. The
left column lists the subelement name, the middle column indicates the
requirement rule, and the right column describes what the element does.
Attributes
The following table describes attributes for the session-manager element. The left
column lists the attribute name, the middle column indicates the default value, and
the right column describes what the attribute does.
134 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Elements in the sun-web.xml File
manager-properties
Specifies session manager properties.
Subelements
The following table describes subelements for the manager-properties element.
The left column lists the subelement name, the middle column indicates the
requirement rule, and the right column describes what the element does.
Attributes
none
Properties
The following table describes properties for the manager-properties element.
The left column lists the property name, the middle column indicates the default
value, and the right column describes what the property does.
store-properties
Specifies session persistence (storage) properties.
Subelements
The following table describes subelements for the store-properties element.
The left column lists the subelement name, the middle column indicates the
requirement rule, and the right column describes what the element does.
Attributes
none
Properties
The following table describes properties for the store-properties element. The
left column lists the property name, the middle column indicates the default value,
and the right column describes what the property does.
136 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Elements in the sun-web.xml File
session-properties
Specifies session properties.
Subelements
The following table describes subelements for the session-properties element.
The left column lists the subelement name, the middle column indicates the
requirement rule, and the right column describes what the element does.
Attributes
none
Properties
The following table describes properties for the session-properties element.
The left column lists the property name, the middle column indicates the default
value, and the right column describes what the property does.
cookie-properties
Specifies session cookie properties.
Subelements
The following table describes subelements for the cookie-properties element.
The left column lists the subelement name, the middle column indicates the
requirement rule, and the right column describes what the element does.
138 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Elements in the sun-web.xml File
Attributes
none
Properties
The following table describes properties for the cookie-properties element. The
left column lists the property name, the middle column indicates the default value,
and the right column describes what the property does.
cookiePath context path at Specifies the path name that is set when
which the web the session tracking cookie is created. The
application is browser sends the cookie if the path name
installed. for the request contains this path name. If
set to / (slash), the browser sends cookies
to all URLs served by the Sun ONE Web
Server. You can set the path to a narrower
mapping to limit the request URLs to
which the browser sends cookies.
cookieMaxAgeSecond -1 Specifies the expiration time (in seconds)
s after which the browser expires the cookie.
The default value of -1 indicates that the
cookie never expires.
cookieDomain (unset) Specifies the domain for which the cookie
is valid.
cookieComment Sun ONE Web Specifies the comment that identifies the
Server session session tracking cookie in the cookie file.
tracking cookie Applications can provide a more specific
comment for the cookie.
Reference Elements
Reference elements are as follows:
• resource-env-ref
• resource-env-ref-name
• resource-ref
• res-ref-name
• default-resource-principal
• name
• password
• jndi-name
resource-env-ref
Maps the res-ref-name in the corresponding J2EE web.xml file
resource-env-ref entry to the absolute jndi-name of a resource.
Subelements
The following table describes subelements for the resource-env-ref element.
The left column lists the subelement name, the middle column indicates the
requirement rule, and the right column describes what the element does.
Attributes
none
140 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Elements in the sun-web.xml File
resource-env-ref-name
Contains data that specifies the res-ref-name in the corresponding J2EE web.xml
file resource-env-ref entry.
Subelements
none
Attributes
none
resource-ref
Maps the res-ref-name in the corresponding J2EE web.xml file resource-ref
entry to the absolute jndi-name of a resource.
Subelements
The following table describes subelements for the resource-ref element. The left
column lists the subelement name, the middle column indicates the requirement
rule, and the right column describes what the element does.
Attributes
none
res-ref-name
Contains data that specifies the res-ref-name in the corresponding J2EE web.xml
file resource-ref entry.
Subelements
none
Attributes
none
default-resource-principal
Specifies the default principal (user) for the resource.
If this element is used in conjunction with a JMS Connection Factory resource, the
name and password subelements must be valid entries in Sun™ ONE Message
Queue's broker user repository.
Subelements
The following table describes subelements for the default-resource-principal
element. The left column lists the subelement name, the middle column indicates
the requirement rule, and the right column describes what the element does.
Attributes
none
name
Contains data that specifies the name of the principal.
Subelements
none
Attributes
none
password
Contains data that specifies the password for the principal.
Subelements
none
142 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Elements in the sun-web.xml File
Attributes
none
jndi-name
Contains data that specifies the absolute jndi-name of a URL resource or a
resource in the server.xml file.
NOTE To avoid collisions with names of other enterprise resources in JNDI, and
to avoid portability problems, all names in a Sun ONE Web Server
application should begin with the string java:comp/env.
Subelements
none
Attributes
none
Caching Elements
For details about response caching as it pertains to servlets, see “Caching Servlet
Results” on page 39. For details about JSP caching, see “JSP Cache Tags” on
page 53.
Caching elements are as follows:
• cache
• cache-helper
• default-helper
• cache-mapping
• url-pattern
• cache-helper-ref
• timeout
• refresh-field
• http-method
• key-field
• constraint-field
• value
cache
Configures caching for web application components.
Subelements
The following table describes subelements for the cache element. The left column
lists the subelement name, the middle column indicates the requirement rule, and
the right column describes what the element does.
Attributes
The following table describes attributes for the cache element. The left column
lists the attribute name, the middle column indicates the default value, and the
right column describes what the attribute does.
144 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Elements in the sun-web.xml File
Properties
The following table describes properties for the cache element. The left column
lists the property name, the middle column indicates the default value, and the
right column describes what the property does.
cache-helper
Specifies a class that implements the CacheHelper interface. For details, see
“CacheHelper Interface” on page 43.
Subelements
The following table describes subelements for the cache-helper element. The left
column lists the subelement name, the middle column indicates the requirement
rule, and the right column describes what the element does.
Attributes
The following table describes attributes for the cache-helper element. The left
column lists the attribute name, the middle column indicates the default value, and
the right column describes what the attribute does.
146 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Elements in the sun-web.xml File
default-helper
Allows you to change the properties of the built-in default cache-helper class.
Subelements
The following table describes subelements for the default-helper element. The
left column lists the subelement name, the middle column indicates the
requirement rule, and the right column describes what the element does.
Attributes
none
Properties
The following table describes properties for the default-helper element. The left
column lists the property name, the middle column indicates the default value, and
the right column describes what the property does.
cache-mapping
Maps a URL pattern or a servlet name to its cacheability constraints.
Subelements
The following table describes subelements for the cache-mapping element. The
left column lists the subelement name, the middle column indicates the
requirement rule, and the right column describes what the element does.
148 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Elements in the sun-web.xml File
Attributes
none
url-pattern
Contains data that specifies a servlet URL pattern for which caching is enabled. See
the Java Servlet 2.3 specification, section SRV 11.2 for applicable patterns.
Subelements
none
Attributes
none
cache-helper-ref
Contains data that specifies the name of the cache-helper used by the parent
cache-mapping element.
Subelements
none
Attributes
none
timeout
Contains data that specifies the cache-mapping specific maximum amount of time
in seconds that an entry can remain in the cache after it is created or refreshed. If
not specified, the default is the value of the timeout attribute of the cache element.
Subelements
none
Attributes
The following table describes attributes for the timeout element. The left column
lists the attribute name, the middle column indicates the default value, and the
right column describes what the attribute does.
refresh-field
Specifies a field that gives the application component a programmatic way to
refresh a cached entry.
Subelements
none
150 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Elements in the sun-web.xml File
Attributes
The following table describes attributes for the refresh-field element. The left
column lists the attribute name, the middle column indicates the default value, and
the right column describes what the attribute does.
http-method
Contains data that specifies an HTTP method that is eligible for caching. The
default is GET.
Subelements
none
Attributes
none
key-field
Specifies a component of the key used to look up and extract cache entries. The web
container looks for the named parameter, or field, in the specified scope.
If this element is not present, the web container uses the Servlet Path (the path
section that corresponds to the servlet mapping that activated the current request).
See the Servlet 2.3 specification, section SRV 4.4, for details on the Servlet Path.
Subelements
none
Attributes
The following table describes attributes for the key-field element. The left
column lists the attribute name, the middle column indicates the default value, and
the right column describes what the attribute does.
constraint-field
Specifies a cacheability constraint for the given url-pattern or servlet-name.
All constraint-field constraints must pass for a response to be cached. If there are
value constraints, at least one of them must pass.
Subelements
The following table describes subelements for the constraint-field element.
The left column lists the subelement name, the middle column indicates the
requirement rule, and the right column describes what the element does.
Attributes
The following table describes attributes for the constraint-field element. The
left column lists the attribute name, the middle column indicates the default value,
and the right column describes what the attribute does.
152 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Elements in the sun-web.xml File
value
Contains data that specifies a value to be matched to the input parameter value.
The matching is case sensitive. For example:
<value match-expr="in-range">1-60</value>
Subelements
none
Attributes
The following table describes attributes for the value element. The left column lists
the attribute name, the middle column indicates the default value, and the right
column describes what the attribute does.
Classloader Elements
Classloader elements are as follows:
• class-loader
class-loader
Configures the classloader for the web application.
Subelements
none
Attributes
The following table describes attributes for the class-loader element. The left
column lists the attribute name, the middle column indicates the default value, and
the right column describes what the attribute does.
154 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Elements in the sun-web.xml File
JSP Elements
JSP elements are as follows:
• jsp-config
jsp-config
Specifies JSP configuration information.
Subelements
The following table describes subelements for the jsp-config element. The left
column lists the subelement name, the middle column indicates the requirement
rule, and the right column describes what the element does.
Attributes
none
Properties
The following table describes properties for the jsp-config element. The left
column lists the property name, the middle column indicates the default value, and
the right column describes what the property does.
156 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Elements in the sun-web.xml File
javaCompilerPlugin internal JDK The fully qualified class name of the Java
compiler compiler plugin to be used. Not needed
(javac) for the default compiler.
For example, to use the jikes compiler
for JSP pages, set the
javaCompilerPlugin property to
org.apache.jasper.compiler.Jike
sJavaCompiler, then set the
javaCompilerPath property to point to
the jikes executable.
To use sun.tools.javac.Main to
compile JSP-generated servlets, set the
javaCompilerPlugin property to
org.apache.jasper.compiler.SunJ
avaCompiler (see also the
-deprecatedjavac switch of jspc,
described in “Compiling JSPs: The
Command-Line Compiler” on page 49).
javaCompilerPath none Specifies the path to the executable of an
out-of-process Java compiler such as
jikes. Ignored for the default compiler.
Needed only if the
javaCompilerPlugin property is
specified.
javaEncoding UTF8 Specifies the encoding for the generated
Java servlet. This encoding is passed to the
Java compiler used to compile the servlet
as well. By default, the web container tries
to use UTF8. If that fails, it tries to use the
javaEncoding value.
For encodings you can use, see:
http://java.sun.com/j2se/1.4/do
cs/guide/intl/encoding.doc.html
classdebuginfo false Specifies whether the generated Java
servlets should be compiled with the
debug option set (-g for javac).
keepgenerated true If set to true, keeps the generated Java
files. If false, deletes the Java files.
<jsp-config>
<property name=”initial-capacity” value=”1024” />
</jsp-config>
158 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Elements in the sun-web.xml File
Internationalization Elements
Internationalization elements are as follows:
• parameter-encoding
• locale-charset-info
• locale-charset-map
parameter-encoding
Specifies a hidden field or default charset that determines the character encoding
the web container uses to decode parameters for request.getParameter calls
when the charset is not set in the request's Content-Type.
For encodings you can use, see:
http://java.sun.com/j2se/1.4/docs/guide/intl/encoding.doc.html
Subelements
none
Attributes
The following table describes attributes for the parameter-encoding element. The
left column lists the attribute name, the middle column indicates the default value,
and the right column describes what the attribute does.
locale-charset-info
Specifies the mapping between the locale and the character encoding that should
be set in the Content-type header of the response if a servlet or JSP sets the
response locale using the ServletResponse.setLocale method. This overrides
the web container's default locale-to-charset mapping.
Subelements
The following table describes subelements for the locale-charset-info element.
The left column lists the subelement name, the middle column indicates the
requirement rule, and the right column describes what the element does.
Attributes
The following table describes attributes for the locale-charset-info element.
The left column lists the attribute name, the middle column indicates the default
value, and the right column describes what the attribute does.
locale-charset-map
Maps a locale to a specific character encoding.
For encodings you can use, see:
http://java.sun.com/j2se/1.4/docs/guide/intl/encoding.doc.html
160 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Elements in the sun-web.xml File
Attributes
The following table describes attributes for the locale-charset-map element. The
left column lists the attribute name, the middle column indicates the default value,
and the right column describes what the attribute does.
ja EUC-JP
zh UTF-8
162 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Elements in the sun-web.xml File
resource-env-ref
resource-env-ref-name
resource-ref
role-name
security-role-mapping
servlet
servlet-name
session-config
session-manager
session-properties
store-properties
sun-web-app
timeout
url-pattern
value
<web-app>
<display-name>i18n-simple</display-name>
<distributable></distributable>
<filter>
<filter-name>Simple Filter</filter-name>
<filter-class>samples.i18n.simple.servlet.SimpleFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>usefilter</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Simple Filter</filter-name>
<url-pattern>/SimpleFilterServlet</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>SimpleI18nServlet</servlet-name>
<servlet-class>samples.i18n.simple.servlet.SimpleI18nServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
164 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Sample Web Application XML Files
<servlet>
<servlet-name>IncludedServlet</servlet-name>
<servlet-class>samples.i18n.simple.servlet.IncludedServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>ForwardedServlet</servlet-name>
<servlet-class>samples.i18n.simple.servlet.ForwardedServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>SimpleFilterServlet</servlet-name>
<servlet-class>samples.i18n.simple.servlet.SimpleFilterServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>LocaleCharsetServlet</servlet-name>
<servlet-class>samples.i18n.simple.servlet.LocaleCharsetServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SimpleI18nServlet</servlet-name>
<url-pattern>/SimpleI18nServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>IncludedServlet</servlet-name>
<url-pattern>/IncludedServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ForwardedServlet</servlet-name>
<url-pattern>/ForwardedServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>SimpleFilterServlet</servlet-name>
<url-pattern>/SimpleFilterServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>LocaleCharsetServlet</servlet-name>
<url-pattern>/LocaleCharsetServlet</url-pattern>
</servlet-mapping>
<taglib>
<taglib-uri>/i18ntaglib</taglib-uri>
<taglib-location>/WEB-INF/tlds/i18ntaglib.tld</taglib-location>
</taglib>
</web-app>
<sun-web-app>
<session-config>
<session-manager/>
</session-config>
<cache enabled="true" timeout-in-seconds="300" >
<cache-mapping>
<servlet-name>ServCache</servlet-name>
<key-field name="inputtext" scope="request.parameter"/>
<constraint-field name="inputtext" scope="request.parameter">
<value>one</value>
<value>two</value>
</constraint-field>
</cache-mapping>
</cache>
</sun-web-app>
166 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Chapter 7
This chapter provides guidelines for debugging web applications in Sun ONE Web
Server, and includes the following sections:
• Enabling Debugging
• JPDA Options
• Using Sun ONE Studio for Debugging
• Debugging JSPs
• Generating a Stack Trace for Debugging
• Logging
• Profiling
Debugging applications requires that you edit the server.xml file as described in
this chapter. For more general information about this file, see the Sun ONE Web
Server 6.1 Administrator's Configuration File Reference.
Enabling Debugging
When you enable debugging, you enable both local and remote debugging.
You can enable debugging in one of these ways, as described in this section:
• Using the Administration Interface
• Editing the server.xml File
Sun ONE Application Server debugging is based on the JPDA (Java™ Platform
Debugger Architecture software). For more information, see “JPDA Options” on
page 168.
167
JPDA Options
For details about the server.xml file, see the Sun ONE Web Server 6.1
Administrator's Configuration File Reference.
JPDA Options
The default JPDA options are as follows:
-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n
If you substitute suspend=y, the JVM starts in suspended mode and stays
suspended until a debugger attaches to it. This is helpful if you want to start
debugging as soon as the JVM starts.
168 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Using Sun ONE Studio for Debugging
To specify the port to use when attaching the JVM to a debugger, specify
address=port_number.
You can include additional options. A list of JPDA debugging options is available
here:
http://java.sun.com/products/jpda/doc/conninv.html#Invocation
Debugging JSPs
When you use Sun ONE Studio to debug JSPs, you can set breakpoints in either the
JSP code or the generated servlet code, and you can switch between them and see
the same breakpoints in both.
To set up debugging in Sun ONE Studio, see the previous section.
For more information about the server.xml file, see the Sun ONE Web Server 6.1
Administrator's Configuration File Reference.
Logging
You can use the Sun ONE Web Server's log files to help debug your applications.
For general information about logging, see the Sun ONE Web Server 6.1
Administrator's Guide. For information about configuring logging in the
server.xml file, see the Sun ONE Web Server 6.1 Administrator's Configuration File
Reference.
You can change logging settings in one of these ways:
• Using the Administration Interface
• Editing the server.xml File
170 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Profiling
Profiling
You can use a profiler to perform remote profiling on the Sun ONE Web Server to
discover bottlenecks in server-side performance. This section describes how to
configure these profilers for use with Sun ONE Web Server:
• The HPROF Profiler
• The Optimizeit Profiler
The file option is important because it determines where the stack dump is
written in step 6.
The syntax of HPROF options is as follows:
-Xrunhprof[:help]|[:option=value,option2=value2, ...]
Using help lists options that can be passed to HPROF. The output is as follows:
Hprof usage: -Xrunhprof[:help]|[:<option>=<value>, ...]
Option Name and Value Description Default
--------------------- ----------- -------
heap=dump|sites|all heap profiling all
cpu=samples|old CPU usage off
format=a|b ascii or binary output a
file=<file> write data to file java.hprof
(.txt for ascii)
net=<host>:<port> send data over a socket write to file
depth=<size> stack trace depth 4
cutoff=<value> output cutoff point 0.0001
lineno=y|n line number in traces? y
thread=y|n thread in traces? n
doe=y|n dump on exit? y
172 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Profiling
2. You must also change a line in the Sun ONE Web Server start script. The start
script file is instance_dir/start. Change the following line:
PRODUCT_BIN=webservd-wdog
to this:
PRODUCT_BIN=webservd
3. Start the server by running the start script. Since the server runs in the
foreground (the change in step 2), the command prompt returns only after the
server has been stopped.
4. In another window or terminal, find the process ID of the server process.
% ps -ef | grep webservd
This command lists two webservd processes. Look at the PPID (parent process
ID) column and identify which of the two processes is the parent process and
which is the child process. Note the PID (process ID) of the child process ID.
5. Send a SIGQUIT signal (signal 3) to the child process:
% kill -QUIT child_PID
6. To stop the Web Server, run the stop script from another window.
% ./stop
This writes an HPROF stack dump to the file you specified using the file
HPROF option in step 1. For general information about using a stack dump,
see “Generating a Stack Trace for Debugging” on page 170.
7. To return your Web Server to its original configuration, undo the changes in
steps 1 and 2.
❍ Profiler: Enable
❍ Classpath: Optimizeit_dir/lib/optit.jar
❍ Native Lib Path: Optimizeit_dir/lib
❍ JVM Option: For each of these options, type the option in the JVM Option
field, select Add, then check its box in the JVM Options list:
• -DOPTITHOME=Optimizeit_dir
• -Xrunoii
• -Xbootclasspath/a:Optimizeit_dir/lib/oibcp.jar
• Edit the server.xml file as appropriate:
<!-- Optimizeit options -->
<PROFILER classpath="Optimizeit_dir/lib/optit.jar"
nativelibrarypath="Optimizeit_dir/lib" enabled="true">
<JVMOPTIONS>
-DOPTIT_HOME=Optimizeit_dir -Xboundthreads -Xrunoii
-Xbootclasspath/a:Optimizeit_dir/lib/oibcp.jar
</JVMOPTIONS>
</PROFILER>
In addition, you may need to set the following in your server.policy file:
grant codeBase "file:Optimizeit_dir/lib/optit.jar" {
permission java.security.AllPermission;
};
For more information about the server.policy file, see “The server.policy File”
on page 109.
When the server starts up with this configuration, you can attach the profiler. For
further details, see the Optimizeit documentation.
NOTE If any of the configuration options are missing or incorrect, the profiler
may experience problems that affect the performance of the Sun ONE Web
Server.
174 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Appendix A
Internationalization Issues
Servlets
This section describes how the Sun ONE Web Server determines the character
encoding for the servlet request and the servlet response.
For encodings you can use, see:
http://java.sun.com/j2se/1.4/docs/guide/intl/encoding.doc.html
Servlet Request
When processing the servlet request, the server uses the following order of
precedence, first to last, to determine the character encoding for the request
parameters:
• The ServletRequest.setCharacterEncoding() method.
• A hidden field in the form, if specified using the form-hint-field attribute of
the parameter-encoding element in the sun-web.xml file. For more
information about this element, see “parameter-encoding” on page 159.
• The character encoding specified in the default-charset attribute of the
parameter-encoding element in the sun-web.xml file. For more information
about this element, see “parameter-encoding” on page 159.
• The default encoding, which is ISO-8859-1.
175
JSPs
Servlet Response
When processing a servlet response, the server uses the following order of
precedence, first to last, to determine the response character encoding:
• The ServletResponse.setContentType() method or the
ServletResponse.setLocale() method.
JSPs
A JSP page uses a character encoding. For encodings you can use, see:
http://java.sun.com/j2se/1.4/docs/guide/intl/encoding.doc.html
The encoding can be described explicitly using the pageEncoding attribute of the
page directive. The character encoding defaults to the encoding indicated in the
contentType attribute of the page directive if it is given, or to ISO-8859-1.
For more information, see the “Localization Issues” chapter of the JSP 1.2
specification, which can be found at the following location:
http://java.sun.com/products/jsp/download.html
176 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Appendix B
Netscape Enterprise Server/ iPlanet Web Server 4.0 and 4.1 supported the Java
Servlet 2.1 specification. This specification did not include web applications. A
deployment scheme was developed to make servlet deployment simpler. With the
advent of Java web applications (.war files) and their deployment descriptors, it is
no longer necessary to maintain a proprietary deployment system.
iPlanet Web Server 6.0 supported both types of deployment schemes, but the 4.x
implementation (referred to as legacy servlets) was marked as deprecated (See
Chapter 8: “Legacy Servlet and JSP Configuration” of the iPlanet Web Server,
Enterprise Edition Programmer's Guide to Servlets).
Sun ONE Web Server 6.1 no longer supports legacy servlets. The legacy-style
properties files for the server you want to migrate (servlet.properties,
context.properties, and rules.properties) are removed during migration.
For more information about these files, see "Appendix A" in the Sun ONE Web
Server 6.1 Administrator’s Configuration File Reference.
Because there is no one-to-one mapping for all of the features, legacy servlets
cannot be migrated automatically. This section describes the main features
involved in migrating legacy servlets to web applications.
This appendix includes the following topics:
• JSP by Extension
• Servlet by Extension of Servlet by Directory
• Registered Servlets
177
JSP by Extension
In Sun ONE Web Server 6.1, JSP by extension works as it did in previous releases.
Any file in the document tree that is named as an extension of .jsp will be treated
as a JSP as long as the Java is turned on for the virtual server.
Registered Servlets
In the legacy servlet system there was a two-step process of registering servlets
(servlet.properties) and mapping them to a URL (rules.properties). In Sun
ONE Web Server 6.1, the servlets must be moved into a web application, and these
settings will be maintained in the web.xml file of that web application.
Example
A registered servlet will have entries in both the servlet.properties and
rules.properties files.
The following example uses a servlet file called BuyNow1A.class, which will
respond to /buynow. It is assumed that the web application is deployed at '/'.
The servlet.properties file has:
servlet.BuyNowServlet.classpath=D:/Netscape/server4/docs/servlet
/buy;D:/Netscape/server4/docs/myclasses
servlet.BuyNowServlet.code=BuyNow1A
servlet.BuyNowServlet.initArgs=arg1=45,arg2=online,arg3="quick
shopping"
178 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
/buynow=BuyNowServlet
<servlet>
<servlet-name> BuyNowServlet </servlet-name>
<servlet-class> BuyNow1A </servlet-class>
<init-param>
<param-name> arg1 </param-name>
<param-value> 45 </param-value>
</init-param>
<init-param>
<param-name> arg2 </param-name>
<param-value> online </param-value>
</init-param>
<init-param>
<param-name> arg3 </param-name>
<param-value> “quick shopping” </param-value>
</init-param>
</servlet
<servlet-mapping>
<servlet-name> BuyNowServlet </servlet-name>
<url-pattern> /buynow </url-pattern>
</servlet-mapping>
180 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Index
A HTTP basic 96
J2EE/Servlet-based 94
about secure web applications 95
JSPs 19, 47 SSL mutual 97
servlets 18, 25 authorization 90, 93
sessions 67 ACL-based 93
virtual servers 21 by servlets 100
web applications 17 client certificate 101
about this guide 11 constraints 101
audience 11 J2EE/Servlet-based 94
contents 14 secure web applications 95
other resources 12
accessing a session 70
Administration interface
more information about 13
Administration interface, using to B
change logging settings 170 binding objects to sessions 73
deploy web applications 115 Bootstrap Classloader 122
enable debugging 168
enable or disable web applications 119
use HPROF profiler 171
use Optimizeit profiler 173
AllPermission 110 C
application permissions 109 cache class names 145
changing 110
cache element 144
default 110
cache tags 53
application role mapping 91
cacheClassName property 145
ATTLIST tags 125
cache-helper 146
auth-constraint 101
CacheHelper interface 43
authentication 90, 93
ACL-based 93 cache-helper-ref 149
by servlets 95 CacheKeyGenerator interface 44
for single sign-on 98 cache-mapping 148
181
Section D
182 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Section E
E form-based login 97
editing server.xml
for debugging 167, 170
to change logging settings 171
to configure logging 23 G
to configure single sign-on 99
Get, overriding 30
to enable debugging 168
to enable dynamic reloading of web group-name 132
applications 120
to enable or disable web applications 119
to use HPROF profiling 172
to use Optimizeit profiler 174
elements in sun-web.xml 126
H
alphabetical list of 162 HPROF profiler 171
caching 143 HTTP basic authentication 96
classloader 154 http-method 151
general 127 HTTPS authentication 97
internationalization 159
JSP 156
reference 140
security 130
session 133 I
enabling
improving servlet performance 45
debugging 167
internationalization issues
IWSSessionManager 78
JSPs 176
MMapSessionManager 85
PersistentManager 76 servlets 175
StandardManager 75 internationalizing search 58
the Java Security Manager 109 invalidating a session 74
web applications 119 invoking servlets 37
encodings, supported 175, 176 IWSHttpSession 84
examples IWSHttpSession.java 84
caching 42 IWSSessionManager 78
sun-web.xml file 166 enabling 78
web applications 23 manager properties 79
web.xml file 164 source code 84
exceptions in JSP files 49 IWSSessionManager.java 84
F J
fetching client certificates 101 J2EE
file realm 102 application role mapping 91
FileStore.java 84 security model 88
Index 183
Section K
JAAS 105 K
Java class file, loading 121
key-field 151
Java Security Manager, enabling 109
Java Servlet 2.3 security model 88
Java Servlet API 18
JDBC 22
JDBC driver L
for session management 81 LDAP realm 102
JdbcStore.java 84 legacy servlets, migrating 177
JDPA options 168 library location, JSP tags 53
jndi-name 143 list of sun-web.xml elements 162
JSP locale-charset-info 160
about 47 locale-charset-map 160
by extension 178
logging 170
caching 22, 53
classloader 123 login mechanisms
command-line compiler 49 form-based 97
creating 48 HTTP basic authentication 96
debugging 169 SSL mutual authentication 97
ease of maintenance 49
handling exceptions 49
internationalization 176
overview 19
package names 52 M
parameters 52 manager-properties 135
portability of 49 migrating legacy servlets 177
specification 19 MMapSessionManager 85
standard portable tags 53 enabling 85
tag libraries 53 manager properties 85
using 47
JSP tags 53
cache 53
library location 53
search 57 N
jspc command 50 name element 142
advanced options 51
native realm 105
basic options 50
NativeRealm 105
example of 52
file specifiers 50
format of 50
jspc command-line tool 49
jsp-config 50, 156 O
Optimizeit profiler 173
overriding
184 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Section P
Index 185
Section S
186 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003
Section T
Index 187
Section V
W
WAR files 17, 20, 21, 113, 115, 116
wdeploy utility 116
Web Application Classloader 123
web applications 17
about 17
creating 20
database connection pooling 22
debugging 167
default 22
deploying 21, 113, 116
directory structure of 113
dynamic reloading of 120
enabling and disabling 119
examples 23
internationalization issues 175
Java Servlet and JSP specifications 17
response caching 40
securing 87
servlet and JSP caching 22
virtual servers 21
web container, configuring 23
web deployment descriptors 114
web.xml elements
auth-constraint 101
login-config 96
more information about 96
realm-name 98
res-ref-name 140
run-as role 127
security-role 100, 132
servlet-name 131
session-timeout 80, 86, 138
web.xml file 74, 114
about 100
creating 114
defining roles 100
example 164
188 Sun ONE Web Server 6.1 • Programmer’s Guide to Web Applications • August 2003