一个JSP页面里有个form表单,其响应可以用本jsp来响应吧,当然里面有相应的Scriptlet
如下代码:
<%@ page language="java" import="java.util.*"%>
<%@ page contentType="text/html; charset=GBK"%><%!ArrayList list = null;%>
<%!public ArrayList addUser(String userName, String userIdcard) {
ArrayList ls = new ArrayList();
ls.add(userName);
ls.add(userIdcard);
return ls;
}%><%!public String tdUserName() {
String u = "";
for (int i = 0; i < list.size(); i++) {
if (i % 2 == 0) {
u = list.get(i).toString();
}
}
return u;
}%>
<%!public String tdUserIdCard() {
String uc = "";
for (int i = 0; i < list.size(); i++) {
if (i % 2 == 1) {
uc = list.get(i).toString();
}
}
return uc;
}%>
<%!public String tdUserBd() {
String idCard = tdUserIdCard();
String bd = idCard.substring(6, 10) + "-" + idCard.substring(10, 12)
+ "-" + idCard.substring(12, 14);
return bd;
}%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'uidConvert.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<form action="uidConvert.jsp" method="post">
<table width="400" border="0" align="center">
<tbody>
<tr>
<td>
&nbsp;姓&nbsp;&nbsp;名:
</td>
<td>
&nbsp;
<input name="Uname">
</td>
</tr> <tr>
<td>
&nbsp;身份证号码:
</td>
<td>
&nbsp;
<input name="Uidc">
</td>
</tr>
<tr>
<td>
&nbsp;
</td>
<td>
&nbsp;
<input type="submit" value="添加">
</td>
</tr>
</tbody>
</table> </form>
<br>
<hr>
<hr>
<table width="500" border="1" align="center">
<tbody>
<tr>
<td style="background-color: rgb(192, 192, 192);" align="center"
width="30%">
&nbsp;姓名
</td>
<td style="background-color: rgb(192, 192, 192);" align="center"
width="40%">
&nbsp;身份证号码
</td>
<td style="background-color: rgb(192, 192, 192);" align="center"
width="30%">
&nbsp;生日
</td>
</tr>
<%
String userName = request.getParameter("Uname");
String userIdcard = request.getParameter("Uidc");
list = addUser(userName, userIdcard);
%>
<tr>
<td>
<%=tdUserName()%>
</td>
<td>
<%=tdUserIdCard()%>
</td>
<td>
<%=tdUserBd()%>
</td>
</tr>
</tbody>
</table>
</body>
</html>

解决方案 »

  1.   

    你可以先把<%!.....%>中的内容封装的bean中呀,再放到request或seesion中返回来,这样就能看清你想干什么了,代码太多,看的累呀,呵呵
      

  2.   

    第一次load这个jsp页面时,request.getParameter("Uname")和request.getParameter("Uidc")的结果都是null,导致你在调用addUser函数时ArrayList ls里面装入了两个null,当然会出错了,楼主应该对这种情况做判断处理