Java Tutorial/JSP/Methods — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 05:07, 1 июня 2010
Содержание
Creating a Method
<HTML>
<HEAD>
<TITLE>Creating a Method</TITLE>
</HEAD>
<BODY>
<H1>Creating a Method</H1>
<%!
int addem(int op1, int op2)
{
return op1 + op2;
}
%>
<%
out.println("2 + 2 = " + addem(2, 2));
%>
</BODY>
</HTML>
Declaring Multiple Methods
<HTML>
<HEAD>
<TITLE>Declaring Multiple Methods</TITLE>
</HEAD>
<BODY>
<H1>Declaring Multiple Methods</H1>
<%!
int addem(int op1, int op2)
{
return op1 + op2;
}
int subtractem(int op1, int op2)
{
return op1 - op2;
}
%>
<%
out.println("2 + 2 = " + addem(2, 2) + "<BR>");
out.println("8 - 2 = " + subtractem(8, 2) + "<BR>");
%>
</BODY>
</HTML>
Define function before HTML page
<%@ page language="java" contentType="text/html" %>
<%@ page import="java.util.Date" %>
<%!
private String getGreeting() {
Date now = new Date();
String greeting = null;
if (now.getHours() < 12) {
greeting = "Good morning";
}
else if (now.getHours() < 18) {
greeting = "Good day";
}
else {
greeting = "Good evening";
}
return greeting;
}
%>
<html>
<head>
<title>All Scripting Elements</title>
</head>
<body bgcolor="white">
<%= getGreeting() %>
<% if (request.getParameter("name") == null) { %>
stranger!
<% } else { %>
partner!
<% } %>
How are you?
</body>
</html>
Passing Arrays to Methods
<HTML>
<HEAD>
<TITLE>Passing Arrays to Methods</TITLE>
</HEAD>
<BODY>
<H1>Passing Arrays to Methods</H1>
<%!
void doubler(int a[])
{
for (int i = 0; i < a.length;i++) {
a[i] *= 2;
}
}
%>
<%
int array[] = {1, 2, 3, 4, 5};
out.println("Before the call to doubler...<BR>");
for (int i = 0; i < array.length;i++) {
out.println("array[" + i + "] = " + array[i] + "<BR>");
}
doubler(array);
out.println("After the call to doubler...<BR>");
for (int i = 0; i < array.length; i++) {
out.println("array[" + i + "] = " + array[i] + "<BR>");
}
%>
</BODY>
</HTML>
Passing the out Object to a Method
<HTML>
<HEAD>
<TITLE>Passing the out Object to a Method</TITLE>
</HEAD>
<BODY>
<H1>Passing the out Object to a Method</H1>
<%!
void printem(javax.servlet.jsp.JspWriter out) throws java.io.IOException
{
out.println("Hello from JSP!");
}
%>
<%
printem(out);
%>
</BODY>
</HTML>
Use page level function
<%@ page language="java" contentType="text/html" %>
<%!
String randomColor() {
java.util.Random random = new java.util.Random();
int red = (int) (random.nextFloat() * 255);
int green = (int) (random.nextFloat() * 255);
int blue = (int) (random.nextFloat() * 255);
return "#" +
Integer.toString(red, 16) +
Integer.toString(green, 16) +
Integer.toString(blue, 16);
}
%>
<html>
<head>
<title>Random Color</title>
</head>
<body bgcolor="white">
<h1>Random Color</h1>
<table bgcolor="<%= randomColor() %>" >
<tr><td width="100" height="100"> </td></tr>
</table>
</body>
</html>
Using Recursion
<HTML>
<HEAD>
<TITLE>Using Recursion</TITLE>
</HEAD>
<BODY>
<H1>Using Recursion</H1>
<%!
int factorial(int n)
{
if (n == 1) {
return n;
}
else {
return n * factorial(n - 1);
}
}
%>
<%
out.println("The factorial of 6 is " + factorial(6));
%>
</BODY>
</HTML>