一、局部变量和全局变量
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'eg2.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%!int Num=0;
<%int count=0;
<h1><%="欢迎!" %></h1>
<br>
<%
Num++;
count++;
%>
<%="您是第"+Num+"个客人!" %>
<br>
<%="您是第"+count+"个客人"%>
</body>
</html>
二、求圆的面积和周长
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'eg3.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<P>请输入圆的半径</P>
<form action="eg3.jsp" method="get" name="form">
<input type="text" name="radius">
<input type="submit" name="sumbit" value="ok">
</form>
<%
%>
<%!double area(double r)
{
return Math.PI*r*r;
}
double perimeter(double r)
{
return Math.PI*2*r;
}
%>
<%
String str=request.getParameter("radius");
if(str!=null){
try{
double r;
r=Double.parseDouble(str);
%>
<p>圆的面积是:<%=area(r)%></p>
<p>圆的周长是:<%=perimeter(r)%></p>
<%
}catch(Exception e){
out.print(e.getMessage());
}
}
%>
</body>
</html>