Article

The JSP Files - Parts 1 to 8: Tagged and Bagged

Page: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

You've Got Mail!

Now, our original thesis was that tag libraries would allow Web designers with no knowledge of JSP to implement complex functionality on a Web page. While the examples you've just seen were simple and illustrative, they haven't really proved that thesis - after all, it's reasonable to suppose that any competent Web designer would know how to manipulate date and time values via JavaScript and wouldn't really require a tag library for the purpose. So let's try something most client-side specialists wouldn't know how to handle - email delivery.

Typically, the process of generating email from a Web site is handled via a server-side script - Perl gurus open a pipe to sendmail, while PHP programmers reach for the mail() function. This implies a foreknowledge of either PHP or Perl - not something a novice Web designer may be expected to possess. What solution does JSP offer this poor soul?

The Jakarta MAILER library.

Using a couple of simple tags from this library, any Web developer can quickly add mail-processing capability to a Web page - in fact, the process is simpler than the equivalent techniques in Perl and PHP. Take a look.

<html>                                      
<head>                                      
</head>                                      
<body>                                      
                                     
<%@ taglib uri="jspmailer" prefix="jmail" %>                                      
                                     
<jmail:mail server="mail.server.com" to="recipient@server.com"                                        
from="sender@server.com" subject="This stuff is pretty cool!">                                        
<jmail:message> OK, you've convinced me - this tag library thing                                        
is awesome! Where do I sign up? </jmail:message> <jmail:send/>                                        
</jmail:mail>                                      
                                     
</body>                                      
</html>

By simplifying otherwise complex code, custom tag libraries like the one above can promote both efficiency and harmony in group development projects, by allowing the developers to concentrate on building better tools (libraries) and the designers to concentrate on presentation (rather than code). As JSP evolves, you can expect tag libraries to grow in importance - even now, they are by far one of the most compelling reasons to choose this server-side language over others.

Applet Antics

You've already seen how JSP "actions" work - in the last article, for example, we demonstrated the <jsp:useBean> and <jsp:setProperty> actions in conjunction with JavaBeans, while <jsp:include> was illustrated in the very first article in this series. However, we missed out on a couple of important ones - and so, we'd like to introduce you to <jsp:plugin>, used to incorporate Java applets into a Web page.

The <jsp:plugin> directive takes care of generating all the HTML code necessary to embed and activate a Java applet. Consider the following
example:

<html>                                      
<head>                                      
</head>                                      
<body>                                      
                                     
<jsp:plugin type="applet" code="NewsTicker.class"                                      
name="newsticker" height="100" width="100">                                      
                                     
<jsp:params>                                      
<jsp:param name="x" value="10"/>                                      
<jsp:param name="y" value="25"/>                                      
<jsp:param name="cx" value="90"/>                                      
<jsp:param name="cy" value="114"/>                                      
<jsp:param name="bgcolor" value="102,102,153"/>                                      
<jsp:param name="textcolor" value="0,0,0"/>                                      
<jsp:param name="hilitecolor" value="255,0,0"/>                                      
</jsp:params>                                      
                                     
<jsp:fallback>Oops! Something bad happened and I can't display this                                      
applet</jsp:fallback>                                      
                                     
</jsp:plugin>                                      
                                     
</body>                                      
</html>

The code above sets up the applet contained in "NewsTicker.class", and passes it a bunch of name-value pairs of parameters. The <jsp:param> tag is used to pass these parameters to the applet, while the <jsp:fallback> directive contains error text, in the event that the applet cannot be found or displayed.

When JSP compiles and renders the page, the code above is automatically converted to its HTML equivalent.

<html>                                      
<head>                                      
</head>                                      
<body>                                      
                                     
<OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"                                        
width="100" height="100" codebase="http://java.sun.com/products/                                      
plugin/1.2.2/jinstall-1_2_2-win.cab#V                                      
ersion=1,2,2,0">                                      
<PARAM name="java_code" value="NewsTicker.class">                                      
<PARAM name="type" value="application/x-java-applet;">                                      
<PARAM name="cy" value="114">                                      
<PARAM name="cx" value="90">                                      
<PARAM name="bgcolor" value="102,102,153">                                      
<PARAM name="hilitecolor" value="255,0,0">                                      
<PARAM name="y" value="25">                                      
<PARAM name="x" value="10">                                      
<PARAM name="textcolor" value="0,0,0">                                      
<COMMENT>                                      
<EMBED type="application/x-java-applet;"  width="100"  height="100"                                      
pluginspage="http://java.sun.com/products/plugin/"                                      
java_code="NewsTicker.class"                                      
cy=114                                      
cx=90                                      
bgcolor=102,102,153                                      
hilitecolor=255,0,0                                      
y=25                                      
x=10                                      
textcolor=0,0,0                                      
>                                      
<NOEMBED>                                      
</COMMENT>                                      
Oops! Something bad happened and I can't display this applet                                        
</NOEMBED></EMBED> </OBJECT>                                      
                                     
</body>                                      
</html>

And finally, the <jsp:forward> directive is used to automatically redirect a client request to another document. The following code would automatically redirect the client to the script "endzone.jsp".

<jsp:forward page="endzone.jsp" />

Just as in the previous example, additional parameters can be passed to the new script via <jsp:param>. For example,

<jsp:forward page="endzone.jsp">                                      
 <jsp:param name="user" value="joe" />                                      
 <jsp:param name="uid" value="653" />                                      
 <jsp:param name="gid" value="1220" />                                      
</jsp:forward>

And with that, it's about time to call this a wrap. We hope you enjoyed it, and that it served as a good starting point for your entry into the world of JSP.

If you're interested in learning more about the topics discussed in this series, take a look at our JSP section on Melonfire Trog, Sun Microsystems' JSP pages, Java documentation and references, or this tutorial on tag libraries. If, on the other hand, you have questions, comments, or large sums of money for us, drop us a line - we'd love to hear from you!

Until next time... stay healthy!

Note: All examples in this article have been tested on Linux/i586 with Tomcat 3.2 and JServ 1.1. Examples are illustrative only, and are not meant for a production environment. YMMV!
Copyright Melonfire, 2000. All rights reserved.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links

Rate This Article

  • 1
    Poor
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
    Great

Comment on This Article

Have something to say?

Post A Comment

You need to be a member of the SitePoint Forums to comment on this post. Sign Up

Already a member? Post using your SitePoint Forums account: