Java Reference
In-Depth Information
Example 18−6: portal.jsp
<%@page language='java' contentType='text/html'%> <%-- On every JSP page --%>
<%-- Specify a tag library to use in this file --%>
<%@taglib uri='http://www.davidflanagan.com/tlds/decor_0_1.tld'
prefix='decor'%>
<%-- Include the JSP code from forcelogin.jsp when this page is compiled --%>
<%-- The included file checks if the user is logged in, and if not, --%>
<%-- forwards to the login.jsp page to get a username and password. This --%>
<%-- ensures that the 'username' attribute is defined in the session. --%>
<%@include file='forcelogin.jsp'%>
<%-- Declare a new variable named 'user'. It is an instance of UserBean --%>
<%-- and it is associated with the session object. If this is a new --%>
<%-- session, then instantiate the bean and set its 'userName' property --%>
<%-- to the value of the username from the session. --%>
<jsp:useBean id='user' scope='session'
class='com.davidflanagan.examples.servlet.UserBean'>
<jsp:setProperty name='user' property='userName'
value='<%=(String)session.getAttribute("username")%>'/>
</jsp:useBean>
<%-- Each time this page is displayed, set the 'favoriteColor' property of --%>
<%-- the 'user' bean. Since no 'value' parameter is supplied, set the --%>
<%-- property to the 'favoriteColor' parameter of the incoming request, if --%>
<%-- there is a parameter of that name; otherwise don't set the property. --%>
<jsp:setProperty name='user' property='favoriteColor'/>
<%-- Begin to output the page. Note the use of the JSP to include a Java --%>
<%-- expression within the HTML <body> tag. This is an explicit use of
--%>
<%-- 'user' bean declared above. --%>
<head><title>The Demo Portal</title></head>
<body bgcolor='<%=user.getFavoriteColor()%>'>
<%-- Greet the user by invoking the hello servlet and including its output --%>
<%-- here. The hello servlet expects to find the username in the session --%>
<%-- where the login.jsp page stored it. Note the difference between --%>
<%-- this runtime jsp:include tag and the @include directive used above --%>
<%-- to include the forcelogin.jsp file at compile-time. --%>
<div align=center>
<h1><jsp:include page='servlet/hello' flush='true'/></h1>
</div>
<%-- Display a box using our 'decor' tag library, and as its content --%>
<%-- use the value of the 'customContent' property of the 'user' bean --%>
<%-- Hopefully, the bean will provide interesting content tailored --%>
<%-- to the specified user --%>
<decor:box title='Your Custom Content'>
<jsp:getProperty name='user' property='customContent'/>
</decor:box>
<%-- Begin another box --%>
<%-- The content of this box is a table with 2 cells side-by-side --%>
<p>
<decor:box color='yellow' margin='15' borderColor='darkgreen'
title='Favorite Colors' titleAlign='center' titleColor='#aaffaa'>
<table><tr><td>
<%-- The first cell is another box. It displays some text and the value--%>
Search WWH ::




Custom Search