表单提交的问题 在线等
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>我的JSP</title>
</head>
<body>
<p>java web application 1</p>
<%
String name1=request.getParameter("name1");
if (name1==null){
%>
<form action="jspform.jsp" method="post">
<table width="75%" border="1">
<tr>
<td bgcolor="#FFFF99" align="right" width="49%">姓名</td>
<td width="51%" colspan="2">
<input name="name1" type="text" id="name1">
</td>
</tr>
<tr>
<td align="right">性别</td>
<td colspan="2">
<input name="sex" type="radio" value="radiobutton" checked>
男
<input type="radio" name="sex" value="radiobutton">
女
</td>
</tr>
<tr>
<td bgcolor="#FFFF99"align="right">年龄</td>
<td colspan="2"><select name="age" size="1">
<option>20-30</option>
<option>30-40</option>
<option>50-60</option>
</select></td>
</tr>
<tr>
<td align="right">电子邮箱</td>
<td colspan="2"><input type="text" name="mail"></td>
</tr>
<tr>
<td bgcolor="#FFFF99" align="right" height="102">从事的职务</td>
<td colspan="2"><select name="titles" size="3" multiple>
<option>程序员</option>
<option>开发经理</option>
<option>WEB设计人员</option>
<option>数据库管理人员</option>
</select></td>
</tr>
<tr>
<td align="right" height="41">你的爱好</td>
<td colspan="2"><input name="favor" type="checkbox" id="favor" value="checkbox">
sport
<input type="checkbox" name="favor" value="checkbox">
travel
<input name="favor" type="checkbox" id="favor" value="checkbox">
music</td>
</tr>
</table>
<p>
<input type="submit" name="Submit" value="提交">
</p>
</form>
<%
}else
{
try{
String sex=request.getParameter("sex");
String age=request.getParameter("age");
String mail1=request.getParameter("mail1");
String[] titles =request.getParameterValues("titles");
String[] favor =request.getParameterValues("favor'");}
catch (Exception ex)
{
out.print(ex.getMessage());
}
%>
<p>你输入的信息如下</p>
<br>
<%=name1%>
<%=mail1%>
<%=sex%>
<%=age%>
<%
for (int i =0;i<titles.length;i++)
{
out.print(titles[i]);
}
for (int j =0;j<favor.length;j++)
{
out.print(favor[j]);
}
%>
<%}%>
</body>
</html>
各位帮我看一下是哪里错了
问题点数:100、回复次数:10Top
1 楼szpqq(Gavin)回复于 2003-12-01 18:50:55 得分 10
根据你自己输入的数据看看是输哪些数据错了,然后再在一些地方输入一些信息看看,看能不能找到问题,另外看变量在引用前是否得判断一下为空,例如
if(titles==null)
titles="";
Top
2 楼sandy91(雨儿)回复于 2003-12-01 18:59:55 得分 10
建议:把表单提交到另外一页,然后把变量都一一打印出来,可以帮助你排查一下错误!Top
3 楼lyqq(曾经是菜,现在还是菜!)回复于 2003-12-01 19:09:55 得分 0
<%=name1%>
<%=mail1%>
<%=sex%>
<%=age%>
这些变量的定义不应该在 try里面,因为try语句在if else 中的else中,第一次执行时,不会执行到else中的语句,所以错误为无法解析变量!Top
4 楼lingee1999(网上邻居)回复于 2003-12-01 19:18:11 得分 40
这样即可:::
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>我的JSP</title>
</head>
<body>
<p>java web application 1</p>
<%
String name1=request.getParameter("name1");
if (name1==null){
%>
<form action="jspform.jsp" method="post">
<table width="75%" border="1">
<tr>
<td bgcolor="#FFFF99" align="right" width="49%">姓名</td>
<td width="51%" colspan="2">
<input name="name1" type="text" id="name1">
</td>
</tr>
<tr>
<td align="right">性别</td>
<td colspan="2">
<input name="sex" type="radio" value="radiobutton" checked>
男
<input type="radio" name="sex" value="radiobutton">
女
</td>
</tr>
<tr>
<td bgcolor="#FFFF99"align="right">年龄</td>
<td colspan="2"><select name="age" size="1">
<option>20-30</option>
<option>30-40</option>
<option>50-60</option>
</select></td>
</tr>
<tr>
<td align="right">电子邮箱</td>
<td colspan="2"><input type="text" name="mail"></td>
</tr>
<tr>
<td bgcolor="#FFFF99" align="right" height="102">从事的职务</td>
<td colspan="2"><select name="titles" size="3" multiple>
<option>程序员</option>
<option>开发经理</option>
<option>WEB设计人员</option>
<option>数据库管理人员</option>
</select></td>
</tr>
<tr>
<td align="right" height="41">你的爱好</td>
<td colspan="2"><input name="favor" type="checkbox" id="favor" value="checkbox">
sport
<input type="checkbox" name="favor" value="checkbox">
travel
<input name="favor" type="checkbox" id="favor" value="checkbox">
music</td>
</tr>
</table>
<p>
<input type="submit" name="Submit" value="提交">
</p>
</form>
<%
}else
{
String sex="";
String age="";
String mail1="";
String[] titles=null;
String[] favor=null;
try{
sex=request.getParameter("sex");
age=request.getParameter("age");
mail1=request.getParameter("mail1");
titles =request.getParameterValues("titles");
favor =request.getParameterValues("favor'");}
catch (Exception ex)
{
out.print(ex.getMessage());
}
%>
<p>你输入的信息如下</p>
<br>
<%=name1%>
<%=mail1%>
<%=sex%>
<%=age%>
<%
if (titles!=null)
for (int i =0;i<titles.length;i++)
{
out.print(titles[i]);
}
if (favor!=null)
for (int j =0;j<favor.length;j++)
{
out.print(favor[j]);
}
%>
<%}%>
</body>
</html>
Top
5 楼lyqq(曾经是菜,现在还是菜!)回复于 2003-12-01 19:19:15 得分 40
大概改了一下,主要的改动都在后面!现在可以正常显示了!
提交以后还是有点问题,关系到中文显示的问题,和其他的一点问题。剩下的问题你自己应该可以解决!
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>我的JSP</title>
</head>
<body>
<p>java web application 1</p>
<%
String name1=request.getParameter("name1");
if (name1==null){
%>
<form action="jspform.jsp" method="post">
<table width="75%" border="1">
<tr>
<td bgcolor="#FFFF99" align="right" width="49%">姓名</td>
<td width="51%" colspan="2">
<input name="name1" type="text" id="name1">
</td>
</tr>
<tr>
<td align="right">性别</td>
<td colspan="2">
<input name="sex" type="radio" value="radiobutton" checked>
男
<input type="radio" name="sex" value="radiobutton">
女
</td>
</tr>
<tr>
<td bgcolor="#FFFF99"align="right">年龄</td>
<td colspan="2"><select name="age" size="1">
<option>20-30</option>
<option>30-40</option>
<option>50-60</option>
</select></td>
</tr>
<tr>
<td align="right">电子邮箱</td>
<td colspan="2"><input type="text" name="mail1"></td>
</tr>
<tr>
<td bgcolor="#FFFF99" align="right" height="102">从事的职务</td>
<td colspan="2"><select name="title" size="3" multiple>
<option>程序员</option>
<option>开发经理</option>
<option>WEB设计人员</option>
<option>数据库管理人员</option>
</select></td>
</tr>
<tr>
<td align="right" height="41">你的爱好</td>
<td colspan="2"><input name="favor" type="checkbox" id="favor" value="checkbox">
sport
<input type="checkbox" name="favor" value="checkbox">
travel
<input name="favor" type="checkbox" id="favor" value="checkbox">
music</td>
</tr>
</table>
<p>
<input type="submit" name="Submit" value="提交">
</p>
</form>
<%
}else
{
try{
String sex=request.getParameter("sex");
String age=request.getParameter("age");
String mail1=request.getParameter("mail1");
String[] titles =request.getParameterValues("title");
String[] favor =request.getParameterValues("favor");
%>
<p>你输入的信息如下</p>
<br>
<%=name1%>
<%=mail1%>
<%=sex%>
<%=age%>
<%
for (int i =0;i<titles.length;i++)
{
out.print(titles[i]);
}
for (int j =0;j<favor.length;j++)
{
out.print(favor[j]);
}
}
catch (Exception ex)
{
out.print(ex.getMessage());
}
%>
<%
%>
<%}%>
</body>
</html>Top
6 楼tangyanjun1(唐延军)回复于 2003-12-01 19:22:21 得分 0
报了什么错误?Top
7 楼lyqq(曾经是菜,现在还是菜!)回复于 2003-12-01 19:22:50 得分 0
关于中文显示的问题,可以这样解决!
在开头部分,加入一句
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%request.setCharacterEncoding("GBK");%> //这句话是新加的!Top
8 楼lyqq(曾经是菜,现在还是菜!)回复于 2003-12-01 19:23:39 得分 0
解决后的完整代码如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>我的JSP</title>
</head>
<body>
<p>java web application 1</p>
<%
String name1=request.getParameter("name1");
if (name1==null){
%>
<form action="jspform.jsp" method="post">
<table width="75%" border="1">
<tr>
<td bgcolor="#FFFF99" align="right" width="49%">姓名</td>
<td width="51%" colspan="2">
<input name="name1" type="text" id="name1">
</td>
</tr>
<tr>
<td align="right">性别</td>
<td colspan="2">
<input name="sex" type="radio" value="radiobutton" checked>
男
<input type="radio" name="sex" value="radiobutton">
女
</td>
</tr>
<tr>
<td bgcolor="#FFFF99"align="right">年龄</td>
<td colspan="2"><select name="age" size="1">
<option>20-30</option>
<option>30-40</option>
<option>50-60</option>
</select></td>
</tr>
<tr>
<td align="right">电子邮箱</td>
<td colspan="2"><input type="text" name="mail"></td>
</tr>
<tr>
<td bgcolor="#FFFF99" align="right" height="102">从事的职务</td>
<td colspan="2"><select name="titles" size="3" multiple>
<option>程序员</option>
<option>开发经理</option>
<option>WEB设计人员</option>
<option>数据库管理人员</option>
</select></td>
</tr>
<tr>
<td align="right" height="41">你的爱好</td>
<td colspan="2"><input name="favor" type="checkbox" id="favor" value="checkbox">
sport
<input type="checkbox" name="favor" value="checkbox">
travel
<input name="favor" type="checkbox" id="favor" value="checkbox">
music</td>
</tr>
</table>
<p>
<input type="submit" name="Submit" value="提交">
</p>
</form>
<%
}else
{
String sex="";
String age="";
String mail1="";
String[] titles=null;
String[] favor=null;
try{
sex=request.getParameter("sex");
age=request.getParameter("age");
mail1=request.getParameter("mail1");
titles =request.getParameterValues("titles");
favor =request.getParameterValues("favor'");}
catch (Exception ex)
{
out.print(ex.getMessage());
}
%>
<p>你输入的信息如下</p>
<br>
<%=name1%>
<%=mail1%>
<%=sex%>
<%=age%>
<%
if (titles!=null)
for (int i =0;i<titles.length;i++)
{
out.print(titles[i]);
}
if (favor!=null)
for (int j =0;j<favor.length;j++)
{
out.print(favor[j]);
}
%>
<%}%>
</body>
</html>Top
9 楼lyqq(曾经是菜,现在还是菜!)回复于 2003-12-01 19:24:34 得分 0
上一个落了一句话:
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%request.setCharacterEncoding("GBK");%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>我的JSP</title>
</head>
<body>
<p>java web application 1</p>
<%
String name1=request.getParameter("name1");
if (name1==null){
%>
<form action="jspform.jsp" method="post">
<table width="75%" border="1">
<tr>
<td bgcolor="#FFFF99" align="right" width="49%">姓名</td>
<td width="51%" colspan="2">
<input name="name1" type="text" id="name1">
</td>
</tr>
<tr>
<td align="right">性别</td>
<td colspan="2">
<input name="sex" type="radio" value="radiobutton" checked>
男
<input type="radio" name="sex" value="radiobutton">
女
</td>
</tr>
<tr>
<td bgcolor="#FFFF99"align="right">年龄</td>
<td colspan="2"><select name="age" size="1">
<option>20-30</option>
<option>30-40</option>
<option>50-60</option>
</select></td>
</tr>
<tr>
<td align="right">电子邮箱</td>
<td colspan="2"><input type="text" name="mail"></td>
</tr>
<tr>
<td bgcolor="#FFFF99" align="right" height="102">从事的职务</td>
<td colspan="2"><select name="titles" size="3" multiple>
<option>程序员</option>
<option>开发经理</option>
<option>WEB设计人员</option>
<option>数据库管理人员</option>
</select></td>
</tr>
<tr>
<td align="right" height="41">你的爱好</td>
<td colspan="2"><input name="favor" type="checkbox" id="favor" value="checkbox">
sport
<input type="checkbox" name="favor" value="checkbox">
travel
<input name="favor" type="checkbox" id="favor" value="checkbox">
music</td>
</tr>
</table>
<p>
<input type="submit" name="Submit" value="提交">
</p>
</form>
<%
}else
{
String sex="";
String age="";
String mail1="";
String[] titles=null;
String[] favor=null;
try{
sex=request.getParameter("sex");
age=request.getParameter("age");
mail1=request.getParameter("mail1");
titles =request.getParameterValues("titles");
favor =request.getParameterValues("favor'");}
catch (Exception ex)
{
out.print(ex.getMessage());
}
%>
<p>你输入的信息如下</p>
<br>
<%=name1%>
<%=mail1%>
<%=sex%>
<%=age%>
<%
if (titles!=null)
for (int i =0;i<titles.length;i++)
{
out.print(titles[i]);
}
if (favor!=null)
for (int j =0;j<favor.length;j++)
{
out.print(favor[j]);
}
%>
<%}%>
</body>
</html>
Top
10 楼duan17()回复于 2003-12-01 19:46:56 得分 0
我先试一下Top




