The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.servlet.jsp  [18 examples] > Java Server Pages Output  [4 examples]

e1058. Generating Dynamic Content on a JSP Page

The primary method for generating dynamic content on a JSP page is through the use of JSP expressions. A JSP expression is essentially a Java expression that is automatically converted to a String and written to the output stream. Here are some examples of JSP expressions:
    The value of Pi is: <%= Math.PI %>
    
    A random number between 1 and 100 is: <%= (int)(Math.random()*100)+1 %>
    
    The value of the "p" request paramter is: <%= request.getParameter("p") %>
    
    Today is: <%= new java.text.SimpleDateFormat("EEEE").format(new java.util.Date()) %>
Dynamic content can also be generated from a scriptlet by using out to write to the output stream. Here are some examples:
    <%
        if (Math.random() > .5) {
            out.println("<b>You win!</b>");
        } else {
            out.println("Sorry, you lose. Try again.");
        }
    %>

 Related Examples
e1057. Commenting a JSP Page
e1059. Implementing Conditional Content on a JSP Page
e1060. Handling Unhandled Exceptions in a JSP Page

See also: Java Server Pages    Java Server Pages Headers    Java Server Pages Input   


© 2002 Addison-Wesley.