Java Tutorial/JSP/Variable

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

Creating a Variable

   <source lang="java">

<HTML>

 <HEAD>
   <TITLE>Creating a Variable</TITLE>
 </HEAD>
 <BODY>

Creating a Variable

   <%
       int days;
       days = 365;
       out.println("Number of days = " + days);
    %>
 </BODY>

</HTML></source>





Define variable and reference it later

   <source lang="java">

<%--

 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> <% } %></source>





Page counter

   <source lang="java">

<%@ 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.

<% int localCounter = 0;  %> This counter never increases its value: <%= ++localCounter %> </body> </html></source>

Reference variable

   <source lang="java">

<%--

 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 %>

<%

  value += 2;

%>

After adding 2, the value is <%= value %>

</source>