Java Tutorial/JSP/Introduction

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

ASCII Table

<%--
  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>
<center>
<h1>ASCII Table</h1>
<table border="0" cellpadding="0" cellspacing="0">
<%
   StringBuffer sb = new StringBuffer();
   sb.append("<tr>");
   sb.append("<td width=\"40\">&nbsp;</td>");
   for (int col = 0; col < 16; col++) {
      sb.append("<td>");
      sb.append(Integer.toHexString(col));
      sb.append("</td>");
   }
   sb.append("</tr>");
   for (int row = 0; row < 16; row++) {
      sb.append("<tr>");
      sb.append("<td>");
      sb.append(Integer.toHexString(row));
      sb.append("</td>");
      for (int col = 0; col < 16; col++) {
         char c = (char)(row * 16 + col);
         sb.append("<td width=\"32\" align=\"center\">");
         sb.append(c);
         sb.append("</td>");
      }
      sb.append("</tr>");
   }
   out.println(sb);
%>
</table>
</center>
</body>
</html>





A Web Page with JSP code

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





Declaration Tag Example

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





Declaration Tag - Methods

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





Divide jsp page to several parts

<%--
  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++) {
%>
<P><%= colors[i] %></P>
<%
   }
%>





Embedding Code

<%!
  String[] names = {"A", "B", "C", "D"};
%>
<HTML>
  <HEAD><TITLE>Embedding Code</TITLE></HEAD>
  <BODY>
    <H1>List of people</H1>
    <TABLE BORDER="1">
      <TH>Name</TH>
      <% for (int i=0; i<names.length; i++) { %>
        <TR><TD><%= names[i]%></TD></TR>
      <% } %>
    </TABLE>
  </BODY>
</HTML>





JSP code tag

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





Jsp page without Java code

<%@ page language="java"  contentType="text/html" %>
<html>
<head>
</head>
<body>
<h1> This is an HTML page. </h1>
<table border="1">
 <tr>
   <td>This is a HTML table.</td>
 </tr>
</table>
</body>
</html>





Jsp page with XML namespace

<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>
        <h1> This is an HTML page. </h1>
<table border="1">
 <tr>
   <td>This is a HTML table.</td>
 </tr>
</table>
</body>
</html>
</jsp:root>





Mix Jsp code with HTML tags

<HTML>
  <HEAD>
    <TITLE>A Simple Java Server Page</TITLE>
  </HEAD>
  <BODY>
    <H3 ALIGN="CENTER">
      The current signal strength (0 to 10) is 
      <FONT COLOR="RED">
        <%= (int) (Math.random() * 10) %>
      </FONT>
    </H3>
    <H4 ALIGN="CENTER">(Refresh the page to see if the signal strength changes...)</H4>
  </BODY>
</HTML>





Multiple Declaration

<%! int i;
%>
<%! void foo(){}%>
<html>
  <body>
    <%! int j; %>
hello
      <%! void bar(){} %>
  </body>
</html>





Output HTML tags

<%--
  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("<code>out</code> is an <i>");
   out.println(out.getClass().getName());
   out.println("</i> object.");
%>





Output Selected System Properties in a table

<%--
  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>
<h1>Selected System Properties</h1>
<table border="1" cellpadding="3" cellspacing="0">
<%
   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);
%>
<tr>
   <td align="left" valign="top"><%= name %></td>
   <td align="left" valign="top"><%= normalize(value) %></td>
</tr>
<% } %>
</body>
</table>
</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("<br>");
      }
      return sb.toString();
   }
%>





Page session false

<%--
  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" %>
<table border="0" align="center" width="50%">
<tr>
   <td align="right">Celsius</td>
   <td align="right">Fahrenheit</td>
</tr>
<%
   for (int c = 0; c <= 100; c += 10) {
      int f = 32 + 9*c/5;
      out.print("<tr>");
      out.print("<td align=\"right\">" + c + "</td>");
      out.print("<td align=\"right\">" + f + "</td>");
      out.print("</tr>");
   }
%>
</table>





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

<%--
  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>
<center>
<h1>Fuel Efficiency Conversion Chart</h1>
<table border="1" cellpadding="3" cellspacing="0">
<tr>
   <td>Kilometers per Liter</td>
   <td>Miles per Gallon</td>
</tr>
<%
   for (double kmpl = 5; kmpl <= 20; kmpl += 1.0) {
      double mpg = kmpl * CONVERSION_FACTOR;
%>
   <tr>
      <td align="right"><%= FMT.format(kmpl) %></td>
      <td align="right"><%= FMT.format(mpg) %></td>
   </tr>
<% } %>
</table>
</center>
</body>
</html>





Selected System Properties

<%--
  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>
<h1>Selected System Properties</h1>
<table border="1" cellpadding="3" cellspacing="0">
<%
   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);
%>
<tr>
   <td align="left" valign="top"><%= name %></td>
   <td align="left" valign="top"><%= value %></td>
</tr>
<% } %>
</body>
</table>
</html>





Single-line Java comments are copied to the generated servlet

<!-- Here is an HTML comment - copied to the output -->
<%! // 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>





Use out

<%@ page language="java" %>
<HTML>
  <HEAD><TITLE>JSP Example</TITLE></HEAD>
  <BODY>
    <H1>Quadratic Equation: y = x^2</H1>
      <TABLE BORDER="1">
        <TH>x</TH><TH>y</TH>
        <% 
          for (int i=0; i<10; i++)
            out.print("<TR><TD WIDTH="100">" + i + "</TD><TD WIDTH="100">" + (i*i) + "</TD></TR>");
        %>
      </TABLE>
  </BODY>
</HTML>





Using a Literal

<HTML>
  <HEAD>
    <TITLE>Using a Literal</TITLE>
  </HEAD>
  <BODY>
    <H1>Using a Literal</H1>
    <%
        out.println("Number of days = ");
        out.println(365);
     %>
  </BODY>
</HTML>