servlet 获取form表单值为空

ye-子殇 2013-05-25 01:05:52
jsp表单
<%@page contentType="text/html; charset=GB2312"%>
<html><body><font size=3>
<center>
<form action="getNumber" method=post>
商品  编号:<input type="text" name=number>
<p>商品  名称:<input type="text" name=name></p>
<p>商品起拍价:<input type="text" name=price></p>
<p>商品描述:<textArea name="content" rows="6" cols="20"></textArea></p>
<br/><input type="submit" value="提交">
</center>
</form></font></body></html>


servlet 获取值
package china.dalian;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

import bean.Upload;

@SuppressWarnings("serial")
public class Computer extends HttpServlet{

public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
response.setContentType("text/html;charset=GB2312");
Upload upload=new Upload();
request.setAttribute("upload", upload);
String number=request.getParameter("number");
System.out.print(number+"========================");
String name=request.getParameter("name");
String price=request.getParameter("price");
String content=request.getParameter("content");
if(number==null){
number=" ";
}
if(name==null){
name=" ";
}
if(price==null){
price=" ";
}
if(content==null){
content=" ";
}
upload.setNumber(number);
upload.setName(name);
upload.setPrice(price);
upload.setContent(content);
request.setAttribute("upload", upload);
request.getRequestDispatcher("show.jsp").forward(request, response);
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
doPost(request, response);
}
}

show.jsp输出
<%@page contentType="text/html; charset=GB2312"%>
<%@page import="bean.Upload" %>
<html><body><center>
<% Upload upload=(Upload)request.getAttribute("upload"); %>
输入的数为:
<br>名字:<%=upload.getName() %>
<br/>编号:<%=upload.getNumber() %>
<br/>价格:<%=upload.getPrice() %>
<br/>内容:<%=upload.getContent() %>
</center></body></html>

最后输出是为空的。大家帮忙看下那里出错了。
...全文
656 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
一叶飞舟 2013-05-28
  • 打赏
  • 举报
回复
保证servlet中每个form值都能获取到;保证页面跳转前保存的实体不是空的。
长笛党希望 2013-05-28
  • 打赏
  • 举报
回复
在servlet中设断点,逐行调试。
shuaiguo_18 2013-05-27
  • 打赏
  • 举报
回复
Upload upload=new Upload(); request.setAttribute("upload", upload); String number=request.getParameter("number"); System.out.print(number+"====================== 这个出错了,将request.setAttribute("upload", upload);将request.setAttribute("upload", upload);放在后面
java_cyy 2013-05-27
  • 打赏
  • 举报
回复
引用 7 楼 w304341736 的回复:
正解
引用 5 楼 whos2002110 的回复:
你这为空是指什么?? 是后台就无法从request中取出你想要的值,还是show.jsp没有输出. 先定位到问题,就容易解决了
+1
oh_Maxy 2013-05-25
  • 打赏
  • 举报
回复
如下修改: RequestDispatcher rd = request.getRequestDispatcher("show.jsp") request.setAttribute("upload", upload); rd.forward(request, response); 还不行就跟下代码,看看在Computer里,upload的操作的那些参数是否有问题。
  • 打赏
  • 举报
回复
(1)name="name",name="price" 这些加上双引号 (2)</form> </center>位置换一下 (3)request.setAttribute("upload", upload); 放在session中试试,request.getSession.setAttribute("upload", upload); (4)method="post"加上双引号 希望对楼主会有帮助、、、、、、
w304341736 2013-05-25
  • 打赏
  • 举报
回复
正解
引用 5 楼 whos2002110 的回复:
你这为空是指什么?? 是后台就无法从request中取出你想要的值,还是show.jsp没有输出. 先定位到问题,就容易解决了
  • 打赏
  • 举报
回复
引用 4 楼 a81418021 的回复:
[quote=引用 3 楼 liushangchao 的回复:] center 和 form 放错位置了吧
这些没问题的。 只是表达我的form在剧中位置而已[/quote] 你把 font 标签删了 试试 我刚把你代码复制了 eclipse 警告 说表单位置不正确
whos2002110 2013-05-25
  • 打赏
  • 举报
回复
你这为空是指什么?? 是后台就无法从request中取出你想要的值,还是show.jsp没有输出. 先定位到问题,就容易解决了
ye-子殇 2013-05-25
  • 打赏
  • 举报
回复
引用 3 楼 liushangchao 的回复:
center 和 form 放错位置了吧
这些没问题的。 只是表达我的form在剧中位置而已
  • 打赏
  • 举报
回复
center 和 form 放错位置了吧
ye-子殇 2013-05-25
  • 打赏
  • 举报
回复
引用 1 楼 whos2002110 的回复:
name属性引号加上,像这样name="number",name="price"
还是为空。。
whos2002110 2013-05-25
  • 打赏
  • 举报
回复
name属性引号加上,像这样name="number",name="price"

67,550

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧