传递参数时,为什么获取不到值?注:就是普通的表单(重新修改了,请高手赐教)
我的服务器配置window2000server+resin3.0+sql2000+ajax+hibernate
提交方法:
从一个表单发布提交到js文件,通过ajax方式传到servlet文件,在servlet文件,其他参数都能获得,就是其中一个text框的文件不能获取,在js打印是有值的。
但也不总是获不到值,如果手写的文本一般能获到,如果是将网上的新闻页面保存成档案格式的文件(*.mht),然后打开此文件,全选、复制,然后粘贴到表单文本框,保存时,在servlet就的到一个null值,而其他输入框的职都正常。很奇怪。
表单:
<form name= "frm " method= "post " action= "saveinfo ">
<table width= "100% " border= "0 " align= "center " cellpadding= "1 " cellspacing= "1 ">
<tr bgcolor= "#FFFFFF ">
<td width= "100% " colspan=2 align= "right " >
<table width= "100% " border= "0 " align= "center " cellpadding= "6 " cellspacing= "1 ">
<tr>
<td width= "8% " NOWRAP> 标 题: </td>
<td colspan= "3 ">
<input name= "title " type= "text " id= "title " value= " " size= "60 " maxlength= "100 "> </td>
</tr>
<tr>
<td colspan= "4 ">
<table width= "100% " border= "0 " cellpadding= "0 " cellspacing= "0 ">
<tr>
<td>
<textarea name= "nr " cols= "80 " rows= "15 "> </textarea> </td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan= "4 " align= "center ">
<input type= "button " value= "发布 " onClick= "checkValidity() ">
<input type= "button " value= "返回 " onClick= "javascript:history.back(); ">
</td>
</tr>
</table>
</form>
js文件:
function checkValidity()
{
var title = document.frm.title.value;
var content=document.frm.nr.value;
content=escape(content);
//alert(content);
var para= "title= "+title+ "&content= "+content;
//alert(para);
postRequest( "saveinfo ",para);
}
其他都是ajax方式处理的函数,略。
servlet文件:
package info;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.lang.*;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.*;
public class InfoSave extends HttpServlet
{
public InfoSave()
{
}
public void init(ServletConfig servletconfig)
throws ServletException
{
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
request.setCharacterEncoding( "utf-8 ");
String title=request.getParameter( "title ");
String content=request.getParameter( "content ");//信息内容,问题就出在这儿,获取不到值。
//获取最终信息内容content
if(content==null){
System.out.println( "null ");
content= "无内容 ";
} else if(content!=null && content.equals( " ")){
System.out.println( "not null ");
content= "无内容 ";
}
//System.out.println(content);
//获得发送者的职位编码
HttpSession httpsession = request.getSession();
Db db = new Db();
//保存信息
Info info=new Info();
info.settitle(title);
info.setcontent(content);
db.save(info);
response.setContentType( "text/xml; charset=utf-8 ");
response.setHeader( "Cache-Control ", "no-cache ");
PrintWriter out = response.getWriter();
out.println( " <response> ");
out.println( " <xxfl> 1 </xxfl> ");
out.println( " </response> ");
out.close();
}
protected void doPost(HttpServletRequest arg0, HttpServletResponse arg1)
throws ServletException, IOException
{
doGet(arg0, arg1);
}
}
[解决办法]
你手写文本可以传过去,但是将网上的新闻页面放到文本框里就不行了.
说明para的长度过长,超过规定,就传不过去了.因为你用的是doGet()方式,一般不允许超过1k,
你写到dopost()里试下.
[解决办法]
我也碰到过类似问题,很是郁闷。我当时的解决方法是:将 <form> 表单换成 <html:form> 表单。具体什么原因,不清楚。