Java Tutorial/JSP/Introduction

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

ASCII Table

   <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" %> <html> <body>

ASCII Table

<% StringBuffer sb = new StringBuffer(); sb.append(""); sb.append("");
  for (int col = 0; col < 16; col++) {
sb.append(""); } sb.append(""); for (int row = 0; row < 16; row++) { sb.append(""); sb.append(""); for (int col = 0; col < 16; col++) { char c = (char)(row * 16 + col); sb.append(""); } sb.append(""); } out.println(sb); %>
 ");
     sb.append(Integer.toHexString(col));
sb.append("
");
     sb.append(Integer.toHexString(row));
sb.append("
");
        sb.append(c);
sb.append("

</body> </html></source>





A Web Page with JSP code

   <source lang="java">

<HTML>

 <HEAD>
   <TITLE>A Web Page</TITLE>
 </HEAD>
 <BODY>
   <% out.println("Hello there!"); %>
 </BODY>

</HTML></source>





Declaration Tag Example

   <source lang="java">

<%!

 String name = "Joe";
 String date = "8th April, 2002";

%> <HTML>

 <TITLE>Declaration Tag Example</TITLE>
 <BODY>
   This page was last modified on <%= date %> by <%= name %>.
 </BODY>

</HTML></source>





Declaration Tag - Methods

   <source lang="java">

<%!

 private String getNephewName()
 {
   return "J";
 }
 private int getNephewAge()
 {
   return 3;
 }

%> <HTML>

 <HEAD><TITLE>Declaration Tag - Methods</TITLE></HEAD>
 <%= getNephewName() %>, age <%= getNephewAge() %>, is one funny kid!
 

</HTML></source>





Divide jsp page to several parts

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

  String[] colors = {"red", "green", "blue"};
  for (int i = 0; i < colors.length; i++) {

%>

<%= colors[i] %>

<%

  }

%></source>





Embedding Code

   <source lang="java">

<%!

 String[] names = {"A", "B", "C", "D"};

%> <HTML>

 <HEAD><TITLE>Embedding Code</TITLE></HEAD>
 <BODY>

List of people

     <% for (int i=0; i<names.length; i++) { %>
     <% } %>
Name
<%= names[i]%>
 </BODY>

</HTML></source>





JSP code tag

   <source lang="java">

<HTML>

 <HEAD>
   <TITLE>A Web Page</TITLE>
 </HEAD>
 <BODY>
   <% out.println("Hello there!"); %>
 </BODY>

</HTML></source>





Jsp page without Java code

   <source lang="java">

<%@ page language="java" contentType="text/html" %> <html> <head> </head> <body>

This is an HTML page.

This is a HTML table.

</body> </html></source>





Jsp page with XML namespace

   <source lang="java">

<jsp:root xmlns:jsp="http://java.sun.ru/JSP/Page" version="2.0"> <jsp:directive.page language="java" contentType="text/html" /> <html> <head> </head> <body>

This is an HTML page.

This is a HTML table.

</body> </html> </jsp:root></source>





Mix Jsp code with HTML tags

   <source lang="java">

<HTML>

 <HEAD>
   <TITLE>A Simple Java Server Page</TITLE>
 </HEAD>
 <BODY>

The current signal strength (0 to 10) is <%= (int) (Math.random() * 10) %>

(Refresh the page to see if the signal strength changes...)

 </BODY>

</HTML></source>





Multiple Declaration

   <source lang="java">

<%! int i; %> <%! void foo(){}%> <html>

 <body>
   <%! int j; %>

hello

     <%! void bar(){} %>
 </body>

</html></source>





Output HTML tags

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

  out.println("out is an ");
  out.println(out.getClass().getName());
  out.println(" object.");

%></source>





Output Selected System Properties in a table

   <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" %> <html> <head> <title>Selected System Properties</title> </head> <body>

Selected System Properties

<% final String[] propNames = { "java.awt.printerjob", "java.class.path", "java.class.version", "java.ext.dirs", "java.library.path", }; for (int i = 0; i < propNames.length; i++) { String name = propNames[i]; String value = System.getProperty(name); %> <% } %> </body>
<%= name %> <%= normalize(value) %>

</html> <%!

  private static final String normalize(String s)
  {
     StringBuffer sb = new StringBuffer();
     for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);
        sb.append(c);
        if (c == ";")
           sb.append("
"); } return sb.toString(); }

%></source>





Page session false

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

<% for (int c = 0; c <= 100; c += 10) { int f = 32 + 9*c/5; out.print(""); out.print(""); out.print(""); out.print(""); } %>
Celsius Fahrenheit
" + c + "" + f + "
</source>





Prints a conversion table of miles per gallon to kilometers per liter

   <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" %> <%@ page import="java.io.*" %> <%@ page import="java.text.*" %> <%@ page import="java.util.*" %> <%-- Prints a conversion table of miles per gallon

     to kilometers per liter --%>

<%!

  private static final DecimalFormat FMT
     = new DecimalFormat("#0.00");
  private static final double CONVERSION_FACTOR = 2.352145;

%> <html> <head> <title>Fuel Efficiency Conversion Chart</title> </head> <body>

Fuel Efficiency Conversion Chart

<% for (double kmpl = 5; kmpl <= 20; kmpl += 1.0) { double mpg = kmpl * CONVERSION_FACTOR; %> <% } %>
Kilometers per Liter Miles per Gallon
<%= FMT.format(kmpl) %> <%= FMT.format(mpg) %>

</body> </html></source>





Selected System Properties

   <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" %> <html> <head> <title>Selected System Properties</title> </head> <body>

Selected System Properties

<% final String[] propNames = { "java.awt.printerjob", "java.class.path", "java.class.version", "java.ext.dirs", "java.library.path", }; for (int i = 0; i < propNames.length; i++) { String name = propNames[i]; String value = System.getProperty(name); %> <% } %> </body>
<%= name %> <%= value %>

</html></source>





Single-line Java comments are copied to the generated servlet

   <source lang="java">

<%! // Single-line Java comments are copied to the generated servlet.

 /** This is a Javadoc comment, used to document Java functions */
 public void someMethod() {
   /* Multi-line Java comment, useful to comment out
      blocks of code during the development cycle.
      This sort of comment is copied to the generated servlet.
   */
 }

%> <HTML>

 <HEAD>
   <TITLE>Comments in a JSP</TITLE>
 </HEAD>
 <BODY>
   <% // A Java comment inside a scriptlet - copied into the generated servlet %>
   Java and JSP comments.
   <%-- A JSP comment - not copied to the servlet, or the output --%>
 </BODY>

</HTML></source>





Use out

   <source lang="java">

<%@ page language="java" %> <HTML>

 <HEAD><TITLE>JSP Example</TITLE></HEAD>
 <BODY>

Quadratic Equation: y = x^2

       <% 
         for (int i=0; i<10; i++)
out.print("");
       %>
xy
" + i + "" + (i*i) + "
 </BODY>

</HTML></source>





Using a Literal

   <source lang="java">

<HTML>

 <HEAD>
   <TITLE>Using a Literal</TITLE>
 </HEAD>
 <BODY>

Using a Literal

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

</HTML></source>