(1)inputName.jsp具体的要求
该页面有个表单,用户通过该表单输入自己的姓名并提交给people.jsp页面。
(2)people.jsp的具体要求
- 该页面有名字为person、类型是StringBuffer以及名字是count、类型为int的成员变量
- 该页面有public void judge()方法。该方法负责创建person对象,当count的值是0时,judge()方法创建person对象。
- 该页面有public void addPerson(String p)的方法,该方法将参数p指定的字符串添加到成员变量person末尾,同时将count作自增运算。
- 该页面在程序片中获取inputName.jsp页面提交的姓名,然后调用judge()创建person对象、调用addPerson方法将用户的姓名添加到成员变量person末尾。
- 如果inputName.jsp页面没有提交姓名,或姓名含有的字符个数大于10,就是用<jsp:forward page=”要转向的页面” />标记将用户转到inputName.jsp页面。
- 通过java表达式输出person和count的值。
inputName.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 当输入超过10个时,提示 -->
<%
String messname=request.getParameter("mess");
if(messname==null)
messname="";
%>
<%=messname %>
<!-- 表单 -->
<form action="people.jsp" method="post">
姓名:<input type="text" name="rdName">
<input type="submit" value="提交">
</form>
</body>
</html>
people.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%request.setCharacterEncoding("UTF-8"); %>
<%!
int count;
StringBuffer person;
public void judge(){
if(count==0)
person=new StringBuffer(); /* 创建对象 */
}
public void addPerson(String p){
if(count==0)
person.append(p); /*append为追加字符串在末尾*/
else
person.append(','+p);
count++;
}
%>
数据接收
<%String name=request.getParameter("rdName");
if(name==null || name.length()==0||name.length()>10){
%>
<jsp:forward page="inputName.jsp">
<jsp:param value="请输入长度大于0且长度小于等于10的名称" name="mess"/>
</jsp:forward>
<% }
judge();
addPerson(name);
%>
<br>目前有<font color="blue" size='5'><%=count %></font>人浏览了该页面.
<hr color="red">
<br>姓名分别为:<%=person %>
<br>
<% out.print("<a href='javascript:history.back()'>返回</a> ");%>
</body>
</html>
当输入长度大于10,提示,如上图。
结果: