Javascript 将动态文本输入存储到数据库中
使用servlet将动态文本输入值插入mysql 我有一个表单,使用javascript添加额外的文本字段。 如何在数据库中存储这些值? 在我的servlet中,当我尝试调用request.getParameterValues()时,我得到 java.lang.NullPointerException 这是我的表格Javascript 将动态文本输入存储到数据库中,javascript,html,eclipse,servlets,dynamic-forms,Javascript,Html,Eclipse,Servlets,Dynamic Forms,使用servlet将动态文本输入值插入mysql 我有一个表单,使用javascript添加额外的文本字段。 如何在数据库中存储这些值? 在我的servlet中,当我尝试调用request.getParameterValues()时,我得到 java.lang.NullPointerException 这是我的表格 <form method="POST" action="ServletDB" name="submissionForm" id="submissionForm" onsubmi
<form method="POST" action="ServletDB" name="submissionForm" id="submissionForm" onsubmit="return validate()">
<div id="authors">
<b>Author 1</b><br/>
<b>Name: </b><input type='text' name='name[]'><br/>
<b>Surname:</b><input type='text' name='surName[]'><br/>
<b>Email:</b><input type='text' name='eMail[]'><br/>
<b>Affiliations:</b><textarea rows='4' cols='50' name='affiliations[]'></textarea> <br/>
</div>
<input type="button" value="Add Author" onClick="addAuth('authors');">
函数addAuth(authDiv){
var newAuth=document.createElement('div');
newAuth.innerHTML=“Author”+(authCount+1)+“
姓名:
姓氏:
电子邮件:
联系:
”;
document.getElementById(authDiv).appendChild(newAuth);
authCount++;
}
servlet在DoPost中的实现:
String[] name = req.getParameterValues("name");
String[] surName = req.getParameterValues("surName");
String[] eMail = req.getParameterValues("eMail");
String[] affiliations = req.getParameterValues("affiliations");
int authCount = name.length;
boolean rs1 = true;
try {
// Load the database driver
Class.forName("org.gjt.mm.mysql.Driver");
// Get a Connection to the database
connection = DriverManager.getConnection
(connectionURL, "root","");
//Add the data into the database
for(int i=0;i<authCount;i++){
String sql ="INSERT INTO mytable.author(articleld,Surname,FirstName,Email,) VALUES (?,?,?,?)";
PreparedStatement pst =
connection.prepareStatement(sql);
pst.setString(1,surName[i]);
pst.setString(3,name[i]);
pst.setString(4,eMail[i]);
out.println(pst);
rs1=pst.execute();
pst.close();
}
String[]name=req.getParameterValues(“name”);
字符串[]姓氏=req.getParameterValues(“姓氏”);
字符串[]eMail=req.getParameterValues(“eMail”);
String[]affiliations=req.getParameterValues(“affiliations”);
int authCount=name.length;
布尔值rs1=true;
试一试{
//加载数据库驱动程序
Class.forName(“org.gjt.mm.mysql.Driver”);
//获取到数据库的连接
connection=DriverManager.getConnection
(connectionURL,“根”,“根”);
//将数据添加到数据库中
对于(int i=0;i
var newAuth = document.createElement('div');
newAuth.innerHTML = "Author " + (authCount + 1) + "<br/> <b>Name: </b><input type='text' name='name[]'><br/><b>Surname:</b><input type='text' name='surName[]'><br/><b>Email:</b><input type='text' name='eMail[]'><br/><b>Affiliations:</b><textarea rows='4' cols='50' name='affiliations[]'></textarea><br/>";
document.getElementById(authDiv).appendChild(newAuth);
authCount++;
}
String[] name = req.getParameterValues("name");
String[] surName = req.getParameterValues("surName");
String[] eMail = req.getParameterValues("eMail");
String[] affiliations = req.getParameterValues("affiliations");
int authCount = name.length;
boolean rs1 = true;
try {
// Load the database driver
Class.forName("org.gjt.mm.mysql.Driver");
// Get a Connection to the database
connection = DriverManager.getConnection
(connectionURL, "root","");
//Add the data into the database
for(int i=0;i<authCount;i++){
String sql ="INSERT INTO mytable.author(articleld,Surname,FirstName,Email,) VALUES (?,?,?,?)";
PreparedStatement pst =
connection.prepareStatement(sql);
pst.setString(1,surName[i]);
pst.setString(3,name[i]);
pst.setString(4,eMail[i]);
out.println(pst);
rs1=pst.execute();
pst.close();
}