实验要求:
编写两个JSP页面,名字分别为inputName.jsp和people.jsp
inputName.jsp的具体要求:
该页面有一个表单,用户通过该表单输入自己的姓名并提交给people.jsp页面
people.jsp的具体要求:
(1)JSP页面有名字为personList,类型是StringBuffer以及名字是count,类型为int的成员变量。
(2)JSP页面有public void judge()方法,该方法负责创建personList对象,当count的值是0时,judge()方法创建personList对象
inputName.jsp代码文件
<%@ page contentType="text/html;charset=utf-8" %>
<html>
<body>
<form action="people.jsp" method=get name=form>
请输入姓名:<input type="text"name ="name"><br>
<input type="submit" value="送出" name=submit>
</form>
</body>
</html>
people.jsp代码文件
<%@ page contentType="text/html;charset=utf-8" %>
<html>
<body>
<%!
int count;
StringBuffer personList;
public void judge()
{
if(count==0)
{
personList=new StringBuffer();
}
}
public void addPerson(String p)
{
if(count==0)
personList.append(p);
else
personList.append(','+p);
count++;
}
%>
<%
String name=request.getParameter("name");
if(name.length()==0||name.length()>10)
{
%>
<jsp:forward page="inputName.jsp"></jsp:forward>
<%
}
judge();
addPerson(name);
%>
<br>目前共有<%=count %>人浏览了该页面,他们的名字是:
<br><%=personList %>
</body>
</html>
浏览器显示结果: