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

梦里梦外; 2024-03-02 09:43 23阅读 0赞

(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,就是用标记将用户转到inputName.jsp页面。
  6. 通过java表达式输出person和count的值。

inputName.jsp:

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <meta charset="UTF-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. <!-- 当输入超过10个时,提示 -->
  11. <%
  12. String messname=request.getParameter("mess");
  13. if(messname==null)
  14. messname="";
  15. %>
  16. <%=messname %>
  17. <!-- 表单 -->
  18. <form action="people.jsp" method="post">
  19. 姓名:<input type="text" name="rdName">
  20. <input type="submit" value="提交">
  21. </form>
  22. </body>
  23. </html>

people.jsp:

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <meta charset="UTF-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. <%request.setCharacterEncoding("UTF-8"); %>
  11. <%!
  12. int count;
  13. StringBuffer person;
  14. public void judge(){
  15. if(count==0)
  16. person=new StringBuffer(); /* 创建对象 */
  17. }
  18. public void addPerson(String p){
  19. if(count==0)
  20. person.append(p); /*append为追加字符串在末尾*/
  21. else
  22. person.append(','+p);
  23. count++;
  24. }
  25. %>
  26. 数据接收
  27. <%String name=request.getParameter("rdName");
  28. if(name==null || name.length()==0||name.length()>10){
  29. %>
  30. <jsp:forward page="inputName.jsp">
  31. <jsp:param value="请输入长度大于0且长度小于等于10的名称" name="mess"/>
  32. </jsp:forward>
  33. <% }
  34. judge();
  35. addPerson(name);
  36. %>
  37. <br>目前有<font color="blue" size='5'><%=count %></font>人浏览了该页面.
  38. <hr color="red">
  39. <br>姓名分别为:<%=person %>
  40. <br>
  41. <% out.print("<a href='javascript:history.back()'>返回</a> ");%>
  42. </body>
  43. </html>

afe88c29c0b143b89ca91b9784e0dc21.png

当输入长度大于10,提示,如上图。

结果:

c89688ca333f4214ba798a7a3648c286.png

发表评论

表情:
评论列表 (有 0 条评论,23人围观)

还没有评论,来说两句吧...

相关阅读

    相关 s的数字

    题目描述 输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的。 由于数组是排序的,采用双指针