getParameter(): 使用 request.getParameter() 方法来获取表单参数的值。
实现简单加法计算:
<html>
<head>
<title>加法运算</title>
</head>
<body>
<%
int num1 = 0;
int num2 = 0;
int sum = 0;
if (request.getParameter("num1") != null && request.getParameter("num2") !=null){
String s1 = request.getParameter("num1");
String s2 = request.getParameter("num2");
num1 = Integer.parseInt(s1);
num2 = Integer.parseInt(s2);
sum=num1 + num2;
}
%>
<form action="" method="get">
<input type="text" name="num1" value="<%=num1%>">+
<input type="text" name="num2" value="<%=num2%>">=
<input type="text" name="sum" value="<%=sum%>"><br><br>
<input type="submit" name="submit" value="提交">
<input type="reset" name="reset" value="取消">
</form>
</body>
</html>
使用application对象:getAttribute()方法和setAttribute()方法,实现访问量统计:
<html>
<head>
<title>访问量统计</title>
</head>
<body>
<%
Integer count = (Integer)application.getAttribute("counter");
if(count ==null ){
count = 1;
}else{
count += 1;
}
application.setAttribute("counter", count);
%>
本页面访问量为:<%= count%>次!
</body>
</html>