Expressions Example of a JSP with HTML and Embedded Java

13-6 Developing Web Applications, Servlets, and JSPs for Oracle WebLogic Server

13.6 Scriptlets

JSP scriptlets make up the Java body of your JSP servlets HTTP response. To include a scriptlet in your JSP page, use the shorthand or XML scriptlet tags shown here: Shorthand: Your Java code goes here XML: jsp:scriptlet Your Java code goes here jsp:scriptlet Note the following features of scriptlets: ■ You can have multiple blocks of scriptlet Java code mixed with plain HTML. ■ You can switch between HTML and Java code anywhere, even within Java constructs and blocks. In Section 13.8, Example of a JSP with HTML and Embedded Java the example declares a Java loop, switches to HTML, and then switches back to Java to close the loop. The HTML within the loop is generated as output multiple times as the loop iterates. ■ You can use the predefined variable out to print HTML text directly to the servlet output stream from your Java code. Call the print method to add a string to the HTTP page response. ■ Any time you print data that a user has previously supplied, Oracle recommends that you remove any HTML special characters that a user might have entered. If you do not remove these characters, your Web site could be exploited by cross-site scripting. For more information, refer to Section 13.10, JSP Expression Language . ■ The Java tag is an inline tag; it does not force a new paragraph.

13.7 Expressions

To include an expression in your JSP file, use the following tag: = expr Replace expr with a Java expression. When the expression is evaluated, its string representation is placed inline in the HTML response page. It is shorthand for out.print expr ; This technique enables you to make your HTML more readable in the JSP page. Note the use of the expression tag in the example in the next section. Expressions are often used to return data that a user has previously supplied. Any time you print user-supplied data, Oracle recommends that you remove any HTML special characters that a user might have entered. If you do not remove these characters, your Web site could be exploited by cross-site scripting. For more information, refer to Section 13.10, JSP Expression Language .

13.8 Example of a JSP with HTML and Embedded Java

The following example shows a JSP with HTML and embedded Java: WebLogic JSP Reference 13-7 html headtitleHello World Testtitlehead body bgcolor=ffffff center h1 font color=DB1260 Hello World Test fonth1 font color=navy out.printJava-generated Hello World; font p This is not Java piMiddle stuff on pagei p font color=navy for int i = 1; i=3; i++ { h2This is HTML in a Java loop = i h2 } font center body html After the code shown here is compiled, the resulting page is displayed in a browser as follows:

13.9 Actions