Form.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Form</title>
<body bgcolor="#FFFFFF" text="#000000">
<h1>Please enter your details</h1>
<form name="RegistrationForm" action="NewUser" method="post">
<table cellspacing="5" cellpadding="5" border="1">
<tr>
<td align="right">First Name:</td>
<td><input type="text" name="NewFirstName"></td>
</tr>
<tr>
<td align="right">Last Name:</td>
<td><input type="text" name="NewLastName"></td>
</tr>
<tr>
<td align="right">Email Address:</td>
<td><input type="text" name="EmailAddress"></td>
</tr>
<tr>
<td align="right">Phone Number:</td>
<td><input type="text" name="Phone Number"></td>
</tr>
<tr>
<td align="right">Semester</td>
<td><input type="text" name="Semester"></td>
</tr>
</table>
<input type="submit" value="Submit">
</form>
</body>
</html>
NewUser.java(Servlet类)
package com.seria.quiz;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class NewUser
*/
@WebServlet("/NewUser")
public class NewUser extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public NewUser() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
Connection conn = null;
try {
String FirstName = request.getParameter("firstName");
System.out.println("Your firstname: " + FirstName);
String LastName = request.getParameter("lastName");
System.out.println("Your LastName: " + LastName);
String Emailid = request.getParameter("email");
String PhoneNumber = request.getParameter("phoneNumber");
String Semester = request.getParameter("semester");
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/seriaquiz", "root", "root");
PreparedStatement pst = (PreparedStatement) conn
.prepareStatement("insert into formdetails(firstName,lastName,email,phoneNumber,semester) values(?,?,?,?,?)");// try2
// the
// name
pst.setString(1, FirstName);
System.out.println("Your firstname1: " + FirstName);
pst.setString(2, LastName);
System.out.println("Your LastName1: " + LastName);
pst.setString(3, Emailid);
pst.setString(4, PhoneNumber);
pst.setString(5, Semester);
int i = pst.executeUpdate();
String msg = " ";
if (i != 0) {
msg = "Record has been inserted";
pw.println("<font size='6' color=blue>" + msg + "</font>");
} else {
msg = "failed to insert the data";
pw.println("<font size='6' color=blue>" + msg + "</font>");
}
pst.close();
} catch (Exception e) {
pw.println(e);
}
}
}
正如标题所说,request.getParameter每次都返回null。我在requestParameter之后放了sysout语句,它显示了null值。任何帮助将不胜感激。很抱歉给您带来不便,我是新来的。
答案 0 :(得分:1)
servlet中的参数名称需要在JSP中匹配它们
所以
String FirstName = request.getParameter("firstName");
应该是
String FirstName = request.getParameter("NewFirstName");
答案 1 :(得分:0)
你要求错误的参数 你应该总是询问你的html
中描述的相同的id / namerequest.getParameter("NewFirstName");
答案 2 :(得分:0)
来自w3.org部分 17.2控件
用户通过命名控件与表单进行交互。
控件的名称&#34;控制名称&#34;由名称属性提供。一个
的name属性的范围 FORM元素中的控件是FORM元素。当提交表单进行处理时,某些控件的名称与其当前的
配对 价值和这些对与表格一起提交。
控制的例子是你的
<input type="text" name="NewFirstName">
因此,当提交表单时,它包含控件名称=值,例如
NewFirstName=YourFirstName
在JSP中,您应该使用控件名称
访问它String FirstName = request.getParameter("NewFirstName");
答案 3 :(得分:0)
获取表单参数的确切值,试试这个......
String FirstName = request.getParameter(“NewFirstName”);
因为..
通过'NewFirstName'指定name属性...