example.ejava 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?java
  2. import java.util.*;
  3. public class example {
  4. public static void main(String[] args) {
  5. String user = "Erubis";
  6. String[] list = { "<aaa>", "b&b", "\"ccc\"" };
  7. StringBuffer _buf = new StringBuffer();
  8. ?>
  9. <p>Hello @{user}@!</p>
  10. <table>
  11. <tbody>
  12. <?java for (int i = 0; i < list.length; i++) { ?>
  13. <tr bgcolor="@{i % 2 == 0 ? "#FFCCCC" : "#CCCCFF"}@">
  14. <td>@!{i + 1}@</td>
  15. <td>@{list[i]}@</td>
  16. </tr>
  17. <?java } ?>
  18. </tbody>
  19. </table>
  20. <?java
  21. System.out.print(_buf.toString());
  22. }
  23. public static String escape(String s) {
  24. StringBuffer sb = new StringBuffer();
  25. for (int i = 0; i < s.length(); i++) {
  26. char ch = s.charAt(i);
  27. switch (ch) {
  28. case '<': sb.append("&lt;"); break;
  29. case '>': sb.append("&gt;"); break;
  30. case '&': sb.append("&amp;"); break;
  31. case '"': sb.append("&quot;"); break;
  32. default: sb.append(ch);
  33. }
  34. }
  35. return sb.toString();
  36. }
  37. public static String escape(int i) {
  38. return Integer.toString(i);
  39. }
  40. }
  41. ?>