servlet 获取form表单值为空
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>
最后输出是为空的。大家帮忙看下那里出错了。
------解决方案--------------------你这为空是指什么?? 是后台就无法从request中取出你想要的值,还是show.jsp没有输出. 先定位到问题,就容易解决了