Java Tutorial/JSP/Variable — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
 
м (1 версия)
 
(нет различий)

Текущая версия на 05:07, 1 июня 2010

Creating a Variable

<HTML>
  <HEAD>
    <TITLE>Creating a Variable</TITLE>
  </HEAD>
  <BODY>
    <H1>Creating a Variable</H1>
    <%
        int days;
        days = 365;
        out.println("Number of days = " + days);
     %>
  </BODY>
</HTML>





Define variable and reference it later

<%--
  Copyright (c) 2002 by Phil Hanna
  All rights reserved.
  
  You may study, use, modify, and distribute this
  software for any purpose provided that this
  copyright notice appears in all copies.
  
  This software is provided without warranty
  either expressed or implied.
--%>
<%!
   static final String[] COLORS = {
      "#CA9A26",
      "#3BF428",
      "#F7E339",
      "#FF40FF",
   };
%>
<%
   for (int i = 0; i < COLORS.length; i++) {
      String color = COLORS[i];
%>
<div style="background-color: <%= color%>;
            font-size: 12pt;
            font-weight: bold;">
   This is color <%= color %>
</div>
<% } %>





Page counter

<%@ page language="java" contentType="text/html" %>
<%! 
  int globalCounter = 0; 
%>
<html>
  <head>
    <title>A page with a counter</title>
  </head>
  <body bgcolor="white">
    This page has been visited: <%= ++globalCounter %> times.
    <P>
    <% 
      int localCounter = 0;
    %>
    This counter never increases its value: <%= ++localCounter %>
  </body>
</html>





Reference variable

<%--
  Copyright (c) 2002 by Phil Hanna
  All rights reserved.
  
  You may study, use, modify, and distribute this
  software for any purpose provided that this
  copyright notice appears in all copies.
  
  This software is provided without warranty
  either expressed or implied.
--%>
<%@ page session="false" %>
<%
   int value = 3;
%>
<P>The value is <%= value %></p>
<%
   value += 2;
%>
<P>After adding 2, the value is <%= value %></p>