![]() | ![]() | ![]() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
documentation examples changes overview quick start installation command-line configuration admin amber clustering caching database deployment ejb 3.0 embedding filters hessian hmtp ioc jsp logging messaging performance quercus/php remoting scheduled tasks security server push servlets third-party troubleshooting virtual hosting watchdog webapp xml and xslt introduction compilation el jstl directives variables actions applications schema for jsp-2.0 .tld files velocity syntax jsp templates | actions
Actions are the core of JSP. Actions range from printing a script expression, to creating and storing a Java Bean. whitespaceJSP whitespace is copied to the output directly, unless escaped. In Resin 1.2, if you place a backslash immediately after a JSP action and before a line end, Resin will omit the end of line. In the following example, Resin keeps the whitespace after the and removes it after after the .<% @page session="true" %> a <%= 2 + 2 %>\ c a 4c <%= expression %>Prints the value of language. evaluated the page'sThe expression action is equivalent to the following:
out.print( );
It also has the following XML equivalent
<jsp:expression>
</jsp:expression>
The following simple example just prints the value of a form variable. Name: <%= request.getParameter("name") %> Name: George Washington <% scriptlet %>Executes the statements in language. using the page'sThe implicit variables, such as the request object and the out writer. is any statement list in the language, e.g. Java. The scriptlet can use any of theScriptlets have the following XML equivalent
<jsp:scriptlet>
</jsp:scriptlet>
<h1>Form results</h1> <pre> <% Enumeration e = request.getParameterNames(); while (e.hasMoreElements()) { String key = e.nextElement(); String value = request.getParameter(key); out.println(key + ": " + value); } %> </pre> <h1>Form results</h1> <pre> Name: George Washington Rank: General </pre> <%! declaration %>Adds code to the Servlet classJSP places the declaration code in the servlet class. In contrast, scriptlet and expression code are in a service method. So declarations can declare class variables and methods. Note Declarations are primarily useful for Java, but are allowed in JavaScript.Declarations have the following XML equivalent
<jsp:declaration>
</jsp:declaration>
<%= foo() %> <%! private int foo() { return 1329; } %> <jsp:include page="path"/>Includes the contents of the local URL at during runtime.jsp:include is a runtime action. It will call the included just as if its own HTTP request. The result of that page will be included in the current page.is relative to the current page. Its root is the root of the application. For compile-time includes, use <%@ include file='path'%> inc.jsp <%= 2 + 2 %> test.jsp Header <jsp:include page='inc.jsp'/> Footer Header 4 Footer <jsp:forward page="path" />Forwards the request to another page, i.e. an internal redirect. If the page has already written some output, jsp:request will clear the output buffer. is relative to the current page. fwd.jsp <%= 2 + 2 %> test.jsp Header <jsp:forward page='inc.jsp'/> Footer 4 <jsp:useBean id="name" ...>...Creates a new bean and variable for the page.
jsp:useBean enables a popular style JSP page creation where Java Beans calculate the content, and JSP formats the presentation. jsp:useBean creates an initializes a JSP bean for the page. The scope attribute determines the bean lifetime. For example, a session bean will be created once in a session. jsp:useBean assigns the bean to the variable . It will also store the bean in the appropriate scope variable. For example, an application bean "foo" will be stored in the application variable.jsp:useBean can also initialize beans. When jsp:useBean creates a new bean, it will execute the JSP in the jsp:useBean tag. Roughly, JSP makes the following translation: <jsp:useBean id='foo' class='com.caucho.test.TestBean' scope='session'> <% foo.myInitialization("test"); %gt; </jsp:useBean> com.caucho.test.TestBean foo; foo = (com.caucho.test.TestBean) session.getValue("foo"); if (foo == null) { foo = new com.caucho.test.TestBean(); session.value.foo = foo; foo.myInitialization("test"); } <jsp:getProperty name="name" ... />Prints a bean property.
jsp:getProperty converts property names following the bean standards. Roughly, jsp:getProperty makes the following conversion: <jsp:getProperty name='foo' property='bar'/> out.print(foo.getBar()); <jsp:setProperty ... value="value"/>Sets a bean property to .
If value is a runtime attribute, the bean property gets the expression value. If it's a static string, the value is first converted to the argument type and then set. <jsp:setProperty name='foo' property='count' value='10'/> foo.setCount(10); <jsp:setProperty name='foo' property='string' value='10'/> foo.setString("10"); <jsp:setProperty name='foo' property='count' value='<%= 2 + 2 %>'/> foo.setCount(2 + 2); <jsp:setProperty name='foo' property='count' value='2 + 2'/> error <jsp:setProperty name='foo' property='char' value='10'/> foo.setChar('1'); <jsp:setProperty ... param="param"/>Sets a bean property to a parameter value.
The second form of jsp:setProperty lets scripts easily set Bean properties to form values.
|