Using Struts Bean Tags That Access Aspects of the Servlet Context


This section provides information about the following tags:

  • <bean:cookie> ” Accesses a previously written cookie

  • <bean:header> ” Accesses information in the request header

  • <bean:parameter> ” Accesses a form parameter

  • <bean:page> ” Accesses information stored in a page, request, session, or application object

If you point your browser to the URL

 http://myAppServer/StrutsTaglibs/bean.jsp 

you'll bring up the main page linking to all the sample code for the Struts tag chapters. This section uses the Bean Servlet page at /StrutsTaglibs/BeanServlet.do . The rendered page is shown in Figure 13.2. See the Note on page 269, for details.

Figure 13.2. The Bean Servlet page.

graphics/13fig02.gif

This page demonstrates a typical use of each of the tags. The source is shown in Listing 13.1.

Listing 13.1 JSP File Demonstrating Use of Struts Bean Tags Accessing Aspects of the Servlet ( BeanServlet.jsp )
 <%@ page language="java" %> <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <%@ page import="javax.servlet.http.Cookie" %> <html:html> <head> <title>Bean Servlet sample code</title> </head> <body bgcolor="white"> <h3>Bean Servlet sample code</h3> <p>This page provides examples of the following Struts BEAN tags:<br> <ul> <li>&lt;bean:cookie&gt;</li> <li>&lt;bean:header&gt;</li> <li>&lt;bean:parameter&gt;</li> <li>&lt;bean:page&gt;</li> </ul> <table border="1" width="100%">   <%--   The following section contains creating and displaying a cookie.   --%>   <tr>     <td align="left">      <bean:cookie id="cookie" name="/tags/cookiedemo" value="firsttime"/>      <%        if (cookie.getValue().equals("firsttime")) {          Cookie c = new Cookie("/tags/cookiedemo", "Hi Mom!");          c.setComment("A test cookie");          c.setMaxAge(3600); //60 seconds times 60 minutes          response.addCookie(c);        }        %>       The cookie value is: <bean:write name="cookie" property="value"/><P>     </td>   </tr>   <%--   The following section gets a value from the request header.   --%>   <tr>      <td align="left">         <bean:header id="lang" name="Accept-Language"/>         The preferred language for this browser is: <bean:write name="lang"/><P>      </td>   </tr>   <%--   The following section looks at some parameters passed in.   --%>   <tr>      <td align="left">       <bean:parameter id="arg" multiple="yes" name="testarg" value="noarg"/>      Argument list:<BR>      <% for (int i = 0; i < arg.length; i++) {         out.write(arg[i] + "<BR>");      }      %>      <P>      <html:link page="/BeanServlet.do?testarg=123&testarg=456&testarg=789">      Click here to submit values to this page.</html:link>     </td>   </tr>   <%--   The following section finds a value from the session.   --%>   <tr>     <td align="left">        <bean:page id="this_session" property="session"/>        Session Created = <bean:write name="this_session"                           property="creationTime"/>     </td>   </tr> </table> </body> </html:html> 

The following sections outline functionality related to the tags demonstrated in this listing.

The <bean:cookie> Tag

The <bean:cookie> tag retrieves a cookie or cookies sent to the server from the browser. The id attribute is the name of the page context variable in which to store the result. The name attribute specifies the name of the cookie to match against. The value attribute enables you to give a value to be returned if no cookie is found. Note that this is a little more complicated than it sounds because the tag will actually create and return a fake cookie.

One attribute that's not shown, multiple , enables you to retrieve all the cookies that match a name, not just the default one that would be chosen by the server as the correct value. If you specify a nonblank value for this attribute, the id variable will be handed an array of type Cookie rather than a single Cookie . For a similar example, see the <bean:parameter> tag later in the chapter.

The example shows an attempt to read the /tags/cookiedemo cookie. If it isn't found, the default value of "firsttime" is placed into a dummy cookie. Later, if that value is found in the cookie, a new cookie with different values is placed on the response, with a one- hour timeout.

The <bean:header> Tag

If you need to retrieve one of the values transmitted to the server from the client in the HTTP request header, you can use the <bean:header> tag. The syntax is identical to the <bean:cookie> tag, with the exception that the values returned are strings or arrays of strings (in the case of the multiple attribute) as opposed to the pseudo-cookie objects returned by <bean:cookie> .

In the example, the client's language preference is extracted and printed.

The <bean:parameter> Tag

The <bean:parameter> tag enables you to explicitly get a parameter passed into this page (either by a form submit or by arguments in the URL). Again, the syntax is identical to the previous two tags, except that the name attribute now refers to a parameter name, rather than to a cookie or HTTP header value.

In this example, you see how the multiple attribute works.

The <bean:page> Tag

Sometimes you need to gain access to the request, session, response, Struts config, or application data. That's the purpose of the <bean:page> tag. You specify the variable you want to hold the object using the id attribute, and which object you want it to hold (which must be one of the strings "request" , "session" , "config" , "response" ,or "application" ).

This example uses the session object to find out when the session was created. You should consult the appropriate Javadoc for the object you're interested in to find out what properties are available.



Struts Kick Start
Struts Kick Start
ISBN: 0672324725
EAN: 2147483647
Year: 2002
Pages: 177

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net