主要问题
几乎相同的 deal.jsp 代码(index.jsp不变),在IDEA相同项目相互运行,结果却不一样,实在想不出来
主要项目
index.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>显示历史、显示添加留言功能</title>
<link rel="stylesheet" type="text/css" href="word_index.css" />
<script language="JavaScript">
function ifNull(form_Msg){
var userName = form_Msg.upload_UserName.value;
var userMsg = form_Msg.upload_UserMsg.value;
if (userName === "") {
alert("留言人不能为空");
form_Msg.upload_UserName.focus();
return false;
}
if (userMsg === "") {
alert("留言内容不能为空");
form_Msg.upload_UserMsg.focus();
return false;
}
}
</script>
</head>
<body>
<span class="word_Msg">留言板</span><br><br>
<%
if (session.isNew()){
int number_person_index = 0;
session.setAttribute("number_person",number_person_index);
}
int i;
String[] message_index = (String[])session.getAttribute("message");
int num = Integer.parseInt(session.getAttribute("number_person").toString());
if (num != 0) {
out.println("123213132");
out.println("number_person: " + num + "<br><br>");
for (i=0; i < num; i++) {
out.println(message_index[i]);
}
}
%>
<br><hr>
<form action="deal.jsp" id="form_UploadMassage" name="form_UploadMassage" method="post" onSubmit="return ifNull(form_UploadMassage)">
<span class="word_Msg">添加留言区域</span><br>
留言人:<input type="text" id="upload_UserName" name="upload_UserName" title="留言人">
留言内容:<textarea id="upload_UserMsg" name="upload_UserMsg" title="留言内容"></textarea>
<input name="Submit" type="submit" value="提交留言">
<input name="Submit2" type="reset" value="重置">
</form>
</body>
</html>
deal.jsp(正确可运行):
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%
request.setCharacterEncoding("utf-8");
String userName_deal=request.getParameter("upload_UserName"); //获取留言人
String userMsg_deal=request.getParameter("upload_UserMsg"); //获取留言内容
String message_add="["+userName_deal+"]说:"+userMsg_deal+"<br>"; //组合留言信息
int i;
int num = Integer.parseInt(session.getAttribute("number_person").toString());
int num_add = num+1;
String[] message_old = (String[])session.getAttribute("message");
String[] message_new = new String[num_add+1];
if(session.getAttribute("number_person")!=null){
if (num > 0) {
for (i=0; i<num; i++) {
message_new[i] = message_old[i];
}
}
message_new[num_add-1] = message_add;
}
session.setAttribute("number_person",num_add);
session.setAttribute("message",message_new); //将留言信息保存到session中
response.sendRedirect("index.jsp");
%>
deal.jsp(错误不可运行):
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%
request.setCharacterEncoding("utf-8");
String userName_deal = request.getParameter("upload_UserName");
String userMsg_deal = request.getParameter("upload_UserMsg");
String message_add = "["+userName_deal+"]说:"+userMsg_deal+"<br>"; //组合留言信息
int i;
int num = Integer.parseInt(session.getAttribute("number_person").toString());
int num_add = num+1;
String[] message_old = (String[])session.getAttribute("message");
String[] message_new = new String[num_add+1];
if (session.getAttribute("number_person")!=null) {
if (num > 0) {
for (i=0; i<num; i++) {
message_new[i] = message_old[i];
}
}
message_new[num_add-1] = message_add;
}
session.setAttribute("number_person",num_add);
session.setAttribute("massage",message_new);
response.sendRedirect("index.jsp");
%>