编写两个JSP页面,分别为inputName.jsp和people.jsp。

(1)inputName.jsp具体的要求

       该页面有个表单,用户通过该表单输入自己的姓名并提交给people.jsp页面。

 (2)people.jsp的具体要求

  1. 该页面有名字为person、类型是StringBuffer以及名字是count、类型为int的成员变量
  2. 该页面有public void judge()方法。该方法负责创建person对象,当count的值是0时,judge()方法创建person对象。
  3. 该页面有public void addPerson(String p)的方法,该方法将参数p指定的字符串添加到成员变量person末尾,同时将count作自增运算。
  4. 该页面在程序片中获取inputName.jsp页面提交的姓名,然后调用judge()创建person对象、调用addPerson方法将用户的姓名添加到成员变量person末尾。
  5. 如果inputName.jsp页面没有提交姓名,或姓名含有的字符个数大于10,就是用<jsp:forward page=”要转向的页面” />标记将用户转到inputName.jsp页面。
  6. 通过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,提示,如上图。

结果:

  • 2
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值