使用request.getParameterValues()方法:
hobby.jsp:
<%@ page language="java" contentType="text/html;charset=GBK" %>
<html>
<head>
<title>
选择自己的爱好
</title>
</head>
<body>
<form name="myform" method="post" action="result.jsp">
选择自己的业余爱好:
<input type="checkbox" name="hobby" value="唱歌">唱歌
<input type="checkbox" name="hobby" value="跳舞">跳舞
<input type="checkbox" name="hobby" value="足球">足球
<input type="checkbox" name="hobby" value="篮球">篮球
<input type="submit" value="提交">
</form>
</body>
</html>
result.jsp:
<%@ page language="java" contentType="text/html;charset=GBK" %>
<html>
<head>
<title>
个人爱好
</title>
</head>
<body>
<%
request.setCharacterEncoding("GBK");
String strhobby="";
String[] hobbies=request.getParameterValues("hobby");
if(hobbies!=null)
{
for(int i=0;i<hobbies.length;i++)
{
strhobby+=hobbies[i]+" ";
}
}
%>
您选择的爱好:<b><%=strhobby %></b>
</body>
</html>
运行截图:
使用request.getParameter()方法:
猜数:guess.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
int guessNum=0;
int randomNum=0;
int count=0;
boolean success=false;
boolean compare=false;
try
{
guessNum=Integer.parseInt(request.getParameter("guessNum"));
randomNum=Integer.parseInt(request.getParameter("randomNum"));
count=Integer.parseInt(request.getParameter("count"));
if(guessNum==randomNum)
{
count++;
success=true;
}
else
{
count++;
success=false;
if(guessNum>randomNum)
{
compare=true;
}
else
{
compare=false;
}
}
if(success)
{
randomNum=(int) (Math.random()*100);
}
}catch(Exception e)
{
guessNum=0;
randomNum=0;
count=0;
success=false;
randomNum=(int) (Math.random()*100);
}
%>
<html>
<head>
<title>猜数</title>
<script type="text/javascript">
function isNum()
{
var num=document.getElementsByName("guessNum");
if(isNaN(num))
{
alert("这不是一个数字,请输入数字类型");
return false;
}
return true;
}
</script>
</head>
<body>
<h1>
猜一猜
</h1>
<%if(!success){ %>
<form action="guess.jsp" method="post" οnsubmit="return isNum()">
<input type="hidden" name="randomNum" value="<%=randomNum %>" />
<input type="hidden" name="count" value="<%=count %>" />
输入数字:<input type="text" name="guessNum" checked="checked"/>
<input type="submit" value="猜一下">
<%if(count!=0){ %>
<h3>
<%=compare?"猜大了":"猜小了" %>
</h3>
<%} %>
<h2>
你猜了<%=count %>次
</h2>
</form>
<%}else{ %>
恭喜你,猜对了,你猜了<%=count %>次
<a href="guess.jsp">再玩一次</a>
<%} %>
</body>
</head>
</html>
全局计数器:counter.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<html>
<head>
<title>
全局计数器
</title>
</head>
<body>
<%
int count=0;
if(application.getAttribute("count")!=null)
count=(Integer) application.getAttribute("count");
%>
一共有<%=++count %>次访问该页面
<%application.setAttribute("count", count) ;%>
</body>
</html>
显示日期Date.jsp:
<%@ page language="java" import="java.util.*,java.text.*" pageEncoding="gbk"%>
<html>
<head>
<title>
实现当前时间
</title>
</head>
<body>
<%
Date dt=new Date();
SimpleDateFormat smf=new SimpleDateFormat("yyyy年MM月dd日");
out.println(smf.format(dt));
%>
</body>
</html>
自动生成表格table.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<html>
<head>
<title>自动生成表格</title>
<style type="text/css">
<!--
.STYLE2{font-size:24px}
-->
</style>
</head>
<body>
<table width="279" border="1">
<caption class="STYLE2">自动生成的表格</caption>
<tr>
<th width="65" bgcolor="#99cc99" scope="col">第1列</th>
<th width="65" bgcolor="#99cc99" scope="col">第2列</th>
<th width="65" bgcolor="#99cc99" scope="col">第3列</th>
<th width="65" bgcolor="#99cc99" scope="col">第4列</th>
<th width="101" bgcolor="#99cc99" scope="col">第5列</th>
</tr>
<%for(int i=0;i<12;i+=5){ %>
<tr>
<td align="center"><%=i+1 %></td>
<td align="center"><%=i+2 %></td>
<td align="center"><%=i+3 %></td>
<td align="center"><%=i+4 %></td>
<td align="center"><%=i+5 %></td>
</tr>
<%} %>
</table>
</body>
</html>