在一个页面上有一个form表单,我想在这个页面通过new一个javaBean来接收表单中的数据,然后通过session发送到服务器去。
具体代码如下:
[code=jsp]<%@page import="com.jintian.StuBean"%>
<%@page contentType="text/html;charset=gbk" %><html>
<head><title>小屋报道</title></head>
<body leftmargin="100" topmargin="50" background="image/3.jpg">
<center><table border="1" width=60% >
<tr><td width="40%"><img src="image/6_1.jpg"></td>
<td align="center"><form action="regServlet" method="post">
<b>输入学号:</b><input name="stuid" type="text"><p>
<b>输入姓名:</b><input name="stuname" type="text"><p>
<b>输入密码:</b><input name="pw" type="password" ><p>
<b>确认密码:</b><input name="repw" type="password"><p>
<p align="center">
<input type="submit" value="注册">
<input type="reset" value="重置">
</p>
</form></td>
</tr>
</table></center>

<%
StuBean stu = new StuBean();
System.out.println(request.getParameter("stuid"));
stu.setId(request.getParameter("stuid"));
stu.setName(request.getParameter("stuname"));
stu.setPw(request.getParameter("pw"));
stu.setRepw(request.getParameter("repw"));
session.setAttribute("student",stu);
%>

</body>
</html>[/code]

解决方案 »

  1.   

    通过session发送到服务器去。
    不太明白你要实现什么。除非你submit本页,不然request是不能得到上面的属性的
      

  2.   

    代码:
    <%@page import="com.jintian.StuBean"%>
    <%@page contentType="text/html;charset=gbk" %><html>
    <head><title>小屋报道</title></head>
    <body leftmargin="100" topmargin="50" background="image/3.jpg">
    <center><table border="1" width=60% >
    <tr><td width="40%"><img src="image/6_1.jpg"></td>
    <td align="center"><form action="regServlet" method="post">
    <b>输入学号:</b><input name="stuid" type="text"><p>
    <b>输入姓名:</b><input name="stuname" type="text"><p>
    <b>输入密码:</b><input name="pw" type="password" ><p>
    <b>确认密码:</b><input name="repw" type="password"><p>
    <p align="center">
    <input type="submit" value="注册">
    <input type="reset" value="重置">
    </p>
    </form></td>
    </tr>
    </table></center>

    <%
    StuBean stu = new StuBean();
    System.out.println(request.getParameter("stuid"));
    stu.setId(request.getParameter("stuid"));
    stu.setName(request.getParameter("stuname"));
    stu.setPw(request.getParameter("pw"));
    stu.setRepw(request.getParameter("repw"));
    session.setAttribute("student",stu);
    %>

    </body>
    </html>
      

  3.   

    这技术早就不实用了,要学就学能实用的,因为投入的学习精力是一样多的。建议你从Struts开始
      

  4.   

    不能获取到你的数据啊,javabean文件的内容是空的。
      

  5.   

    <form action="*.jsp" method="post">
    把action改成你页面名。
    建议用struts,方便
      

  6.   

    <form action="本页面.jsp" method="post">
      

  7.   

    如果要在当前页面获取属性值,需要把表单提交给自身页面,但是你的表单提交给了regServlet(action=regServlet)。所以点注册后在regServlet中读取StuBean中的属性值是null。更好点的做法应该是,action=regServlet不变,在RegServlet中先获取提交的表单值填充StuBean,再将StuBean存储到Session中。RegServlet处理完后,在显示注册结果的页面读取StuBean中的值显示(用jsp:useBean标记或EL语言都可以)。用自动填充表单和struts会更省事。