var xmlHttp;
var username;
var title;
var content;
function createXmlHttp()
{
if(window.XMLHttpRequest)
{
xmlHttp=new XMLHttpRequest();

}
else
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
function submitPost()
{
username=document.getElementById("username").value;
title=document.getElementById("post_title").value;
content=document.getElementById("post_content").value;

if(checkForm())
{
displayStatus("waiting....");

createXmlHttp();
xmlHttp.onreadystatechange=submitPostCallBack;
var param="?title="+document.getElementById("post_title").value+"&content="+document.getElementById("post_content").value+"&username="+document.getElementById("username").value;
xmlHttp.open("POST","http://localhost:8080/www/bbs_post.jsp",true);
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send(param); }
}function checkForm()
{
if(username=="")
{
alert("name");
return false;
}
else if(title=="")
{
alert("title");
return false;
}
else if(content=="")
{
alert("content");
return false;
}
return true;
}function submitPostCallBack()
{
if(xmlHttp.readyState==4)
{
createNewPost(xmlHttp.responseText);
hiddenStatus();

}
}
function createNewPost()
{
document.getElementById("post_title").value="";
document.getElementById("post_content").value="";
document.getElementById("username").value=""; var postDiv=createDiv("post","");
var postTitleDiv=createDiv("post_title",title+"["+username+"]");
var postContentDiv=createDiv("post_content","<pre>"+content+"</pre>");
postDiv.appendChild(postTitleDiv);
postDiv.appendChild(postContentDiv);
document.getElementById("m_table").appendChild(postDiv);
}function createDiv(className,text)
{
var newDiv=document.createElement("div");
newDiv.className=className;
newDiv.innerHTML=text;
return newDiv;
}function displayStatus(info)
{
var statusDiv=document.getElementById("statusDiv");
statusDiv.innerHTML=info;
statusDiv.style.display="";
}function hiddenStatus()
{
var statusDiv=document.getElementById("statusDiv");
statusDiv.innerHTML="";
statusDiv.style.display="none";
}
jsp页面~~<%@ page contentType="text/plain; charset=gb2312"%>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.sql.*"%> <%
out.clear();
request.setCharacterEncoding("gb2312");
response.setCharacterEncoding("gb2312");

String db_host="jdbc:mysql://localhost:3306/mydb";
String db_user="root";
String db_password="123456";


String title=request.getParameter("title");
String content=request.getParameter("content");
String username=request.getParameter("username");

title=new String(title.getBytes("iso8859-1"),"UTF-8");
content=new String(content.getBytes("iso8859-1"),"UTF-8");
username=new String(username.getBytes("iso8859-1"),"UTF-8");

try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(db_host,db_user,db_password);

String sql="insert into blog (title,context,username)  values (?,?,?)";

PreparedStatement stmt=con.prepareStatement(sql);

stmt.setString(1,title);
stmt.setString(2,content);
stmt.setString(3,username);
}
catch (Exception e)
{
out.print("1"+e); }


try
{
stmt.executeUpdate();
}

catch (Exception e)
{
out.print("2"+e);

}
finally
{
try
{
stmt.close();
con.close();
}
catch (Exception e)
{
out.print("3"+e);
}
}


%>谢谢了~~

解决方案 »

  1.   

    catch (Exception e)
    {
      out.print("2"+e);
     }把异常发上来,我们不是神仙
      

  2.   

    //就是不知道参数为什么不能传到bbs_post.jsp页面中~~ 所以没看见什么异常~~
            xmlHttp.open("POST","http://localhost:8080/www/bbs_post.jsp",true);
            xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
            xmlHttp.send(param);
      

  3.   

    使用post在脚本中可以用 http://www.xxxx.xxx?para1=value1&para2=value2 ?
    使用get我知道可以, post没有使用表单提交的吗?
      

  4.   

    呵呵 可以改成get试试 如果可以的话 也许是post的问题