Sample: Web Application (HelloUserWikExample)
Step 5: Create the Display Page (JSP)
The JSP page for the HelloUserWikExample application is called
app.jsp . The scriptlet code in this JSP page
uses methods from SAS Foundation Services to obtain the user's
name from the portal Web application session. The user's name
is then inserted into the text on the display page.
The code for app.jsp follows. The boxes contain
explanatory comments.
<%-- Copyright (c) 2003 by SAS Institute Inc., Cary, NC 27513 --%>
<%@ page language="java" contentType= "text/html; charset=UTF-8" %>
<%@ page import="com.sas.webapp.contextsharing.WebappContextParams" %>
<%@ page import="com.sas.services.webapp.ServicesFacade" %> <%@ page import="com.sas.services.user.UserContextInterface" %> <%@ page import="com.sas.services.session.SessionContextInterface" %>
<%@ page import="com.sas.services.user.UserServiceInterface" %>
<%@ page import="java.util.Enumeration" %>
<%@ page import="java.util.HashMap" %>
<%
try {
In the following code, a new WebappContextParams
object is created to obtain the session key from the
portal Web application request and to obtain a reference
to the portal Web application's remotely deployed services.
The UserContext of a privileged user is required
in order to create this object. The identity of the
privileged user is obtained from the metadata-privilegeduserid
parameter in the application's web.xml
file. |
// Use WebappContextParams to obtain information passed from the
// calling BIA application
UserServiceInterface userService = ServicesFacade.getUserService(); String privuser =
request.getSession(true).getServletContext().getInitParameter( "metadata-privilegeduserid"); UserContextInterface uc = userService.getUser(privuser); WebappContextParams params = new WebappContextParams(uc, request, true);
// See if we are being requested to display as a portlet
The following code determines whether a portlet ID
is available. The application uses this information
to determine whether to return a complete HTML page
(for display alone in a browser window) or an HTML fragment
(for display within a portlet). |
boolean displayAsPortlet = (params.getPortletid() == null) ? false :
true;
SessionContextInterface sharedSession = null;
Object sessionLock = null;
String user = null;
// Get the name of the user from the BIA shared session context
try {
The following code obtains access to the portal Web
application's remotely deployed services. The session
is protected with a lock. |
sharedSession = params.getSessionContext();
if (sharedSession != null) {
sessionLock = sharedSession.lock("com.sas.HelloUserWikExample");
The following code obtains the user's name from the
user context object in the remote services session. |
UserContextInterface userContext = sharedSession.getUserContext();
user = userContext.getName();
} else {
user = "unknown";
}
} catch (Throwable thr2) {
thr2.printStackTrace();
} finally {
The following code removes the lock. |
// Unlock the shared BIA session
try {
if ((sharedSession != null) && (sessionLock != null))
sharedSession.unlock(sessionLock);
} catch (Throwable thr3) {
// Non-fatal
}
}
The following code determines how to display the
application output. If the application was called by
a portlet, then it generates an HTML fragment for display
within the portlet. If the application was called as
a stand-alone application, then it generates a complete
HTML page to be displayed alone in a browser window. |
if (displayAsPortlet == false) {
// Called as a web application
%>
<html>
<head>
<title>Hello User Remote Application</title>
</head>
<body>
<p>Hello '<%= user %>'.</p>
</body>
</html>
<%
} else {
// Called as either a web application or portlet
%>
<p>Hello '<%= user %>'.</p>
<%
}
} catch (Throwable thr1) {
thr1.printStackTrace();
}
%>
|