奇怪现象:
select.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*"
errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%!
public String codeToString(String str)
{
String s=str;
try
{
byte tempB[]=s.getBytes("ISO-8859-1");
s=new String(tempB);
return s;
}
catch(Exception e)
{
return s;
}
}
%>
<%
String sqlString;
// String sex;
String sex=codeToString(request.getParameter("sex"));
if(sex==null||sex.trim().length()==0)
sqlString="select * from information";
else
sqlString="select * from information where sex='"+sex.trim()+"'";
%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>综合查询</title>
</head>
<body>
<%
Connection con;
Statement sql;
ResultSet rs;
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
}
catch(ClassNotFoundException e)
{
out.print("找不到类!");
}
con=DriverManager.getConnection("jdbc:microsoft:sqlserver://MICROSOF-
BJER9M:1433;DatabaseName=basesql","sa","12345");
sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=sql.executeQuery(sqlString);
rs.last();
int rownumber =rs.getRow();
out.print("<table Border>");
out.print("<form action=selectform.jsp method=post>");
out.print("<tr><td>查询性别</td>");
out.print("<td colspan=2>");
out.print("<select name=sex>");
out.print("<option value=''>所有</option>");
out.print("<option value='男' selected>男</option>");
out.print("<option value='女' >女</option></select>");
out.print("<input type=submit value=提交>");
out.print("")
out.print("</td></tr></form>");
out.print("<tr><td colspan=3 align=center>用户数据</td></tr>");
out.print("<tr><td colspan=3 align=center>你查询的记录有:"+rownumber+"条
</td></tr>");
out.print("<tr>");
out.print("<td width=100>"+"用户姓名");
out.print("<td width=100>"+"用户性别");
out.print("<td width=100>"+"用户年龄");
out.print("</tr>");
rs.beforeFirst();
while(rs.next())
{
out.print("<tr>");
out.print("<td>"+rs.getString(2)+"</td>");
out.print("<td>"+rs.getString(3)+"</td>");
out.print("<td>"+rs.getString(4)+"</td>");
out.print("</tr>");
}
out.print("</table>");
con.close();
%>
</body>
</html>
当我运行这个页面的时候,首先出现的是所有性别的记录(男和女的全部);
可当俺把下面这几句删掉时:
out.print("<form action=selectform.jsp method=post>");
out.print("<tr><td>查询性别</td>");
out.print("<td colspan=2>");
out.print("<select name=sex>");
out.print("<option value=''>所有</option>");
out.print("<option value='男' selected>男</option>");
out.print("<option value='女' >女</option></select>");
out.print("<input type=submit value=提交>");
out.print("")
out.print("</td></tr></form>");
按理说,这几句 String sex=codeToString(request.getParameter("sex"));
应该不会发挥作用了(因为已经没有参数传递过来),可是再次(关了原先的页面,再重新输入)打开这个页面
时,这时候还是显示出所有的记录(男和女的全部);
俺以为是一开始初始化时,系统会把sex默认初始化为空,于是把
String sex=codeToString(request.getParameter("sex"));这句话注释掉
加上这句:String sex;
这样重新运行,系统说sex还没有初始化,为什么会是这样子的呢?????
俺原先以为把out.print("<form action=selectform.jsp method=post>");
out.print("<tr><td>查询性别</td>");
out.print("<td colspan=2>");
out.print("<select name=sex>");
out.print("<option value=''>所有</option>");
out.print("<option value='男' selected>男</option>");
out.print("<option value='女' >女</option></select>");
out.print("<input type=submit value=提交>");
out.print("")
out.print("</td></tr></form>");
这个删掉后,会显示出找不到sex的信息,却不知为何出现了这样的结果
在同一个页面提交表单和处理表单,应该先是执行<html>前面的代码,再执行<body></body>之间的内容吧,
顺序是这样的么?
[此贴子已经被作者于2006-8-5 16:35:30编辑过]