ere is a jsp page that inserts four different snippets into a "what's new?" web page. each time the headlines change, authors only need to update the four files, but can leave the main jsp page unchanged.
whatsnew.jsp
you can also download the source or try it on-line.<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title>what's new</title>
<link rel=stylesheet
href="my-style-sheet.css"
type="text/css">
</head>
<body bgcolor="#fdf5e6" text="#000000" link="#0000ee"
vlink="#551a8b" alink="#ff0000">
<center>
<table border=5 bgcolor="#ef8429">
<tr><th class="title">
what's new at jspnews.com</table>
</center>
<p>
here is a summary of our four most recent news stories:
<ol>
<li><jsp:include page="news/item1.html" flush="true"/>
<li><jsp:include page="news/item2.html" flush="true"/>
<li><jsp:include page="news/item3.html" flush="true"/>
<li><jsp:include page="news/item4.html" flush="true"/>
</ol>
</body>
</html>
here's a typical result:
8.2 the jsp:usebean action
this action lets you load in a javabean to be used in the jsp page. this is a a very useful capability because it lets you exploit the reusability of java classes without sacrificing the convenience that jsp adds over servlets alone. the simplest syntax for specifying that a bean should be used is: <jsp:usebean id="name" class="package.class" />this usually means "instantiate an object of the class specified by
class, and bind it to a variable with the name specified by id." however, as we'll see shortly, you can specify a scope attribute that makes the bean associated with more than just the current page. in that case, it is useful to obtain references to existing beans, and the jsp:usebean action specifies that a new object is instantiated only if there is no existing one with the same id and scope. now, once you have a bean, you can modify its properties via jsp:setproperty, or by using a scriptlet and calling a method explicitly on the object with the variable name specified earlier via the id attribute. recall that with beans, when you say "this bean has a property of typex called foo", you really mean "this class has a method called getfoo that returns something of type x, and another method called setfoo that takes an x as an argument." the jsp:setproperty action is discussed in more detail in the next section, but for now note that you can either supply an explicit value, give a param attribute to say that the value is derived from the named request parameter, or just list the property to indicate that the value should be derived from the request parameter with the same name as the property. you read existing properties in a jsp expression or scriptlet by calling the appropriate getxxx method, or more commonly, by using the jsp:getproperty action.
note that the class specified for the bean must be in the server's regular class path, not the part reserved for classes that get automatically reloaded when they change. for example, in the java web server, it and all the classes it uses should go in the classes directory or be in a jar file in the lib directory, not be in the servlets directory.
here is a very simple example that loads a bean and sets/gets a simple string parameter.
beantest.jsp
you can also download the source or try it on-line.<!doctype html public "-//w3c//dtd html 4.0 transitional//en"> <html> <head> <title>reusing javabeans in jsp</title>